Data-Structure-expression-tree
Data-Structure-expression-tree
Expression Tree:-
then the operators at the next level and so on and at the last operator at
the root node is applied and there by the expression is evaluated.
- +
b c d
a
1. Read the prefix expression, the first element becomes the root.
2. Now scan the infix expression till you get an element found in
step1. Place all the elements left of this element to the left of it and
other element to right of it.
3. Repeat steps 1 and 2 till all the elements from infix expression gets
placed in tree.
LNCT GROUP OF COLLEGES
4. stop
1. Read the postfix expression, the last element becomes the root.
2. Now scan the infix expression till you get an element found in
step1. Place all the elements left of this element to the left of it and
other element to right of it.
3. Repeat steps 1 and 2 till all the elements from infix expression get
placed in tree.
4. Stop
4. If all the elements from postfix expression are not read then go
to step 1.
LNCT GROUP OF COLLEGES
6. Stop
Sol. From postfix we get root i.e. / ,hence (5*3^2) is left subtree and
(3+(7+3)/10) is right subtree. Now again find root from postfix for
right You get + , now 3 is left and is (7+3)/10 is right, then 3 is left
child.
10 is root for right subtree. Now in left subtree + is root and 7is left
child and 3 is right child.
Similarly for left subtree * is root and 5 is left subtree and 3^2 is
right subtree.
Now again from right subtree root is ^, 3 is left child and 2 is right
child.
LNCT GROUP OF COLLEGES
* +
5 ^ 3
/
3 2
+ 10
2
0
2
7 3