# Converting an Expression to Binary Tree

Page 1 of 1

## 12 Replies - 845 Views - Last Post: 22 September 2013 - 08:06 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=329705&amp;s=7c3969b9d5e0d2b5e76523bbd58db3a1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hshameer

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

# Converting an Expression to Binary Tree

Posted 22 September 2013 - 05:30 AM

Hello,

(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.
Is This A Good Question/Topic? 0

## Replies To: Converting an Expression to Binary Tree

### #2 Ryano121

• D.I.C Lover

Reputation: 1444
• Posts: 3,246
• 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?

### #3 hshameer

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

## 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 Ryano121

• D.I.C Lover

Reputation: 1444
• Posts: 3,246
• 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.

### #5 hshameer

Reputation: 0
• 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

### #6 Ryano121

• D.I.C Lover

Reputation: 1444
• Posts: 3,246
• 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

### #7 hshameer

Reputation: 0
• 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.

### #8 hshameer

Reputation: 0
• 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

### #9 Ryano121

• D.I.C Lover

Reputation: 1444
• Posts: 3,246
• 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?

### #10 hshameer

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

## Re: Converting an Expression to Binary Tree

Posted 22 September 2013 - 07:22 AM

Thats Div (/)

### #11 Ryano121

• D.I.C Lover

Reputation: 1444
• Posts: 3,246
• 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.

### #12 hshameer

Reputation: 0
• 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

### #13 Ryano121

• D.I.C Lover

Reputation: 1444
• Posts: 3,246
• 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