Header Ads

For given expression eg. a-b*c-d/e+f construct inorder sequence and traverse it using postorder traversal.

Problem statement:-
For given expression eg. a-b*c-d/e+f construct inorder sequence and traverse it using postorder traversal(non recursive).

Code:-


  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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//============================================================================
// Name        : ExpressionTree.cpp
// Author      : VNKDJ5
// Version     :
// Copyright   : github.com/vnkdj5
// Description : Hello World in C++, Ansi-style
//============================================================================
#include<string>
#include <iostream>
using namespace std;
class node;
class tree;
const int MAX=50;


//===========Stack ==========================

class Stack
{
 int top;
 node *info[MAX];

public:
 Stack()
{
  top=-1;
}
 void push(node *cnode)
 {
  // cout<<"here\n";
  //  if(!isFull())
  //  {
  //   cout<<"in push\n";
  top=top+1;
  info[top]=cnode;

  //  else
  //  {
  //   cout<<"\nStack Overflow\n";
  //  }
 }
 node *Top()
 {
  return info[top];
 }
 node * pop()
 {
  if(!empty())
  {
   return info[top--];
  }
  return NULL;
 }
 bool empty()
 {
  if(top==-1)
   return true;
  else
   return false;
 }
 bool isFull()
 {
  if(top==MAX-1)
   return true;
  else
   return false;
 }
};
//==================================================


class node
{
 node *left,*right;
 char data;
public:
 node()
{
  left=right=NULL;

}
 node(char ch)
 {
  left=right=NULL;
  data=ch;
 }
 friend class tree;
};

class tree
{
 node *root;

public:
 tree()
{
  root=NULL;
}
 void create(string str);
 void inorder_rec(node *rnode);
 void preorder_rec(node *rnode);
 void postorder_rec(node *rnode);
 void inorderNonRec();
 void preorderNonRec();

 void postorderNon();
 void postorder()
 {
  postorder_rec(root);
 }
 void inorder()
 {
  inorder_rec(root);
 }
 void preorder()
 {
  preorder_rec(root);
 }
 int priority(char ch);
};
int tree::priority(char ch)
{
 switch(ch)
 {
 case '+':
 case '-':
  return 0;
  break;
 case '*':
 case '/':
  return 1;
  break;
 case '^':
  return 2;
  break;

 }
 return -1;
}
void tree::postorderNon()
{
 Stack s1;
 node *ptr=root;
 int arr[MAX],i=-1,flag;
 LOOP:while(ptr!=NULL)
 {
  s1.push(ptr);
  arr[++i]=0;
  if(ptr->right!=NULL)
  {
   s1.push(ptr->right);
   arr[++i]=1;
  }
  ptr=ptr->left;
 }

 ptr=s1.pop();
 flag=arr[i--];
 while(flag==0&& i>=0)
 {
  cout<<" "<<ptr->data;
  ptr=s1.pop();
  flag=arr[i--];
 }
 if(flag==1 && i>=0)
 {
  goto LOOP;
 }
 cout<<" "<<ptr->data;
}

void tree::preorderNonRec()
{
 Stack s1;
 node *ptr=root;
 while(ptr!=NULL)
 {
  cout<<" "<<ptr->data;
  if(ptr->right!=NULL)
   s1.push(ptr->right);
  if(ptr->left!=NULL)
   ptr=ptr->left;
  else
   ptr=s1.pop();
 }
}
void tree::inorderNonRec()
{
 node *ptr=root;
 Stack s1;
 X:while(ptr!=NULL)
 {
  s1.push(ptr);
  ptr=ptr->left;
 }
 ptr=s1.pop();

 while(ptr!=NULL)
 {
  cout<<" "<<ptr->data;
  if(ptr->right!=NULL)
  {
   ptr=ptr->right;
   goto X;
  }
  ptr=s1.pop();
 }
}
void tree::inorder_rec(node *rnode)
{
 if(rnode)
 {
  inorder_rec(rnode->left);
  cout<<" "<<rnode->data;
  inorder_rec(rnode->right);
 }
}
void tree::preorder_rec(node *rnode)
{
 if(rnode)
 {
  cout<<" "<<rnode->data;
  preorder_rec(rnode->left);
  preorder_rec(rnode->right);
 }
}
void tree::postorder_rec(node *rnode)
{
 if(rnode)
 {
  postorder_rec(rnode->left);
  postorder_rec(rnode->right);
  cout<<" "<<rnode->data;
 }
}
void tree::create(string str)
{
 Stack s1,s2;
 int i=0;
 char ch;
 while(str[i]!='\0')
 {
  //cout<<"in create()";
  ch=str[i];
  if(isalpha(ch)) //s1 operand stack and s2===operator
  {
   node *temp=new node(ch);
   s1.push(temp);
  }
  else //operator
  {
   //cout<<"in operator block()";
   if(s2.empty())
   {
    node *temp=new node(ch);
    s2.push(temp);
   }
   else if(priority(ch)>priority(s2.Top()->data))
   {
    node *temp=new node(ch);
    s2.push(temp);
   }
   else
   {
    while(!s2.empty()&&priority(ch)<=priority(s2.Top()->data) )
    {
     node *op=s2.pop();
     node *rchild=s1.pop();
     node *lchild=s1.pop();

     op->right=rchild;
     op->left=lchild;

     s1.push(op);
    }
    s2.push(new node(ch)); //push operand at last
   }
  }

  i++;
  //cout<<" i "<<i;
 }
 while(!s2.empty()) //pop() until operator stack is not empty
 {
  node *op=s2.pop();
  node *rchild=s1.pop();
  node *lchild=s1.pop();

  op->right=rchild;
  op->left=lchild;
  s1.push(op);
 }

 //set the root element to s1->top()
 root=s1.pop();

}

int main() {
 cout << "" << endl; // prints
 tree t1;
 string exp="a-b*c-d/e+f";
 cout<<"\nOriginal Expression: "<<exp;

 t1.create(exp);
 cout<<"\nInorder Traversal Recursive: ";
 t1.inorder();
 cout<<"\nInorder Non-Recursive: ";
 t1.inorderNonRec();
 cout<<"\nPreorder Traversal Recursive: ";
 t1.preorder();
 cout<<"\nPreorder traversal Non-Recursive: ";
 t1.preorderNonRec();
 cout<<"\nPostorder Traversal recursive: ";
 t1.postorder();
 cout<<"\nPostorder Non-Recursive: ";
 t1.postorderNon();
 return 0;
}

Output:-

No comments:

Powered by Blogger.