-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-tree.cc
486 lines (396 loc) · 10.8 KB
/
binary-tree.cc
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*Name: Binary tree
*Operations: pre-order traversal, in-order traversal, post-order traversal, print level order(BFS + watcher)
* in both cursive and non-cursive version
*Skills: queue, map class in C++
*/
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <stack>
using namespace std;
template<typename T>
struct Node
{
T key;
struct Node<T>* left;
struct Node<T>* right;
};
template<typename T>
class Tree
{
public:
Tree ()
: m_root (0)
{
}
virtual ~Tree ()
{
}
/*APIs*/
// init a fixed binary tree
// 1
// 3 5
// 4 7 6
// 2 8
struct Node<T>* Init ();
// Tree traversal: all operations have the O(N) time complexity
// depth first search (recur): end condition + recursive sequence of (Xorder of left and Xorder of right)
void PreorderTraversal (struct Node<T> *node);
void InorderTraversal (struct Node<T> *node);
void PostorderTraversal (struct Node<T> *node);
// depth first search (non recur): single stack
void NonrecurPreorderTraversal (struct Node<T> *node); // + anti recursive sequence of (Xorder of left and Xorder of right)
void NonrecurInorderTraversal (struct Node<T> *node); // + continue to traversal left until it faces the leaf node, then visit, pop and go to right
void NonrecurPostorderTraversal (struct Node<T> *node);
// http://crackprogramming.blogspot.ca/2012/11/non-recursive-binary-tree-traversal.html
// breadth first search (print by level): single queue + watcher pointer/count numbers
void PrintBFS (); // NOTE: this can also be used to find the heigth and depth of a tree
private:
bool IsEmpty ();
struct Node<T>* GetNode (T &key);
private:
struct Node<T>* m_root;
};
template<typename T>
bool Tree<T>::IsEmpty ()
{
if (m_root == 0)
return true;
else
return false;
}
template<typename T>
struct Node<T>* Tree<T>::GetNode(T &key)
{
struct Node<T>* node = new struct Node<T>;
node->key = key;
node->left = 0;
node->right = 0;
return node;
}
template<typename T>
struct Node<T>* Tree<T>::Init ()
{
if (IsEmpty() == false)
return m_root;
int key = 1;
struct Node<T>* node1 = GetNode (key);
key = 3;
struct Node<T>* node2 = GetNode (key);
key = 5;
struct Node<T>* node3 = GetNode (key);
key = 4;
struct Node<T>* node4 = GetNode (key);
key = 7;
struct Node<T>* node5 = GetNode (key);
key = 6;
struct Node<T>* node6 = GetNode (key);
key = 2;
struct Node<T>* node7 = GetNode (key);
key = 8;
struct Node<T>* node8 = GetNode (key);
node1->left = node2;
node1->right = node3;
node2->left = node4;
node2->right = node5;
node4->left = node7;
node3->left = node6;
node6->right = node8;
m_root = node1;
return m_root;
}
template<typename T>
void Tree<T>::PrintBFS ()
{
if (IsEmpty () == true)
{
cout << "empty tree" << endl;
return;
}
queue<struct Node<T>* > queueNodes;
int nextLevelNodesNum = 0; // another way to do level by level
int curLevelNodesNum = 0;
struct Node<T>* cur = m_root;
cout << cur->key << endl; // visit cur
queueNodes.push (cur);
curLevelNodesNum++;
while (!queueNodes.empty ())
{
cur = queueNodes.front ();
queueNodes.pop ();
curLevelNodesNum--;
if (cur->left != 0)
{
cout << cur->left->key << " ";
queueNodes.push (cur->left);
nextLevelNodesNum++;
}
if (cur->right != 0)
{
cout << cur->right->key << " ";
queueNodes.push (cur->right);
nextLevelNodesNum++;
}
if (curLevelNodesNum == 0)
{
// this is the end of level
cout << endl;
// update current level to the next level
curLevelNodesNum = nextLevelNodesNum;
// reset the next level node number to zero
nextLevelNodesNum = 0;
}
}// while ()
}
template<typename T>
void Tree<T>::PreorderTraversal (struct Node<T> *node)
{
if (node == 0)
return;
// visit node
cout << node->key << " ";
PreorderTraversal(node->left);
PreorderTraversal(node->right);
}
template<typename T>
void Tree<T>::InorderTraversal (struct Node<T> *node)
{
if (node == 0)
return;
InorderTraversal(node->left);
// visit node
cout << node->key << " ";
InorderTraversal(node->right);
}
template<typename T>
void Tree<T>::PostorderTraversal (struct Node<T> *node)
{
if (node == 0)
return;
PostorderTraversal(node->left);
PostorderTraversal(node->right);
// visit node
cout << node->key << " ";
}
/**
* 1 stack
* push right first and then left
* visit and pop left first
then go right
*/
template<typename T>
void Tree<T>::NonrecurPreorderTraversal (struct Node<T> *node)
{
if (node == 0)
return;
stack<struct Node<T>* > stackNodes;
// push root into stack
stackNodes.push (node);
while (!stackNodes.empty ()) // stackNodes.size != 0
{
struct Node<T>* top = stackNodes.top ();
// visit node
cout << top->key << " ";
stackNodes.pop ();
if ( top->right != 0 )
stackNodes.push (top->right);
if ( top->left !=0 )
stackNodes.push (top->left);
}
}
/**
* 1 stack
* track to the most left node and push the node on the track path into stack
* vsit and then pop the most left node
then go to its right, repeat previous steps
*/
template<typename T>
void Tree<T>::NonrecurInorderTraversal (struct Node<T> *node)
{
if (node == 0)
return;
stack<struct Node<T>* > stackNodes;
struct Node<T>* cur = node;
while ( !stackNodes.empty () || cur ) // stackNodes.size != 0
{
// If this is a empty node, it means that we face the left empty child of leaf node;
// Operations: visit node, pop top node and then go to its right
if ( cur == 0 )
{
struct Node<T>* top = stackNodes.top ();
cout << top->key << " ";
stackNodes.pop ();
cur = top-> right;
}
else
{ // if this is not an empty node, we just keep traversal the left node until we face the leaf node
stackNodes.push (cur);
cur = cur->left;
}
} // while
}
/**
* 1 stack
* define a pre pointer, which always trake the previous node visited by cur pointer
* go to the left most node, push all nodes on the track path
visit and then pop
then two cases: cur is the left leaf, go right leaf if it has;
cur is the right leaf, visit and then pop
*/
template<typename T>
void Tree<T>::NonrecurPostorderTraversal (struct Node<T> *node)
{
if (node == 0)
return;
stack<struct Node<T>* > stackNodes;
struct Node<T>* cur = node;
struct Node<T>* pre = 0; // always equal to the previous visited node
stackNodes.push (cur);
// once the node is visited, pop it
while (!stackNodes.empty ())
{
cur = stackNodes.top ();
// in following cases: visit node when cur is the leaf node
// (1) (2) (3)
// pre -> 0
// cur -> root pre pre
// / \
// cur cur
// (1): this case happens in the beginning
// (2): this case happens when the cur goes down along the left path
// (3): this case happens when left child is visited and the parent has the right child
if (!pre || pre->left == cur || pre->right == cur)
{
if (cur->left)
stackNodes.push (cur->left);
else if (cur->right)
stackNodes.push (cur->right);
else
{
cout << cur->key << " ";
stackNodes.pop ();
}
}
// in following cases: this case only happens after the left child (pre) being visited
// when cur does not have right child, visit node
// cur
// /
// pre
//
else if ( cur->left == pre )
{
if (cur->right)
stackNodes.push (cur->right);
else
{
cout << cur->key << " ";
stackNodes.pop ();
}
}
// in following cases: this case only happens after the right child (pre) being visited
// in this case, both left and right childs are visited, then, the
// root node should be visited now
// cur
// \
// pre
//
else if ( cur->right == pre )
{
cout << cur->key << " ";
stackNodes.pop ();
}
pre = cur;
} // while
}
/* use wactch pointer
template<typename T>
void Tree<T>::PrintBFS ()
{
if (IsEmpty () == true)
{
cout << "empty tree" << endl;
return;
}
queue<struct Node<T>* > queueNodes;
struct Node<T>* cur = m_root;
cout << cur->key << endl; // visit cur
queueNodes.push (cur);
// set a watcher to always keep the last node in a level
struct Node<T>* watcher = 0;
if (cur->right != 0)
watcher = cur->right;
else if (cur->left != 0)
watcher = cur->left;
else
return;
while (!queueNodes.empty ())
{
cur = queueNodes.front ();
if (cur == watcher)
cout << endl;
queueNodes.pop ();
if (cur->left != 0)
{
cout << cur->left->key << " ";
queueNodes.push (cur->left);
if (cur->left == watcher)
{
cout << endl;
// update watcher
struct Node<T>* node = cur->left;
if (node->right != 0)
watcher = node->right;
else if (node->left != 0)
watcher = node->left;
}
}
if (cur->right != 0)
{
cout << cur->right->key << " ";
queueNodes.push (cur->right);
if (cur->right == watcher)
{
cout << endl;
// update watcher
struct Node<T>* node = cur->right;
if (node->right != 0)
watcher = node->right;
else if (node->left != 0)
watcher = node->left;
}
}
}// while ()
}
*/
int main (int argc, char *argv[])
{
typedef int type;
Tree<type> tree;
struct Node<type>* root;
root = tree.Init ();
cout << "BFS: " << endl;
tree.PrintBFS ();
cout << endl;
cout << "pre-order: " << endl;
tree.PreorderTraversal (root);
cout << endl;
cout << "pre-order (non-recur): " << endl;
tree.NonrecurPreorderTraversal (root);
cout << endl;
cout << endl;
cout << "in-order: " << endl;
tree.InorderTraversal (root);
cout << endl;
cout << "in-order (non-recur): " << endl;
tree.NonrecurInorderTraversal (root);
cout << endl;
cout << endl;
cout << "post-order: " << endl;
tree.PostorderTraversal (root);
cout << endl;
cout << "post-order (non-recur): " << endl;
tree.NonrecurPostorderTraversal (root);
cout << endl;
return 0;
}