-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.cpp
172 lines (145 loc) · 4.33 KB
/
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
// summary for tree
// Able to write a TreeNode class
class TreeNode {
public:
int val;
TreeNode *left, *right;
TreeNode(int val) : val(val), left(NULL), right(NULL) {}
};
//>>>>>>>>>>> Depth-First Traversal & Breadth(Level)-First Traversal
// 1. Pre-order Traversal
void preorder_traverse(TreeNode *root, vector<int> &vals) {
if (root == NULL) return;
stack<TreeNode *> nodes;
nodes.push(root);
while (!nodes.empty()) {
root = nodes.top();
nodes.pop();
vals.push_back(root->val); // or do something
if (root->right != NULL) {
nodes.push(root->right);
}
if (root->left != NULL) {
nodes.push(root->left);
}
}
return;
}
// 2. In-order Traversal
void inorder_traverse(TreeNode *root, vector<int> &vals) {
if (root == NULL) return;
stack<TreeNode *> nodes;
while (root != NULL || !nodes.empty()) {
if (root != NULL) {
nodes.push(root);
root = root->left;
} else {
root = nodes.top();
nodes.pop();
vals.push_back(root->val); // or do something
root = root->right;
}
}
return;
}
// 3. Post-order Traversal
void postorder_traverse(TreeNode *root, vector<int> &vals) {
if (root == NULL) return;
stack<TreeNode *> nodes;
TreeNode *prev = NULL; // for later check
while (root != NULL || !nodes.empty()) {
if (root != NULL) {
nodes.push(root);
root = root->left;
} else {
TreeNode *curr = nodes.top(); // needs a new pointer
if (curr->right != NULL && prev != curr->right) { // check if processed
root = curr->right;
} else {
vals.push_back(curr->val); // or do something
prev = curr;
nodes.pop();
}
}
}
return;
}
// A general method for writing depth-first traversal
class Guide {
public:
int operation;
TreeNode *node;
Guide(int operation, TreeNode *node) {
this->operation = operation; // 0: visit, 1: print;
this->node = node;
}
};
void postorder_traverse(TreeNode *root, vector<int> &vals) {
stack<Guide> path;
path.push(Guide(0, root));
while (!path.empty()) {
Guide curr = path.top();
path.pop();
if (curr.node == nullptr) continue;
if (curr.operation == 1) {
vals.push_back(curr.node->val);
} else {
// push guide in the reverse order
// post-order
path.push(Guide(1, curr.node));
path.push(Guide(0, curr.node->right));
path.push(Guide(0, curr.node->left));
// in-order
path.push(Guide(0, curr.node->right));
path.push(Guide(1, curr.node));
path.push(Guide(0, curr.node->left));
// pre-order
path.push(Guide(0, curr.node->right));
path.push(Guide(0, curr.node->left));
path.push(Guide(1, curr.node));
}
}
return;
}
// 4. Breadth-First Traversal
void level_traverse(TreeNode *root, vector<int> &vals) {
if (root == NULL) return;
queue<TreeNode *> nodes;
nodes.push(root);
while (!nodes.empty()) {
root = nodes.top();
nodes.pop();
vals.push_back(root->val);
if (root->left != NULL) {
nodes.push(root->left);
}
if (root->right != NULL) {
nodes.push(root->right);
}
}
return;
}
// another form: process level by level
void level_traverse(TreeNode *root, vector<int> &vals) {
if (root == NULL) return;
queue<TreeNode *> nodes;
nodes.push(root);
while (!nodes.empty()) {
int size = nodes.size();
for (int i = 0; i < size; ++i) {
root = nodes.front();
nodes.pop();
vals.push_back(root->val);
if (root->left != NULL) {
nodes.push(root->left);
}
if (root->right != NULL) {
nodes.push(root->right);
}
}
}
return;
}
// difference between traverse and divide & conquer
// traverse: taking notes while trasversing the tree
// divide & conquer: making decision based on the notes passed from subtrees