Skip to content

Commit 0b756dc

Browse files
Add files via upload
1 parent 741cb7b commit 0b756dc

File tree

2 files changed

+240
-0
lines changed

2 files changed

+240
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Expression Tree
2+
================
3+
4+
5+
Data Structures/Concepts Used:
6+
==============================
7+
Stacks, Binary Search Tree, Evaluating an expression Tree, Recursion
8+
9+
10+
Description:
11+
============
12+
This program takes input from the console in the form of an infix expression,
13+
and then converts it into binary tree format.
14+
The Tree is then evaluated.
15+
16+
The algorithm for converting to an exp. tree is as follows:
17+
While the input stack is not empty...
18+
If it is operand, make it into a node, add it to output string.
19+
If it is Closing parenthesis, make into node, push it on stack.
20+
If it is an operator, then
21+
If stack is empty, make node and push operator on stack.
22+
If the top of stack is closing parenthesis, make node and push operator on stack.
23+
If it has same or higher priority than the top of stack, make node and push operator on stack.
24+
Else pop the operator from the stack, perform attachOperator and add it to treenodes. repeat
25+
If it is a opening parenthesis, pop operators from stack and perform attachOperator
26+
until a closing parenthesis is encountered. Pop and discard the closing parenthesis.
27+
If there is no more input, unstack the remaining operators and perform attachOperator
28+
29+
Evaluating the Expression tree is done recursively
30+
31+
32+
Output:
33+
=======
34+
35+
36+
```
37+
Enter Expression : 2+6*6+90+(67*56)/60
38+
39+
PreOrder Traverse (Parent, Left, Right) Postfix Expression : + * 5 6 / 6 0
40+
PostOrder Traverse (Left, Right, Parent) Prefix Expression : 5 6 * 6 0 / +
41+
```
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
typedef struct TreeNode
4+
{
5+
char info;
6+
TreeNode *left, *right;
7+
} TreeNode;
8+
typedef struct TreeNode *ExpTree;
9+
ExpTree Create_Node(int info);
10+
ExpTree constructTree(ExpTree T, char postfix[]);
11+
int getPriority(char ch);
12+
void Convert_In_To_Post(string infix, char postfix[]);
13+
14+
void show_node(ExpTree T);
15+
void preorder(ExpTree T);
16+
void postorder(ExpTree T);
17+
void display(ExpTree tree, char postfix[]);
18+
19+
void display(ExpTree tree, char postfix[])
20+
{
21+
22+
cout << "\n PreOrder Traverse (Parent, Left, Right) Postfix Expression : ";
23+
preorder(tree);
24+
25+
cout << "\n PostOrder Traverse (Left, Right, Parent) Prefix Expression : ";
26+
postorder(tree);
27+
}
28+
29+
bool isDigit(char ch)
30+
{
31+
if (ch > 47 && ch < 57)
32+
return true;
33+
return false;
34+
}
35+
bool isOperator(char ch)
36+
{
37+
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '^')
38+
return true;
39+
return false;
40+
}
41+
ExpTree Create_Node(int info)
42+
{
43+
TreeNode *temp;
44+
temp = (TreeNode *)malloc(sizeof(TreeNode));
45+
if (temp == NULL)
46+
{
47+
cout << "Out of space!\n";
48+
return (temp);
49+
}
50+
temp->left = NULL;
51+
temp->right = NULL;
52+
temp->info = info;
53+
return temp;
54+
};
55+
56+
ExpTree constructTree(ExpTree tree, char postfix[])
57+
{
58+
int i = 0;
59+
stack<TreeNode *> st;
60+
TreeNode *temp_tree1;
61+
TreeNode *temp_tree2;
62+
while (postfix[i] != '\0')
63+
{
64+
if (!(postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/' || postfix[i] == '%' || postfix[i] == '^'))
65+
{
66+
tree = Create_Node(postfix[i]);
67+
st.push(tree);
68+
}
69+
else
70+
{
71+
tree = Create_Node(postfix[i]);
72+
temp_tree1 = st.top();
73+
st.pop();
74+
temp_tree2 = st.top();
75+
st.pop();
76+
tree->right = temp_tree1;
77+
tree->left = temp_tree2;
78+
st.push(tree);
79+
}
80+
i++;
81+
}
82+
return tree;
83+
}
84+
85+
void show_node(ExpTree T)
86+
{
87+
cout << T->info << " ";
88+
}
89+
void preorder(ExpTree T)
90+
{
91+
if (T != NULL)
92+
{
93+
show_node(T);
94+
preorder(T->left);
95+
preorder(T->right);
96+
}
97+
}
98+
void postorder(ExpTree T)
99+
{
100+
if (T != NULL)
101+
{
102+
postorder(T->left);
103+
postorder(T->right);
104+
show_node(T);
105+
}
106+
}
107+
108+
int getPriority(char ch)
109+
{
110+
switch (ch)
111+
{
112+
case '^':
113+
return 4;
114+
case '%':
115+
return 3;
116+
case '/':
117+
case '*':
118+
return 2;
119+
case '+':
120+
case '-':
121+
return 1;
122+
default:
123+
return 0;
124+
}
125+
}
126+
void Convert_In_To_Post(string infix, char postfix[])
127+
{
128+
unsigned int counter1 = 0;
129+
stack<char> st;
130+
int postCount = 0;
131+
char element;
132+
while (counter1 < infix.length())
133+
{
134+
element = infix[counter1];
135+
if (element == '(')
136+
{
137+
st.push(element);
138+
counter1++;
139+
continue;
140+
}
141+
if (element == ')')
142+
{
143+
while (!st.empty() && st.top() != '(')
144+
{
145+
postfix[postCount++] = st.top();
146+
st.pop();
147+
}
148+
if (!st.empty())
149+
{
150+
st.pop();
151+
}
152+
counter1++;
153+
continue;
154+
}
155+
156+
if (getPriority(element) == 0)
157+
{
158+
postfix[postCount++] = element;
159+
}
160+
else
161+
{
162+
if (st.empty())
163+
{
164+
st.push(element);
165+
}
166+
else
167+
{
168+
while (!st.empty() && st.top() != '(' &&
169+
getPriority(element) <= getPriority(st.top()))
170+
{
171+
postfix[postCount++] = st.top();
172+
st.pop();
173+
}
174+
st.push(element);
175+
}
176+
}
177+
counter1++;
178+
}
179+
180+
while (!st.empty())
181+
{
182+
postfix[postCount++] = st.top();
183+
st.pop();
184+
}
185+
postfix[postCount] = '\0';
186+
}
187+
188+
int main()
189+
{
190+
cout << "\n Enter Expression : ";
191+
string infix;
192+
cin >> infix;
193+
char *postfix = (char *)malloc(sizeof(char) * infix.length());
194+
Convert_In_To_Post(infix, postfix);
195+
ExpTree tree1 = NULL;
196+
tree1 = constructTree(tree1, postfix);
197+
display(tree1, postfix);
198+
return 0;
199+
}

0 commit comments

Comments
 (0)