-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarytree.h
377 lines (331 loc) · 9.22 KB
/
binarytree.h
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#pragma once
#include "btree.h"
#include <iostream>
#include "linkqueue.h"
#include "linkStack.h"
template <class T>
class binaryTree;
template <class T>
T max(T a, T b){ return (a > b) ? a : b;}
template <class T>
void print_tree(const binaryTree<T> &t, T flag);
template <class T>
class node{
friend binaryTree<T>;
private:
T data;
node *left, *right;
node() = default;
explicit node(T d, node* L = nullptr, node* R = nullptr){ data = d; left = L, right = R;}
~node() = default;
};
template <class T>
class binaryTree: public bTree<T>{
friend void print_tree<T>(const binaryTree<T> &t, T flag);
private:
node<T>* Root;
node<T>* find(T x, node<T>* t) const;
void clear(node<T>* &t);
// void pre_order(node<T>* t) const; // if implement traverse with recurse, these private functions are needed.
// void mid_order(node<T>* t) const;
// void post_order(node<T>* t) const;
int size(node<T> *t) const;
int height(node<T> *t) const;
node<T> *build_tree(const T pre[], const T mid[], int ps, int pe, int ms, int me);
public:
explicit binaryTree(node<T>* n = nullptr) : Root(n){}
~binaryTree(){clear(Root);}
void create_tree(T flag);
void build_tree(const T pre[], const T mid[], int n); // build tree from the given pre_order and mid_order
void clear();
[[nodiscard]] bool empty() const;
T root(T flag) const;
T parent(T x, T flag) const {return flag;}
T lchild(T x, T flag) const;
T rchild(T x, T flag) const;
void pre_order() const;
void mid_order() const;
void post_order() const;
void level_order() const;
[[nodiscard]] int size() const;
[[nodiscard]] int height() const;
void del_left(T x);
void del_right(T x);
};
template<class T>
node<T> *binaryTree<T>::find(T x, node<T> *t) const {
node<T> *tmp;
if (!t) return nullptr;
if (t->data == x) return t;
if ((tmp = find(x, t->left))) return tmp;
return find(x, t->right);
}
template<class T>
void binaryTree<T>::clear(node<T> *&t) {
if (!t) return;
clear(t->left);
clear(t->right);
delete t;
t = nullptr;
}
template<class T>
int binaryTree<T>::size(node<T> *t) const {
if (!t) return 0;
return 1 + size(t->left) + size(t->right);
}
template<class T>
void binaryTree<T>::create_tree(T flag) {
// create a tree from keyboard, ending with flag
T elem, left_child, right_child;
node<T>* parent;
linkQueue<node<T> *> que;
std::cout << "Starting to create a new tree(input " << flag << " for empty node)" << std::endl << "Please input the Root: ";
std::cin >> elem;
if (elem == flag) return;
Root = new node<T>(elem);
que.push(Root);
while(!que.empty()){
parent = que.pop();
std::cout << "Please input lchild and rchild of " << parent->data<< ": ";
std::cin >> left_child >> right_child;
if (left_child != flag) que.push(parent->left = new node<T>(left_child));
if (right_child != flag) que.push(parent->right = new node<T>(right_child));
}
std::cout << "Create completed!\n";
}
template<class T>
void binaryTree<T>::clear() {
clear(Root);
}
template<class T>
bool binaryTree<T>::empty() const {
return Root == nullptr;
}
template<class T>
T binaryTree<T>::root(T flag) const {
if (!Root) return flag;
return Root->data;
}
template<class T>
T binaryTree<T>::lchild(T x, T flag) const {
node<T> *tmp = find(x, Root);
if (!tmp or !tmp->left) return flag;
return tmp->left->data;
}
template<class T>
T binaryTree<T>::rchild(T x, T flag) const {
node<T> *tmp = find(x, Root);
if (!tmp or !tmp->right) return flag;
return tmp->right->data;
}
// implement traverse without recurse
template<class T>
void binaryTree<T>::pre_order() const{
std::cout << "\nTraverse with pre_order:\n";
if (empty()) return;
node<T> *p;
linkStack<node<T>*> s;
s.push(Root);
while(!s.empty()){
p = s.pop();
std::cout << p->data << ' ';
if (p->right) s.push(p->right);
if (p->left) s.push(p->left);
}
std::cout << std::endl;
}
template<class T>
void binaryTree<T>::mid_order() const {
std::cout << "\nTraverse with mid_order:\n";
if (empty()) return;
node<T> *p; bool q;
linkStack<node<T>*> s1;
linkStack<bool> s2;
s1.push(Root);
s2.push(false);
while(!s1.empty()){
p = s1.pop(); q = s2.pop();
if (!q) {
s1.push(p);
s2.push(true);
if (p->left) {
s1.push(p->left);
s2.push(false);
}
}
if (q) {
std::cout << p->data << ' ';
if (p->right) {
s1.push(p->right);
s2.push(false);
}
}
}
std::cout << std::endl;
}
template<class T>
void binaryTree<T>::post_order() const{
std::cout << "\nTraverse with post_order:\n";
if (empty()) return;
node<T> *p;
int q;
linkStack<node<T>*> s1;
linkStack<int> s2;
s1.push(Root);
s2.push(0);
while(!s1.empty()){
p = s1.pop();
q = s2.pop();
if (q == 0) {
s1.push(p);
s2.push(1);
if (p->left){
s1.push(p->left);
s2.push(0);
}
}
if (q == 1){
s1.push(p);
s2.push(2);
if (p->right){
s1.push(p->right);
s2.push(0);
}
}
if (q == 2)
std::cout << p->data << ' ';
}
std::cout << std::endl;
}
template<class T>
void binaryTree<T>::level_order() const {
std::cout << "\nTraverse with level_order:\n";
linkQueue<node<T>* > que;
node<T> *parent;
if (!Root) return;
que.push(Root);
while(!que.empty()){
parent = que.pop();
std::cout << parent->data << ' ';
if (parent->left) que.push(parent->left);
if (parent->right) que.push(parent->right);
}
std::cout << std::endl;
}
template<class T>
void binaryTree<T>::del_left(T x) {
node<T> * tmp = find(x, Root);
if (!tmp) return;
clear(tmp->left);
}
template<class T>
void binaryTree<T>::del_right(T x) {
node<T> * tmp = find(x, Root);
if (!tmp) return;
clear(tmp->right);
}
template<class T>
int binaryTree<T>::height(node<T> *t) const {
if (!t) return 0;
return 1 + max(height(t->left), height(t->right));
}
template<class T>
int binaryTree<T>::size() const {
return size(Root);
}
template<class T>
int binaryTree<T>::height() const {
return height(Root);
}
template <class T>
void print_tree(const binaryTree<T> &t, T flag){
// int h, cor = 1, count = 1;
// h = t.height();
// for (int i = 0; i < h - 1 ; ++i)
// cor *= 2;
// linkQueue<cordination<T>> q;
// cordination<T> cordi(root(flag), cor);
// q.push(cordi);
linkQueue<T> q;
q.push(t.root(flag));
std::cout << std::endl << "Parent" << " Lchild" << " Rchild:" << std::endl;
while(!q.empty()){
T p, l, r;
p = q.pop();
l = t.lchild(p, flag);
r = t.rchild(p, flag);
// for (int i = 0; i < p.left_distance - 1; ++i){
// std::cout << ' ';
// }
// if (p.data == flag) std::cout << flag;
// else {
// std::cout << p.data;
//
// }
std::cout << p << "\t\t" << l << "\t\t" << r << std::endl;
if (l != flag) q.push(l);
if (r != flag) q.push(r);
}
}
template<class T>
void binaryTree<T>::build_tree(const T *pre, const T *mid, int n) {
clear(Root);
Root = build_tree(pre, mid, 0, n - 1, 0, n - 1);
}
template<class T>
node<T> *binaryTree<T>::build_tree(const T *pre, const T *mid, int ps, int pe, int ms, int me) {
if (ps > pe) return nullptr;
auto t = new node<T>(pre[ps]);
int left_mid_s = ms, left_mid_e, left_pre_s = ps + 1, left_pre_e, right_mid_s, right_mid_e = me, right_pre_s, right_pre_e = pe;
for (int i = ms; i <= me; ++i){
if (mid[i] == pre[ps]){
left_mid_e = i - 1;
right_mid_s = i + 1;
left_pre_e = ps + 1 + left_mid_e - left_mid_s;
right_pre_s = left_pre_e + 1;
break;
}
}
t->left = build_tree(pre, mid, left_pre_s, left_pre_e, left_mid_s, left_pre_e);
t->right = build_tree(pre, mid, right_pre_s, right_pre_e, right_mid_s, right_mid_e);
return t;
}
// implement traverse recursively
/*
template<class T>
void binaryTree<T>::pre_order(node<T> *t) const {
if (!t) return;
std::cout << t->data << ' ';
pre_order(t->left);
pre_order(t->right);
}
template<class T>
void binaryTree<T>::mid_order(node<T> *t) const {
if (!t) return;
mid_order(t->left);
std::cout << t->data << ' ';
mid_order(t->right);
}
template<class T>
void binaryTree<T>::post_order(node<T> *t) const {
if (!t) return;
post_order(t->left);
post_order(t->right);
std::cout << t->data << ' ';
}
template<class T>
void binaryTree<T>::pre_order() const {
std::cout << "\nTraverse with pre_order:\n";
pre_order(Root);
}
template<class T>
void binaryTree<T>::mid_order() const {
std::cout << "\nTraverse with mid_order:\n";
mid_order(Root);
}
template<class T>
void binaryTree<T>::post_order() const {
std::cout << "\nTraverse with post_order:\n";
post_order(Root);
}
*/