-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepresentationOfPostfixExpression.cpp
More file actions
92 lines (86 loc) · 1.58 KB
/
Copy pathrepresentationOfPostfixExpression.cpp
File metadata and controls
92 lines (86 loc) · 1.58 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
/*
* File: representationOfPostfixExpressionIntoBST.cpp
* Author: Manish
*
* Created on 11 January, 2019, 10:50 PM
*/
#include <cstdlib>
#include <iostream>
#include <stack>
using namespace std;
/*
*
*/
class RepresentationInBST
{
private:
struct node
{
node * l;
char data;
node * r;
};
node * nn;
node * root;
public:
RepresentationInBST()
{
nn=NULL;
root=NULL;
}
void create();
void inOrderRec(node *);
void display();
};
void RepresentationInBST::create()
{
char arr[20]; //Array to store Postfix Expression
cout<<"Enter Postfix Expression : ";
cin>>arr;
stack <node *>s;
int i=0;
while(arr[i]!='\0')
{
nn=new node;
nn->data=arr[i];
nn->l=nn->r=NULL;
if(nn->data=='+' or nn->data=='-' or nn->data=='*' or nn->data=='/')
{
node * temp1=NULL;
node * temp2=NULL;
temp1=s.top();
s.pop();
temp2=s.top();
s.pop();
nn->r=temp1;
nn->l=temp2;
s.push(nn);
}
else
{
s.push(nn);
}
i++;
}
root=nn;
}
void RepresentationInBST::inOrderRec(node * temp)
{
if(temp!=NULL)
{
inOrderRec(temp->l);
cout<<temp->data<<" ";
inOrderRec(temp->r);
}
}
void RepresentationInBST::display()
{
inOrderRec(root);
cout<<endl;
}
int main(int argc, char** argv) {
RepresentationInBST t;
t.create();
t.display();
return 0;
}