Hello,
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.
Converting an Expression to Binary Tree
Page 1 of 112 Replies  920 Views  Last Post: 22 September 2013  08:06 AM
Replies To: Converting an Expression to Binary Tree
#2
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  06:03 AM
What have you got/tried so far? What problems are you having?
#3
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  06:09 AM
Thanks for the reply, Ryan0121.
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.
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
No. This is not a 'do the work for you' site. We will help you with any problems you may be having after you have shown a good faith effort to solve the problem yourself, but will not just give you a solution. You will not learn anything from just being given a full answer.
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.
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
Sure, I will take that !
This is what I've got so far.
+
/ \
*\ h 3.25
%
/ \g

/ \
+ f
/ \
+ e
/\
a b
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
This applet is a useful tool to visualize expression trees:
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
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
Sure.
Thanks a lot, Ryan0121.
Thanks a lot, Ryan0121.
#8
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  06:58 AM
Is this correct, Ryano121 ?
+
/ \
* 3.25
\
/ h
%
/ \
g

/ \
+ f
/ \
* e
/ \
+ +
/ \ / \
a b c d
+
/ \
* 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
Not quite. At the moment you are subtracting 'f' when you really want to subtract the total of f/g*h.
What is the '%' in your tree?
What is the '%' in your tree?
#10
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  07:22 AM
Thats Div (/)
#11
Re: Converting an Expression to Binary Tree
Posted 22 September 2013  07:29 AM
That tree doesn't look well formed to me. Although it may just be the formatting putting it off.
Around the
%
/ \
g

/ \
bit.
Try adding more spaces and then put it in code tags to make it easier to read.
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
/ \
* 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
Yes you are subtracting 'f' instead of subtracting 'f/g*h' as a whole  that's when operator precedence comes into play as the division and multiplication operators takes precedence over the subtraction.
The end is really:
(...  ((f/g)*h)) + 3.25
not (((...  f)/g)*h) + 3.25
The end is really:
(...  ((f/g)*h)) + 3.25
not (((...  f)/g)*h) + 3.25
Page 1 of 1