-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree.cpp
181 lines (169 loc) · 4.41 KB
/
binary_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include "my_stack.cpp"
#include "mqueue.cpp"
using namespace std;
template <typename T>
class array_binary_tree
{
private:
public:
T *Datas;
bool *mark;
int capacity;
int size;
array_binary_tree(int size = 1);
bool is_full(){ return size==capacity?true:false;}
bool is_empty(){ return size==0?true:false;}
void push_back(const T);
void full_handler();
};
template <typename T>
void array_binary_tree<T>::full_handler()
{
T *ns = new T[capacity*2];
bool nm[capacity*2];
for(int i = 0;i< capacity ;i++)
{
ns[i] = Datas[i];
nm[i] = mark[i];
}
capacity *=2;
mark = nm;
Datas = ns;
}
template <typename T>
array_binary_tree<T>::array_binary_tree(int cap):capacity(cap)
{
capacity = cap;
Datas = new T[cap];
mark = new bool[cap];
size = 0;
}
template <typename T>
void array_binary_tree<T>::push_back(const T x)
{
if(is_full())
full_handler();
mark[size] = true;
Datas[size++] = x;
//cout << Datas[size-1]<<endl;
}
template <typename T> class linked_tree;
template <typename T>
class linked_node
{
private:
friend class linked_tree<T>;
public:
bool mark;
T data;
int value;
linked_node<T> *left,*right;
linked_node(){right = left = nullptr,mark = false,value = 0; }
linked_node(T);
friend bool operator>(const linked_node<T>& a,const linked_node<T> & b){return a.data > b.data?true:false;}
friend bool operator>=(const linked_node<T> &a,const linked_node<T> &b){return a.data >= b.data?true:false;}
friend bool operator<(const linked_node<T> &a,const linked_node<T> &b){return a.data < b.data?true:false;}
friend bool operator<=(const linked_node<T> &a,const linked_node<T> &b){return a.data <= b.data?true:false;}
};
template <typename T>
linked_node<T>::linked_node(T x)
{
data = x;
left = nullptr;
right = nullptr;
}
template <typename T>
class linked_tree
{
private:
public:
linked_node<T> *root;
linked_tree(){root = nullptr;}
linked_tree(linked_node<T>* ptr){root = ptr;}
void inorder(){inorder(root),cout<<endl;}
void inorder(linked_node<T> *currentNode);
void postorder(){postorder(root),cout<<endl;}
void postorder(linked_node<T> *currentNode);
void preorder(){preorder(root),cout<<endl;}
void preorder(linked_node<T> *currentNode);
void levelorder(){levelorder(root),cout<<endl;}
void levelorder(linked_node<T> *currentNode);
linked_node<T>* copy(linked_node<T> *);
linked_tree(const linked_tree<T> &s){root = copy(s.root);}
};
template <typename T>
void linked_tree<T>::inorder(linked_node<T> *currentNode)
{
if(currentNode != nullptr)
{
inorder(currentNode->left);
cout << currentNode->data<<" ";
inorder(currentNode->right);
}
return;
}
template <typename T>
void linked_tree<T>::preorder(linked_node<T> *currentNode)
{
my_stack<linked_node<T>*> s;
s.push(currentNode);
while(!s.is_empty())
{
currentNode = s.pop();
cout<< currentNode->data << " ";
if(currentNode->right)
s.push(currentNode->right);
if(currentNode->left)
s.push(currentNode->left);
}
}
template <typename T>
void linked_tree<T>::postorder(linked_node<T> *currentNode)
{
my_stack<linked_node<T>*> s;
s.push(currentNode);
while(!s.is_empty())
{
currentNode = s.pop();
if(currentNode->mark)
cout << currentNode->data << " ";
else
{
s.push(currentNode);
currentNode->mark = true;
if(currentNode->right )
s.push(currentNode->right);
if(currentNode->left)
s.push(currentNode->left);
}
}
}
template <typename T>
void linked_tree<T>::levelorder(linked_node<T> *currentNode)
{
c_queue<linked_node<T> *> q;
q.push_back(currentNode);
while(!q.is_empty())
{
currentNode = q.pop();
cout << currentNode->data<<" ";
if (currentNode->left)
q.push_back(currentNode->left);
if(currentNode->right)
q.push_back(currentNode->right);
}
}
template <typename T>
linked_node<T>* linked_tree<T>::copy(linked_node<T>* x)
{
if(x)
{
linked_node<T> *temp = new linked_node<T>;
temp->data = x->data;
temp->left = copy(x->left);
temp->right = copy(x->right);
return temp;
}
return nullptr;
}