12 Replies - 524 Views - Last Post: 22 September 2013 - 08:06 AM Rate Topic: -----

#1 hshameer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-September 13

Converting an Expression to Binary Tree

Posted 22 September 2013 - 05:30 AM

Hello,

Can someone please help me in constructing a Binary Tree from the below given expression.

(a+B) * (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.
Is This A Good Question/Topic? 0
  • +

Replies To: Converting an Expression to Binary Tree

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

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?
Was This Post Helpful? 0
  • +
  • -

#3 hshameer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-September 13

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.
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

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.
Was This Post Helpful? 0
  • +
  • -

#5 hshameer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-September 13

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
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

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
Was This Post Helpful? 0
  • +
  • -

#7 hshameer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-September 13

Re: Converting an Expression to Binary Tree

Posted 22 September 2013 - 06:46 AM

Sure.

Thanks a lot, Ryan0121.
Was This Post Helpful? 0
  • +
  • -

#8 hshameer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-September 13

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
Was This Post Helpful? 0
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

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?
Was This Post Helpful? 0
  • +
  • -

#10 hshameer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-September 13

Re: Converting an Expression to Binary Tree

Posted 22 September 2013 - 07:22 AM

Thats Div (/)
Was This Post Helpful? 0
  • +
  • -

#11 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 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.
Was This Post Helpful? 0
  • +
  • -

#12 hshameer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-September 13

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
Was This Post Helpful? 0
  • +
  • -

#13 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1