-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.cpp
More file actions
200 lines (172 loc) · 3.66 KB
/
Copy pathhello.cpp
File metadata and controls
200 lines (172 loc) · 3.66 KB
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
#include <iostream>
#include <stack>
#define MAX 100
using namespace std;
class node
{
public:
node *left;
node *right;
char data;
node(char key)
{
data = key;
left = NULL;
right = NULL;
}
};
class Stack
{
public:
node *arr[MAX];
int top;
Stack() // constructor to set default value of top
{
top = -1;
}
void push(node *new_node);
void pop();
node *gettop();
bool isempty();
};
void Stack::push(node *new_node)
{
if (top == (MAX - 1)) // if stack is full
{
cout << "Overflow!\n";
return;
}
arr[top++] = new_node;
}
void Stack::pop()
{
if (top == -1) // if stack is empty
{
cout << "Underflow!\n";
return;
}
top--;
}
node *Stack::gettop()
{
return arr[top];
}
bool Stack::isempty()
{
return (top == -1);
}
node *construct_tree(string exp)
{
// stack<node *> mystack;
Stack mystack;
int exp_length = exp.length();
for (int i = (exp_length - 1); i >= 0; i--)
{
if ((exp[i] == '+') || (exp[i] == '-') || (exp[i] == '*') || (exp[i] == '/'))
{
node *op1 = mystack.gettop();
mystack.pop();
node *op2 = mystack.gettop();
mystack.pop();
node *new_root = new node(exp[i]);
new_root->left = op1;
new_root->right = op2;
mystack.push(new_root);
}
else
{
node *new_node = new node(exp[i]);
mystack.push(new_node);
}
}
return mystack.gettop();
}
void display_postorder(node *root)
{
if (root == NULL)
{
return;
}
display_postorder(root->left);
display_postorder(root->right);
cout << root->data << " ";
}
void display_inorder(node *root)
{
if (root == NULL)
{
return;
}
display_inorder(root->left);
cout << root->data << " ";
display_inorder(root->right);
}
void delete_tree(node *root)
{
if (root == NULL)
{
return;
}
delete_tree(root->left);
delete_tree(root->right);
delete root;
}
void iter_postorder(node *root)
{
// stack<node *> stack_1;
// stack<node *> stack_2;
Stack stack_1;
Stack stack_2;
stack_1.push(root);
while (!stack_1.isempty())
{
node *temp_node = stack_1.gettop();
stack_1.pop();
stack_2.push(temp_node);
if (temp_node->left != NULL)
{
stack_1.push(temp_node->left);
}
if (temp_node->right != NULL)
{
stack_1.push(temp_node->right);
}
}
while (!stack_2.isempty())
{
cout << stack_2.gettop()->data << " ";
stack_2.pop();
}
}
int main()
{
string expression;
expression = "+--A*BC/DEF";
// cout << "Enter expression: ";
// cin >> expression;
node *test_root = construct_tree(expression);
cout << "\nPostorder expression: ";
display_postorder(test_root);
cout << "\nInorder expression: ";
display_inorder(test_root);
cout << "\nNon recursive postorder : ";
iter_postorder(test_root);
delete_tree(test_root);
cout << "\nTree deleted";
}
// int main()
// {
// string expression;
// expression = "+--A*BC/DEF";
// // cout << "Enter expression: ";
// // cin >> expression;
// node *test_root = construct_tree(expression);
// // cout << "\nPostorder expression: ";
// // display_postorder(test_root);
// // cout << "\nInorder expression: ";
// // display_inorder(test_root);
// cout << "\nNon recursive postorder : ";
// iter_postorder(test_root);
// delete_tree(test_root);
// cout << "\nTree deleted";
// }