-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExpression Tree
More file actions
141 lines (106 loc) · 2.64 KB
/
Expression Tree
File metadata and controls
141 lines (106 loc) · 2.64 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
struct node
{
int data;
struct node* llink;
struct node* rlink;
};
typedef struct node* NODE;
NODE getnode()
{
NODE x;
x = malloc(sizeof(struct node));
return x;
}
NODE create(char postfix[])
{
// This function creates an expression tree.
// This tree is then passed to the evaluate function.
int i,k;
NODE temp;
char symbol;
NODE stack[100];
// Assing each character in the postfix expression to symbol till it reaches the last(null) character.
for(i=k=0; (symbol = postfix[i]) != '\0'; i++)
{
temp = getnode();
// Initialise
temp->data = symbol;
temp->llink = temp->rlink = NULL;
// If symbol encountered is a alphabet or a character, keep moving.
if(isalnum(symbol))
{
stack[k++] = temp;
}
// If you encounter an operator(+,-,*,/), add the right node and left node to that operator.
// We use a stack to keep track of the symbol.
else
{
temp->rlink = stack[--k];
temp->llink = stack[--k];
stack[k++] = temp;
}
}
// k gets incremented in the last iteration of the loop. Decrement it back.
return stack[--k];
}
float evaluate(NODE root)
{
float num;
// left node (operator) right node is the order of evaluation.
switch(root->data)
{
case '+' : return evaluate(root->llink) + evaluate(root->rlink);
case '-' : return evaluate(root->llink) - evaluate(root->rlink);
case '*' : return evaluate(root->llink) * evaluate(root->rlink);
case '/' : return evaluate(root->llink) / evaluate(root->rlink);
// Both ^ and $ are used for power.
case'^':
case'$':
return pow(evaluate(root->llink),evaluate(root->rlink));
default:
// If alphabets (x,y,z,a,b...) are entered.
// Ask the user the value of those variables.
// and continue normal execution by returning.
if(isalpha(root->data))
{
printf("%c = \n", root->data);
scanf("%f",&num);
return num;
}
// with ASCII you can just add and subtract 48(ASCII value of 0)
// to go between integer and character values.
// Just a smart way to convert int to char.
else
return root->data - '0';
}
}
void display(NODE root,int i)
{
int j;
if(root==NULL)return;
display(root->rlink,i+1);
for(j=0;j<i;j++)
printf(" ");
printf("%c ",root->data);
printf("\n");
display(root->llink,i+1);
}
int main()
{
char postfix[100], infix[100];
float res = 0;
NODE root;
root = NULL;
printf("Enter a valid postfix expression: \n");
scanf("%s",postfix);
root = create(postfix);
res = evaluate(root);
printf("The created tree is : \n");
display(root, 1);
printf("Result : %f \n",res);
return 0;
}