-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression_tree.cpp
More file actions
194 lines (163 loc) · 2.54 KB
/
expression_tree.cpp
File metadata and controls
194 lines (163 loc) · 2.54 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
#include <iostream>
#include <string.h>
using namespace std;
#define max 30
class node
{
public:
char data;
node *left, *right;
node()
{
left=right=NULL;
}
};
class expressiontree
{
public:
node *root;
expressiontree()
{
root=NULL;
}
void create(string str);
void nonrec_postorder(node *temp);
int priority(char ch);
};
class stack
{
int top;
node *stk[max];
public:
stack()
{
top=-1;
}
int empty()
{
if(top==-1)
return 1;
else
return 0;
}
void push(node *temp)
{
stk[++top]= temp;
}
node *pop()
{
return(stk[top--]);
}
node *Top()
{
return(stk[top]);
}
};
int expressiontree :: priority(char ch)
{
switch(ch)
{
case '+':
case '-':
return 0;
case '*':
case '/':
return 1;
case '^':
return 2;
}
}
void expressiontree :: create(string str)
{
stack operand_st, operator_st;
int i=0;
char ch;
while(str[i]!= '\0')
{
ch = str[i];
node *temp = new node();
temp->data = ch;
if(isalpha(ch))
{
operand_st.push(temp);
}
else
{
if(operator_st.empty())
operator_st.push(temp);
else
if(priority(ch)>priority(operator_st.Top()->data))
operator_st.push(temp);
else
{
while(!operator_st.empty() && priority(ch)<= operator_st.Top()->data)
{
node *operat = operator_st.pop();
operat->right= operand_st.pop();
operat->left = operand_st.pop();
operand_st.push(operat);
}
operator_st.push(temp);
}
}
i++;
}
while(!operator_st.empty())
{
node *operat = operator_st.pop();
operat->right= operand_st.pop();
operat->left = operand_st.pop();
operand_st.push(operat);
}
root= operand_st.pop();
}
void expressiontree :: nonrec_postorder(node *temp)
{
if(root==NULL)
cout<<"\n Empty";
else
{
stack s1, s2;
s1.push(temp);
while(!s1.empty())
{
node *temp= s1.pop();
s2.push(temp);
if(temp->left)
s1.push(temp->left);
if(temp->right)
s1.push(temp->right);
}
while(!s2.empty())
{
node *temp= s2.pop();
cout<<temp->data<<" ";
}
}
}
int main()
{
expressiontree e;
string str;
int ch;
do
{
cout<<"\n Enter Choice: ";
cout<<"\n\t1.Create expression tree \n\t2.Non recursive Postorder Traversal \n\t3.Exit";
cout<<"\n Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Expression : ";
cin >> str;
e.create(str);
break;
case 2:cout<<"\n Non Recursive Postorder Traversal: ";
e.nonrec_postorder(e.root);
break;
case 3: break;
}
}while(ch != 3);
return 0;
}