-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExpressionTree.c
140 lines (138 loc) · 3.54 KB
/
ExpressionTree.c
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
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
struct exptree{
char data;
struct exptree *left,*right;
};
struct exptree *getnode(char);
struct exptree *createexptree(char[]);
int prcd(char);
void preorder(struct exptree *);
void inorder(struct exptree *);
void postorder(struct exptree *);
int main()
{
struct exptree *root=NULL;
char infix[20];
printf("ENTER THE INFIX EXP\n");
scanf("%s",infix);
root=createexptree(infix);
printf("\nINFIX\n");
inorder(root);
printf("\nPOSTFIX\n");
postorder(root);
printf("\nPREFIX\n");
preorder(root);
return 0;
}
struct exptree *getnode(char item)
{
struct exptree *newnode;
newnode=(struct exptree *)malloc(sizeof(struct exptree));
newnode->data=item;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
int prcd(char x)
{
switch(x)
{
case '^':return 3;break;
case '/':
case '*':return 2;break;
case '+':
case '-':return 1;break;
}
return 0;
}
struct exptree *createexptree(char infix[20])
{
int i =0;
char symbol;
struct exptree *temp,*t1,*t2,*t3;
struct exptree *operst[10],*treest[10];//operator stack and operand stack
int top1=-1,top2=-1;
for(i=0;infix[i]!='\0';i++)
{
symbol=infix[i];
if(isalnum(symbol))
{
temp=getnode(symbol);
treest[++top2] = temp;
}
else
{
temp=getnode(symbol);
if(symbol=='(')
{operst[++top1]=temp;}
else if(top1 ==-1 || prcd(operst[top1]->data)< prcd(symbol))
{operst[++top1]=temp;}
else if(symbol==')')
{
while((operst[top1]->data)!='(' && (top1!= -1) && top2!= -1 && prcd(operst[top1]->data) >= prcd(symbol))
{
t3=operst[top1--]; //take operator
t1=treest[top2--]; //take operand
t2=treest[top2--]; //another operand
t3->right=t1;
t3->left=t2;
treest[++top2]=t3; //push tree to stack
}
if(operst[top1]->data=='(')
{top1--;}
}
else
{
while((top1!=-1)&&(top2!=-1)&&prcd(operst[top1]->data)>=prcd(symbol))
{
t3=operst[top1--];
t1=treest[top2--];
t2=treest[top2--];
t3->right=t1;
t3->left=t2;
treest[++top2]=t3;
}
operst[++top1]=temp;
}
}
} //end of for
while(top1 !=-1)//if any character is left in stacks
{
t1=treest[top2--];
t2=treest[top2--];
temp=operst[top1--];
temp->right=t1;
temp->left=t2;
treest[++top2]=temp;
}
return treest[top2];
}
void inorder(struct exptree *root)//infix exp
{
if(root!=NULL)
{
inorder(root->left);
printf("%c",root->data);
inorder(root->right);
}
}
void preorder(struct exptree *root)//for prefix expression
{
if(root!=NULL)
{
printf("%c",root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct exptree *root)//for post fix expression
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%c",root->data);
}
}