TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
iterative_tree_traversals.cpp
Go to the documentation of this file.
1
38#include <algorithm>
39#include <cassert>
40#include <iostream>
41#include <stack>
42#include <vector>
43
48namespace others {
58struct Node {
59 int64_t data = 0;
60 struct Node *left{};
61 struct Node *right{};
62};
63
68 public:
70 int64_t);
71 std::vector<int64_t> preOrderIterative(
72 Node *);
74 std::vector<int64_t> postOrderIterative(
75 Node *);
77 std::vector<int64_t> inOrderIterative(
78 Node *);
80};
81
89 Node *node = new Node();
90 node->data = data;
91 node->left = node->right = nullptr;
92 return node;
93}
94
102std::vector<int64_t> BinaryTree::preOrderIterative(Node *root) {
103 std::stack<Node *>
104 stack;
105 std::vector<int64_t> result;
106
107 stack.push(root);
108
109 while (!stack.empty()) {
110 result.push_back(stack.top()->data);
111 Node *current = stack.top();
112 stack.pop();
113
114 if (current->right) {
115 stack.push(current->right);
116 }
117 if (current->left) {
118 stack.push(current->left);
119 }
120 }
121
122 return result;
123}
124
132std::vector<int64_t> BinaryTree::postOrderIterative(Node *root) {
133 std::stack<Node *>
134 stack;
135 std::vector<int64_t> result;
136
137 stack.push(root);
138
139 while (!stack.empty()) {
140 result.push_back(stack.top()->data);
141 Node *current = stack.top();
142 stack.pop();
143
144 if (current->left) {
145 stack.push(current->left);
146 }
147 if (current->right) {
148 stack.push(current->right);
149 }
150 }
151
152 reverse(result.begin(), result.end());
153
154 return result;
155}
156
164std::vector<int64_t> BinaryTree::inOrderIterative(Node *root) {
165 std::stack<Node *>
166 stack;
167 std::vector<int64_t> result;
168
169 Node *current = root;
170
171 while (!stack.empty() || current) {
172 while (current) {
173 stack.push(current);
174 current = current->left;
175 }
176 current = stack.top();
177 stack.pop();
178 result.push_back(current->data);
179 current = current->right;
180 }
181 return result;
182}
183void deleteAll(Node *root) {
184 if (root) {
185 std::stack<Node *> stack;
186 stack.push(root);
187
188 while (!stack.empty()) {
189 const Node *current = stack.top();
190 stack.pop();
191
192 if (current->right) {
193 stack.push(current->right);
194 }
195 if (current->left) {
196 stack.push(current->left);
197 }
198 delete current;
199 }
200 }
201}
202} // namespace iterative_tree_traversals
203} // namespace others
204
212 std::vector<int64_t> actual_result{1, 2, 4, 5, 3};
213 std::vector<int64_t>
214 result;
215
216 // Calling preOrderIterative() function by passing a root node,
217 // and storing the preorder traversal in result.
218 result = binaryTree.preOrderIterative(root);
219
220 // Self-testing the result using `assert`
221 for (int i = 0; i < result.size(); i++) {
222 assert(actual_result[i] == result[i]);
223 }
224
225 // Printing the result storing preorder.
226 std::cout << "\nPreOrder Traversal Is : " << std::endl;
227 for (auto i : result) {
228 std::cout << i << " ";
229 }
230}
231
239 std::vector<int64_t> actual_result{4, 5, 2, 3, 1};
240 std::vector<int64_t>
241 result;
242
243 // Calling postOrderIterative() function by passing a root node,
244 // and storing the postorder traversal in result.
245 result = binaryTree.postOrderIterative(root);
246
247 // Self-testing the result using `assert`
248 for (int i = 0; i < result.size(); i++) {
249 assert(actual_result[i] == result[i]);
250 }
251
252 // Printing the result storing postorder.
253 std::cout << "\nPostOrder Traversal Is : " << std::endl;
254 for (auto i : result) {
255 std::cout << i << " ";
256 }
257}
258
266 std::vector<int64_t> actual_result{4, 2, 5, 1, 3};
267 std::vector<int64_t>
268 result;
269
270 // Calling inOrderIterative() function by passing a root node,
271 // and storing the inorder traversal in result.
272 result = binaryTree.inOrderIterative(root);
273
274 // Self-testing the result using `assert`
275 for (int i = 0; i < result.size(); i++) {
276 assert(actual_result[i] == result[i]);
277 }
278
279 // Printing the result storing inorder.
280 std::cout << "\nInOrder Traversal Is : " << std::endl;
281 for (auto i : result) {
282 std::cout << i << " ";
283 }
284}
285
293 std::vector<int64_t> actual_result{-1, -2, -4, -5, -3};
294 std::vector<int64_t>
295 result;
296
297 // Calling preOrderIterative() function by passing a root node,
298 // and storing the preorder traversal in result.
299 result = binaryTree.preOrderIterative(root);
300
301 // Self-testing the result using `assert`
302 for (int i = 0; i < result.size(); i++) {
303 assert(actual_result[i] == result[i]);
304 }
305
306 // Printing the result storing preorder.
307 std::cout << "\nPreOrder Traversal Is : " << std::endl;
308 for (auto i : result) {
309 std::cout << i << " ";
310 }
311}
312
321 std::vector<int64_t> actual_result{-4, -5, -2, -3, -1};
322 std::vector<int64_t>
323 result;
324
325 // Calling postOrderIterative() function by passing a root node,
326 // and storing the postorder traversal in result.
327 result = binaryTree.postOrderIterative(root);
328
329 // Self-testing the result using `assert`
330 for (int i = 0; i < result.size(); i++) {
331 assert(actual_result[i] == result[i]);
332 }
333
334 // Printing the result storing postorder.
335 std::cout << "\nPostOrder Traversal Is : " << std::endl;
336 for (auto i : result) {
337 std::cout << i << " ";
338 }
339}
340
348 std::vector<int64_t> actual_result{-4, -2, -5, -1, -3};
349 std::vector<int64_t>
350 result;
351
352 // Calling inOrderIterative() function by passing a root node,
353 // and storing the inorder traversal in result.
354 result = binaryTree.inOrderIterative(root);
355
356 // Self-testing the result using `assert`
357 for (int i = 0; i < result.size(); i++) {
358 assert(actual_result[i] == result[i]);
359 }
360
361 // Printing the result storing inorder.
362 std::cout << "\nInOrder Traversal Is : " << std::endl;
363 for (auto i : result) {
364 std::cout << i << " ";
365 }
366}
367
372int main() {
373 // Creating a tree with the following structure,
374 /*
375 1
376 / \
377 2 3
378 / \
379 4 5
380 */
381
383 binaryTree;
386 root->left = binaryTree.createNewNode(2);
387 root->right = binaryTree.createNewNode(3);
388 root->left->left = binaryTree.createNewNode(4);
389 root->left->right = binaryTree.createNewNode(5);
390
391 std::cout << "\n| Tests for positive data value |" << std::endl;
392 test1(binaryTree, root); // run preorder-iterative test
393 std::cout << "\nPre-order test Passed!" << std::endl;
394
395 test2(binaryTree, root); // run postorder-iterative test
396 std::cout << "\nPost-order test Passed!" << std::endl;
397
398 test3(binaryTree, root); // run inorder-iterative test
399 std::cout << "\nIn-order test Passed!" << std::endl;
400
401 // Modifying tree for negative values.
402 root->data = -1;
403 root->left->data = -2;
404 root->right->data = -3;
405 root->left->left->data = -4;
406 root->left->right->data = -5;
407
408 std::cout << "\n| Tests for negative data values |" << std::endl;
409 test4(binaryTree, root); // run preorder-iterative test on negative values
410 std::cout << "\nPre-order test on-negative value Passed!" << std::endl;
411
412 test5(binaryTree, root); // run postorder-iterative test on negative values
413 std::cout << "\nPost-order test on-negative value Passed!" << std::endl;
414
415 test6(binaryTree, root); // run inorder-iterative test on negative values
416 std::cout << "\nIn-order test on-negative value Passed!" << std::endl;
417
418 deleteAll(root);
419
420 return 0;
421}
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13
defines the functions associated with the binary tree
std::vector< int64_t > inOrderIterative(Node *)
inOrderIterative() function that will perform the inorder traversal iteratively, and return the resul...
Node * createNewNode(int64_t)
function that will create new node for insertion.
std::vector< int64_t > postOrderIterative(Node *)
postOrderIterative() function that will perform the postorder traversal iteratively,...
std::vector< int64_t > preOrderIterative(Node *)
preOrderIterative() function that will perform the preorder traversal iteratively,...
for std::invalid_argument
Definition stack.hpp:19
void pop()
Definition stack.hpp:62
void push(const value_type &item)
Definition stack.hpp:47
value_type top() const
Definition stack.hpp:56
static void test2()
Self-implementations, 2nd test.
static void test1()
Self-test implementations, 1st test.
static void test3()
int data[MAX]
test data
static void test4(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
Test the computed preorder with the actual preorder on negative value.
static void test5(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
Test the computed postorder with the actual postorder on negative value.
int main()
Main function.
static void test6(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
Test the computed inorder with the actual inorder on negative value.
Functions for the Traversal of the Tree algorithm.
for vector
char stack[MAX]
defines the structure of a node of the tree
struct Node * left
struct pointer to left subtree.
struct Node * right
struct pointer to right subtree.