-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
164 lines (152 loc) · 3.61 KB
/
Copy pathtree.cpp
File metadata and controls
164 lines (152 loc) · 3.61 KB
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
#include <stdio.h>
#include <stack>
struct Node
{
int value;
Node *left;
Node *right;
Node(int v)
{
value = v;
left = NULL;
right = NULL;
}
};
Node * build_tree(int *& array, int& len)
{
if (len <= 0) return NULL;
int v = array[0];
array++;
len--;
if (v == -1) return NULL;
Node *n = new Node(v);
n->left = build_tree(array, len);
n->right = build_tree(array, len);
return n;
}
#if 0
#define PRINTHASH printf("# ")
#else
#define PRINTHASH
#endif
void preorder_traverse(Node *root)
{
if (!root) { PRINTHASH; return; }
printf("%d ", root->value);
preorder_traverse(root->left);
preorder_traverse(root->right);
}
void preorder_traverse_stack(Node *root)
{
std::stack<Node *> s;
while (root) {
printf("%d ", root->value);
if (root->left) {
if (root->right) {
s.push(root->right);
}
root = root->left;
} else if (root->right) {
root = root->right;
} else if (s.size() > 0) {
root = s.top();
s.pop();
} else {
break;
}
}
}
void inorder_traverse(Node *root)
{
if (!root) { PRINTHASH; return; }
inorder_traverse(root->left);
printf("%d ", root->value);
inorder_traverse(root->right);
}
void inorder_traverse_stack(Node *root)
{
std::stack<Node *> s;
bool go_left = true;
while (root) {
bool last = go_left;
go_left = true;
if (last && root->left) {
s.push(root);
root = root->left;
} else if (root->right) {
printf("%d ", root->value);
root = root->right;
} else {
printf("%d ", root->value);
if (s.size() > 0) {
root = s.top();
s.pop();
go_left = false;
} else {
break;
}
}
}
}
void postorder_traverse(Node *root)
{
if (!root) { PRINTHASH; return; }
postorder_traverse(root->left);
postorder_traverse(root->right);
printf("%d ", root->value);
}
void postorder_traverse_stack(Node *root)
{
std::stack<Node *> s;
Node * last_print = NULL;
while (root) {
if (last_print && (last_print == root->left || last_print == root->right)) {
printf("%d ", root->value);
last_print = root;
if (s.size() > 0) {
root = s.top();
s.pop();
continue;
} else {
break;
}
}
if (root->left) {
s.push(root);
if (root->right) {
s.push(root->right);
}
root = root->left;
} else if (root->right) {
s.push(root);
root = root->right;
} else {
printf("%d ", root->value);
last_print = root;
if (s.size() > 0) {
root = s.top();
s.pop();
} else {
break;
}
}
}
}
void test1()
{
int tree1[] = {0, 1, 3, 7, -1, -1, 8, -1, -1, 4, -1, -1, 2, 5, 11, -1, -1, -1, 6, -1, 14, -1, -1};
int *ptree1 = tree1;
int len1 = sizeof(tree1) / sizeof(int);
Node * root1 = build_tree(ptree1, len1);
preorder_traverse(root1); printf("\n");
preorder_traverse_stack(root1); printf("\n");
inorder_traverse(root1); printf("\n");
inorder_traverse_stack(root1); printf("\n");
postorder_traverse(root1); printf("\n");
postorder_traverse_stack(root1); printf("\n");
}
int main()
{
test1();
return 0;
}