-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
traversal.cpp
59 lines (49 loc) · 1.26 KB
/
traversal.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
/*************************************************************************
> File Name: travelsal.cpp
> Author: Louis1992
> Mail: [email protected]
> Blog: http://gzc.github.io
> Created Time: Mon Aug 10 19:04:08 2015
************************************************************************/
#include<iostream>
using namespace std;
struct tree_t {
struct tree_t *left;
struct tree_t *right;
struct tree_t *parent;
int key;
tree_t() {left = 0; right = 0; parent = 0;}
};
void print_tree(tree_t *tree) {
tree_t *prev;
prev = 0;
while (tree) {
if (prev == tree->parent) {
cout << tree->key << endl;
prev = tree;
tree = tree->left ? tree->left :
tree->right ? tree->right :
tree->parent;
} else if (prev == tree->left && tree->right) {
prev = tree;
tree = tree->right;
} else {
prev = tree;
tree = tree->parent;
}
}
}
int main() {
tree_t root;
root.key = 1;
tree_t left;
left.key = 2;
tree_t right;
right.key = 3;
root.left = &left;
root.right = &right;
left.parent = &root;
right.parent = &root;
print_tree(&root);
return 0;
}