-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_expression_tree.cpp
174 lines (164 loc) · 3.57 KB
/
05_expression_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
164
165
166
167
168
169
170
171
172
173
174
#include<bits/stdc++.h>
using namespace std;
struct Node
{
char data;
Node* left;
Node* right;
Node (char data)
{
this->data = data;
left = NULL;
right = NULL;
}
};
struct Stack
{
Node* data[100];
int top = -1;
void push(Node* element)
{
if (isFull() == true)
{
cout << "Overflow" << endl;
return;
}
top++;
data[top] = element;
}
void pop()
{
if (isEmpty() == true)
{
cout << "Underflow" << endl;
return;
}
top--;
}
Node* peek()
{
return data[top];
}
int size()
{
return (top + 1);
}
bool isEmpty()
{
if (top == -1) return true;
else return false;
}
bool isFull()
{
if (top == 99) return true;
else return false;
}
};
Node* constructTree(Stack &mystack, string expression)
{
for (int i = expression.length()-1; i >= 0; i--)
{
if ((expression[i] == '+') or (expression[i] == '-') or (expression[i] == '/') or (expression[i] == '*'))
{
Node* op1 = mystack.peek();
mystack.pop();
Node* op2 = mystack.peek();
mystack.pop();
Node* newnode = new Node(expression[i]);
newnode->left = op1;
newnode->right = op2;
mystack.push(newnode);
}
else
{
Node* newnode1 = new Node(expression[i]);
mystack.push(newnode1);
}
}
return mystack.peek();
}
void inorder_traversal(Node* root)
{
if (root == NULL)
{
return;
}
inorder_traversal(root->left);
cout << root->data;
inorder_traversal(root->right);
}
void postorder_traversal(Node* root)
{
if (root == NULL)
{
return;
}
postorder_traversal(root->left);
postorder_traversal(root->right);
cout << root->data;
}
void delete_root(Node *root)
{
if (root == NULL)
{
return;
}
delete_root(root->left);
delete_root(root->right);
delete (root);
}
void iterative_preorder(Node* root)
{
Stack mystack;
mystack.push(root);
while (mystack.isEmpty() != true)
{
Node* temp = mystack.peek();
mystack.pop();
cout << temp->data;
if (temp->right != NULL) mystack.push(temp->right);
if (temp->left != NULL) mystack.push(temp->left);
}
cout << endl;
}
void iterative_postorder(Node* root)
{
if (root == NULL) return;
Stack stack1;
Stack stack2;
stack1.push(root);
while (stack1.isEmpty() != true)
{
Node* temp = stack1.peek();
stack1.pop();
stack2.push(temp);
if (temp->left != NULL) stack1.push(temp->left);
if (temp->right != NULL) stack1.push(temp->right);
}
while (stack2.isEmpty() != true)
{
cout << stack2.peek()->data;
stack2.pop();
}
cout << endl;
}
int main()
{
Stack mystack;
string s;
cout << "Enter the prefix expression here-\n";
cin >> s;
Node* root = constructTree(mystack, s);
// cout << "Inorder traversal of the prefix tree is- ";
// inorder_traversal(root);
// cout << "\n";
cout << "Postorder traversal of the expression tree is- ";
postorder_traversal(root);
cout << "\n";
// cout << "Iterative preorder traversal of the expression tree is- ";
// iterative_preorder(root);
cout << "Iterative postorder traversal of the expression tree is- ";
iterative_postorder(root);
delete_root(root);
return 0;
}