-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiterative-implementation.cpp
254 lines (228 loc) · 4.89 KB
/
iterative-implementation.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
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
/* binary tree traversal iterative implementation */
//
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
/**
* @brief Tree node in bst
*
*/
class TreeNode {
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
/**
* @brief maximum depth(height) of the node
*
* @return int
*/
int max_depth() {
int left_depth = 0, right_depth = 0;
if (left) {
left_depth = left->max_depth();
}
if (right) {
right_depth = right->max_depth();
}
return left_depth > right_depth ? left_depth + 1 : right_depth + 1;
}
};
class BinarySearchTree {
public:
/**
* @brief root tree node
*
*/
TreeNode *root;
/**
* @brief Construct a new Binary Search Tree object
*
*/
BinarySearchTree() { root = NULL; }
/**
* @brief Destroy the Binary Search Tree object
*
*/
~BinarySearchTree() {
if (root != NULL) {
root = NULL;
}
}
/**
* @brief insert a node to the binary tree
*
* @param val
*/
void insert(int val) {
if (root == NULL) {
root = new TreeNode(val);
} else {
TreeNode *cur = root;
while (cur != NULL) {
if (val < cur->val) {
if (cur->left == NULL) {
cur->left = new TreeNode(val);
break;
} else {
cur = cur->left;
}
} else {
if (cur->right == NULL) {
cur->right = new TreeNode(val);
break;
} else {
cur = cur->right;
}
}
}
}
}
/**
* @brief Inorder traversal iterative implementation
* @return vector<int>
*/
vector<int> InOrderTraversal() {
vector<int> result;
if (root == NULL)
return result;
stack<TreeNode *> s;
TreeNode *cur = root;
while (cur != NULL || !s.empty()) {
if (cur != NULL) {
s.push(cur);
cur = cur->left;
} else {
cur = s.top();
s.pop();
result.push_back(cur->val);
cur = cur->right;
}
}
return result;
}
/**
* @brief Preorder traversal iterative implementation
*
* @param root
* @return vector<int>
*/
vector<int> PreorderTraversal() {
vector<int> result;
if (root == NULL)
return result;
stack<TreeNode *> s;
TreeNode *cur = root;
s.push(root);
while (!s.empty()) {
cur = s.top();
s.pop();
if (cur != NULL) {
if (cur->right != NULL) {
s.push(cur->right);
}
if (cur->left != NULL) {
s.push(cur->left);
}
result.push_back(cur->val);
}
}
return result;
}
/**
* @brief Postorder traversal iterative implementation
*
* @param root
* @return vector<int>
*/
vector<int> PostorderTraversal() {
vector<int> result;
if (root == NULL)
return result;
stack<TreeNode *> s;
TreeNode *cur = root;
s.push(root);
while (!s.empty()) {
cur = s.top();
s.pop();
if (cur != NULL) {
result.push_back(cur->val);
if (cur->left != NULL) {
s.push(cur->left);
}
if (cur->right != NULL) {
s.push(cur->right);
}
}
}
return result;
}
/**
* @brief prints the traversal of the tree
*
* @param vec
*/
void print_vector(vector<int> vec) {
for (int i = 0; i < vec.size(); i++) {
std::cout << vec.at(i) << " ";
}
std::cout << std::endl;
}
/**
* @brief prints a binary tree
*
* @param prefix
* @param node
* @param isLeft
*/
void printBT(string prefix, const TreeNode *node, bool isLeft) {
if (node != nullptr) {
std::cout << (string)(prefix);
std::cout << (isLeft ? "├──" : "└──");
// print the value of the node
std::cout << node->val << std::endl;
// enter the next tree level - left and right branch
printBT(prefix + (isLeft ? "│ " : " "), node->left, true);
printBT(prefix + (isLeft ? "│ " : " "), node->right, false);
}
}
/**
* @brief draw binary tree
*
*/
void draw_tree() {
std::cout << std::endl;
printBT("", root, false);
}
};
int main(int argc, const char **argv) {
BinarySearchTree BStree;
BStree.insert(5);
BStree.insert(3);
BStree.insert(2);
BStree.insert(4);
BStree.insert(6);
BStree.insert(7);
BStree.insert(8);
BStree.insert(9);
BStree.insert(10);
BStree.insert(11);
BStree.insert(1);
std::cout << "Inorder traversal :"
<< " ";
vector<int> vec = BStree.InOrderTraversal();
BStree.print_vector(vec);
std::cout << "Preorder traversal :"
<< " ";
vec = BStree.PreorderTraversal();
BStree.print_vector(vec);
std::cout << "Postorder traversal :"
<< " ";
vec = BStree.PostorderTraversal();
BStree.print_vector(vec);
BStree.draw_tree();
return 0;
}