forked from imnurav/Hactoberfest2021-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreorder_Traversal.cpp
125 lines (100 loc) · 3.1 KB
/
Preorder_Traversal.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
/*Traversal in Binary Tree
Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways.
One way of traversing is Depth First Search:
1
/ \
2 3
/ \
4 5
(a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
Preorder Traversal :
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Uses of Preorder
Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get prefix expression on of an expression tree
*/
#include <bits/stdc++.h>
using namespace std;
// Data structure to store a binary tree node
struct Node
{
int data;
Node *left, *right;
Node(int data)
{
this->data = data;
this->left = this->right = nullptr;
}
};
// Recursive function to perform preorder traversal on the tree
void preorderRecursive(Node* root)
{
// if the current node is empty
if (root == nullptr) {
return;
}
// Display the data part of the root (or current node)
cout << root->data << " ";
// Traverse the left subtree
preorderRecursive(root->left);
// Traverse the right subtree
preorderRecursive(root->right);
}
//Iterative implementation
void preorderIterative(Node* root)
{
// return if the tree is empty
if (root == nullptr)
return;
// create an empty stack and push the root node
stack<Node*> stack;
stack.push(root);
// loop till stack is empty
while (!stack.empty())
{
// pop a node from the stack and print it
Node* curr = stack.top();
stack.pop();
cout << curr->data << " ";
// push the right child of the popped node into the stack
if (curr->right) {
stack.push(curr->right);
}
// push the left child of the popped node into the stack
if (curr->left) {
stack.push(curr->left);
}
// the right child must be pushed first so that the left child
// is processed first (LIFO order)
}
}
int main()
{
/* Construct the following tree
1
/ \
/ \
2 3
/ / \
/ / \
4 5 6
/ \
/ \
7 8
*/
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->right->left = new Node(5);
root->right->right = new Node(6);
root->right->left->left = new Node(7);
root->right->left->right = new Node(8);
preorderRecursive(root);
preorderIterative(root);
return 0;
}