-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
Copy pathiterative_tree_traversals.cpp
421 lines (367 loc) · 14.6 KB
/
iterative_tree_traversals.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
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
/**
* @file
* @brief Iterative version of Preorder, Postorder, and preorder [Traversal of
* the Tree] (https://en.wikipedia.org/wiki/Tree_traversal)
* @author [Motasim](https://github.com/motasimmakki)
* @details
*
* ### Iterative Preorder Traversal of a tree
* Create a Stack that will store the Node of Tree.
* Push the root node into the stack.
* Save the root into the variabe named as current, and pop and elemnt from the
* stack. Store the data of current into the result array, and start traversing
* from it. Push both the child node of the current node into the stack, first
* right child then left child. Repeat the same set of steps untill the Stack
* becomes empty. And return the result array as the preorder traversal of a
* tree.
*
* ### Iterative Postorder Traversal of a tree
* Create a Stack that will store the Node of Tree.
* Push the root node into the stack.
* Save the root into the variabe named as current, and pop and elemnt from the
* stack. Store the data of current into the result array, and start traversing
* from it. Push both the child node of the current node into the stack, first
* left child then right child. Repeat the same set of steps untill the Stack
* becomes empty. Now reverse the result array and then return it to the calling
* function as a postorder traversal of a tree.
*
* ### Iterative Inorder Traversal of a tree
* Create a Stack that will store the Node of Tree.
* Push the root node into the stack.
* Save the root into the variabe named as current.
* Now iterate and take the current to the extreme left of the tree by
* traversing only to its left. Pop the elemnt from the stack and assign it to
* the current. Store the data of current into the result array. Repeat the same
* set of steps until the Stack becomes empty or the current becomes NULL. And
* return the result array as the inorder traversal of a tree.
*/
#include <algorithm> /// for `reverse`
#include <cassert> /// for `assert`
#include <iostream> /// for I/O operations
#include <stack> /// for `stack`
#include <vector> /// for `vector`
/**
* @namespace others
* @brief Other algorithms
*/
namespace others {
/**
* @namespace iterative_tree_traversals
* @brief Functions for the [Traversal of the
* Tree](https://en.wikipedia.org/wiki/Tree_traversal) algorithm
*/
namespace iterative_tree_traversals {
/**
* @brief defines the structure of a node of the tree
*/
struct Node {
int64_t data = 0; ///< The value/key of the node.
struct Node *left{}; ///< struct pointer to left subtree.
struct Node *right{}; ///< struct pointer to right subtree.
};
/**
* @brief defines the functions associated with the binary tree
*/
class BinaryTree {
public:
Node *createNewNode(
int64_t); ///< function that will create new node for insertion.
std::vector<int64_t> preOrderIterative(
Node *); ///< function that takes root of the tree as an argument, and
///< returns its preorder traversal.
std::vector<int64_t> postOrderIterative(
Node *); ///< function that takes root of the tree as an argument, and
///< returns its postorder traversal.
std::vector<int64_t> inOrderIterative(
Node *); ///< function that takes root of the tree as an argument, and
///< returns its inorder traversal.
};
/**
* @brief will allocate the memory for a node and, along the data and return the
* node.
* @param data value that a particular node will contain.
* @return pointer to the newly created node with assigned data.
*/
Node *BinaryTree::createNewNode(int64_t data) {
Node *node = new Node();
node->data = data;
node->left = node->right = nullptr;
return node;
}
/**
* @brief preOrderIterative() function that will perform the preorder traversal
* iteratively, and return the result array that contain the preorder traversal
* of a tree.
* @param root head/root node of a tree
* @return result that is containing the preorder traversal of a tree
*/
std::vector<int64_t> BinaryTree::preOrderIterative(Node *root) {
std::stack<Node *>
stack; ///< is used to find and traverse the child nodes.
std::vector<int64_t> result; ///< list of values, sorted in pre-order.
stack.push(root);
while (!stack.empty()) {
result.push_back(stack.top()->data);
Node *current = stack.top();
stack.pop();
if (current->right) {
stack.push(current->right);
}
if (current->left) {
stack.push(current->left);
}
}
return result;
}
/**
* @brief postOrderIterative() function that will perform the postorder
* traversal iteratively, and return the result array that contain the postorder
* traversal of a tree.
* @param root head/root node of a tree
* @return result that is containing the postorder traversal of a tree
*/
std::vector<int64_t> BinaryTree::postOrderIterative(Node *root) {
std::stack<Node *>
stack; ///< is used to find and traverse the child nodes.
std::vector<int64_t> result; ///< List of values, sorted in post-order.
stack.push(root);
while (!stack.empty()) {
result.push_back(stack.top()->data);
Node *current = stack.top();
stack.pop();
if (current->left) {
stack.push(current->left);
}
if (current->right) {
stack.push(current->right);
}
}
reverse(result.begin(), result.end());
return result;
}
/**
* @brief inOrderIterative() function that will perform the inorder traversal
* iteratively, and return the result array that contain the inorder traversal
* of a tree.
* @param root head/root node of a tree
* @return result that is containing the inorder traversal of a tree
*/
std::vector<int64_t> BinaryTree::inOrderIterative(Node *root) {
std::stack<Node *>
stack; ///< is used to find and traverse the child nodes.
std::vector<int64_t> result; ///< List of values, sorted in in-order.
Node *current = root;
while (!stack.empty() || current) {
while (current) {
stack.push(current);
current = current->left;
}
current = stack.top();
stack.pop();
result.push_back(current->data);
current = current->right;
}
return result;
}
void deleteAll(Node *root) {
if (root) {
std::stack<Node *> stack;
stack.push(root);
while (!stack.empty()) {
const Node *current = stack.top();
stack.pop();
if (current->right) {
stack.push(current->right);
}
if (current->left) {
stack.push(current->left);
}
delete current;
}
}
}
} // namespace iterative_tree_traversals
} // namespace others
/**
* @brief Test the computed preorder with the actual preorder.
* @param binaryTree instance of the BinaryTree class
* @param root head/root node of a tree
*/
static void test1(others::iterative_tree_traversals::BinaryTree binaryTree,
others::iterative_tree_traversals::Node *root) {
std::vector<int64_t> actual_result{1, 2, 4, 5, 3};
std::vector<int64_t>
result; ///< result stores the preorder traversal of the binary tree
// Calling preOrderIterative() function by passing a root node,
// and storing the preorder traversal in result.
result = binaryTree.preOrderIterative(root);
// Self-testing the result using `assert`
for (int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]);
}
// Printing the result storing preorder.
std::cout << "\nPreOrder Traversal Is : " << std::endl;
for (auto i : result) {
std::cout << i << " ";
}
}
/**
* @brief Test the computed postorder with the actual postorder.
* @param binaryTree instance of BinaryTree class
* @param root head/root node of a tree
*/
static void test2(others::iterative_tree_traversals::BinaryTree binaryTree,
others::iterative_tree_traversals::Node *root) {
std::vector<int64_t> actual_result{4, 5, 2, 3, 1};
std::vector<int64_t>
result; ///< result stores the postorder traversal of the binary tree.
// Calling postOrderIterative() function by passing a root node,
// and storing the postorder traversal in result.
result = binaryTree.postOrderIterative(root);
// Self-testing the result using `assert`
for (int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]);
}
// Printing the result storing postorder.
std::cout << "\nPostOrder Traversal Is : " << std::endl;
for (auto i : result) {
std::cout << i << " ";
}
}
/**
* @brief Test the computed inorder with the actual inorder.
* @param binaryTree instance of BinaryTree class
* @param root head/root node of a tree
*/
static void test3(others::iterative_tree_traversals::BinaryTree binaryTree,
others::iterative_tree_traversals::Node *root) {
std::vector<int64_t> actual_result{4, 2, 5, 1, 3};
std::vector<int64_t>
result; ///< result stores the inorder traversal of the binary tree.
// Calling inOrderIterative() function by passing a root node,
// and storing the inorder traversal in result.
result = binaryTree.inOrderIterative(root);
// Self-testing the result using `assert`
for (int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]);
}
// Printing the result storing inorder.
std::cout << "\nInOrder Traversal Is : " << std::endl;
for (auto i : result) {
std::cout << i << " ";
}
}
/**
* @brief Test the computed preorder with the actual preorder on negative value.
* @param binaryTree instance of BinaryTree class
* @param root head/root node of a tree
*/
static void test4(others::iterative_tree_traversals::BinaryTree binaryTree,
others::iterative_tree_traversals::Node *root) {
std::vector<int64_t> actual_result{-1, -2, -4, -5, -3};
std::vector<int64_t>
result; ///< result stores the preorder traversal of the binary tree
// Calling preOrderIterative() function by passing a root node,
// and storing the preorder traversal in result.
result = binaryTree.preOrderIterative(root);
// Self-testing the result using `assert`
for (int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]);
}
// Printing the result storing preorder.
std::cout << "\nPreOrder Traversal Is : " << std::endl;
for (auto i : result) {
std::cout << i << " ";
}
}
/**
* @brief Test the computed postorder with the actual postorder on negative
* value.
* @param binaryTree instance of BinaryTree class
* @param root head/root node of a tree
*/
static void test5(others::iterative_tree_traversals::BinaryTree binaryTree,
others::iterative_tree_traversals::Node *root) {
std::vector<int64_t> actual_result{-4, -5, -2, -3, -1};
std::vector<int64_t>
result; ///< result stores the postorder traversal of the binary tree.
// Calling postOrderIterative() function by passing a root node,
// and storing the postorder traversal in result.
result = binaryTree.postOrderIterative(root);
// Self-testing the result using `assert`
for (int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]);
}
// Printing the result storing postorder.
std::cout << "\nPostOrder Traversal Is : " << std::endl;
for (auto i : result) {
std::cout << i << " ";
}
}
/**
* @brief Test the computed inorder with the actual inorder on negative value.
* @param binaryTree instance of BinaryTree class
* @param root head/root node of a tree
*/
static void test6(others::iterative_tree_traversals::BinaryTree binaryTree,
others::iterative_tree_traversals::Node *root) {
std::vector<int64_t> actual_result{-4, -2, -5, -1, -3};
std::vector<int64_t>
result; ///< result stores the inorder traversal of the binary tree.
// Calling inOrderIterative() function by passing a root node,
// and storing the inorder traversal in result.
result = binaryTree.inOrderIterative(root);
// Self-testing the result using `assert`
for (int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]);
}
// Printing the result storing inorder.
std::cout << "\nInOrder Traversal Is : " << std::endl;
for (auto i : result) {
std::cout << i << " ";
}
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
// Creating a tree with the following structure,
/*
1
/ \
2 3
/ \
4 5
*/
others::iterative_tree_traversals::BinaryTree
binaryTree; ///< instace of BinaryTree, used to access its members
///< functions.
others::iterative_tree_traversals::Node *root = binaryTree.createNewNode(1);
root->left = binaryTree.createNewNode(2);
root->right = binaryTree.createNewNode(3);
root->left->left = binaryTree.createNewNode(4);
root->left->right = binaryTree.createNewNode(5);
std::cout << "\n| Tests for positive data value |" << std::endl;
test1(binaryTree, root); // run preorder-iterative test
std::cout << "\nPre-order test Passed!" << std::endl;
test2(binaryTree, root); // run postorder-iterative test
std::cout << "\nPost-order test Passed!" << std::endl;
test3(binaryTree, root); // run inorder-iterative test
std::cout << "\nIn-order test Passed!" << std::endl;
// Modifying tree for negative values.
root->data = -1;
root->left->data = -2;
root->right->data = -3;
root->left->left->data = -4;
root->left->right->data = -5;
std::cout << "\n| Tests for negative data values |" << std::endl;
test4(binaryTree, root); // run preorder-iterative test on negative values
std::cout << "\nPre-order test on-negative value Passed!" << std::endl;
test5(binaryTree, root); // run postorder-iterative test on negative values
std::cout << "\nPost-order test on-negative value Passed!" << std::endl;
test6(binaryTree, root); // run inorder-iterative test on negative values
std::cout << "\nIn-order test on-negative value Passed!" << std::endl;
deleteAll(root);
return 0;
}