-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostfix to infix binary trees.cpp
106 lines (85 loc) · 1.56 KB
/
postfix to infix binary trees.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
#include<iostream>
#include<cstring>
#include<string>
#define TRUE 1
#define FALSE 0
const int m=10;
using namespace std;
bool isOperator(char c)
{
if (c == '+' || c == '-' ||
c == '*' || c == '/' ||
c == '^')
return true;
return false;
}
struct et
{
char value;
et *left;
et *right;
}*root;
et* a[m];
int t=-1;
void push(et* n){
if(t==m-1){
cout<<"stack fll"<<endl;
return;
}
t++;
a[t]=n;
}
et* pop(){
if(t==-1){
cout<<"EMPTY "<<endl;
return 0;
}
et* d=a[t];
t--;
return d;
}
et* insert ( char num[] )
{
et* temp;
et* t2;
et* t;
et* t1;
for(int i=0;i<strlen(num);i++){
if (!isOperator(num[i]))
{
temp = new et;
temp->left = temp->right = NULL;
temp->value = num[i];
push(temp);
}
else
{
temp = new et;
temp->left = temp->right = NULL;
temp->value =num[i];
root=temp;
t1= pop();
t2 = pop();
temp->right = t1;
temp->left = t2;
push(temp);
}}
temp=pop();
return temp;
}
void inorder ( et *sr )
{
if ( sr != NULL )
{
inorder ( sr -> left) ;
cout << sr -> value << "\t" ;
inorder ( sr -> right ) ;
}
}
int main()
{
char postfix[] = "abcdef--ghi^*j+-/+*";
et* r=insert(postfix);
inorder(r);
return 0;
}