Converting an Expression to Binary Tree
Page 1 of 112 Replies  754 Views  Last Post: 22 September 2013  08:06 AM
#1
Converting an Expression to Binary Tree
Posted 22 September 2013  05:30 AM
Can someone please help me in constructing a Binary Tree from the below given expression.
(a+ * (c+d) + e  f/g*h + 3.25
This is a question from one of the final MS exam papers. Your help is greatly appreciated !
Thanks.
Replies To: Converting an Expression to Binary Tree
#2
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  06:03 AM
#3
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  06:09 AM
Well, I am finding this expression little difficult to solve (am not sure if am getting the operands' priorities correct).
If possible, can you please solve this and let me know the final tree. This would be of great help.
Thanks.
#4
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  06:13 AM
If you don't know where to start I suggest going back and researching the topic more, perhaps solve some simpler expressions, before diving into this problem.
#5
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  06:36 AM
This is what I've got so far.
+
/ \
*\ h 3.25
%
/ \g

/ \
+ f
/ \
+ e
/\
a b
#6
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  06:41 AM
Expression Tree Builder
Just make sure you form the expression correctly beforehand. This will solve your operator priority/precedence issues as well.
You are missing the (c+d) part at the moment.
It's best if you start from a smaller expression and then build up. For example make a tree from
(a+b ) * (c+d) + e
#7
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  06:46 AM
Thanks a lot, Ryan0121.
#8
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  06:58 AM
+
/ \
* 3.25
\
/ h
%
/ \
g

/ \
+ f
/ \
* e
/ \
+ +
/ \ / \
a b c d
#9
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  07:06 AM
What is the '%' in your tree?
#10
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  07:22 AM
#11
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  07:29 AM
Around the
%
/ \
g

/ \
bit.
Try adding more spaces and then put it in code tags to make it easier to read.
#12
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  07:54 AM
/ \
* 3.25
\
h
/ (DIV)
/ \
 g
/ \
+ f
/ \
* e
/ \
+ +
/\ / \
a b c d
#13
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  08:06 AM
The end is really:
(...  ((f/g)*h)) + 3.25
not (((...  f)/g)*h) + 3.25
