# Converting an Expression to Binary Tree

Page 1 of 1

## 12 Replies - 737 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=6deedd6f8108c2b35a71b8ea6f873fcd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hshameer

• New D.I.C Head

Reputation: 0
• 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+ * (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: 1376
• Posts: 3,015
• 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

• New D.I.C Head

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

• D.I.C Lover

Reputation: 1376
• Posts: 3,015
• 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

• New D.I.C Head

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

### #6 Ryano121

• D.I.C Lover

Reputation: 1376
• Posts: 3,015
• 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

• New D.I.C Head

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

### #8 hshameer

• New D.I.C Head

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

### #9 Ryano121

• D.I.C Lover

Reputation: 1376
• Posts: 3,015
• 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

• New D.I.C Head

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

• D.I.C Lover

Reputation: 1376
• Posts: 3,015
• 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

• New D.I.C Head

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

### #13 Ryano121

• D.I.C Lover

Reputation: 1376
• Posts: 3,015
• 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }