# Binary Tree Rotation

Page 1 of 1

## 3 Replies - 8985 Views - Last Post: 24 March 2010 - 03:06 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=164061&amp;s=a9e6cba37deab3d051b404e92c39f979&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Djhar

Reputation: 2
• Posts: 35
• Joined: 24-January 10

# Binary Tree Rotation

Posted 24 March 2010 - 02:08 PM

Hey guys,

I am trying to start this problem dealing with rotating a tree to the left and printing it with proper indentation but I'm confused as to how this is going to work. The binary tree is show as follows:

```                    10
/  \
5   20
/ \
2   7
/
6

```

and the output needs to be:
```                    20

10
7
6
5
2

```

I can see that the 20 replaces the 10, the 10 replaces the 5, the 5 replaces the 2 and so on.. however in the textbook the output is exactly as I printed it- with no lines connecting the nodes. Any help is highly appreciated.

Is This A Good Question/Topic? 0

## Replies To: Binary Tree Rotation

### #2 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• Joined: 11-December 06

## Re: Binary Tree Rotation

Posted 24 March 2010 - 02:34 PM

I don't understand why there were no lines at the output tree, but nevertheless I assume you are having trouble with the algorithm.
So:
http://en.wikipedia....i/Tree_rotation

### #3 Djhar

Reputation: 2
• Posts: 35
• Joined: 24-January 10

## Re: Binary Tree Rotation

Posted 24 March 2010 - 02:40 PM

PennyBoki, on 24 March 2010 - 01:34 PM, said:

I don't understand why there were no lines at the output tree, but nevertheless I assume you are having trouble with the algorithm.
So:
http://en.wikipedia....i/Tree_rotation

The algorithm part of it I understand but I just don't know how to go about writing this code. And don't get me wrong, I'm not asking for it to be written for me. I just need a clear explanation as to how it goes from the original tree to the output tree as I shown above. Wikipedia doesn't always cut it =/

### #4 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• Joined: 11-December 06

## Re: Binary Tree Rotation

Posted 24 March 2010 - 03:06 PM

I see. Well do you know how to make a binary tree in code? Or are you having trouble with the screen formatting, i.e. the display of the tree?

Iterate through the tree, breadth first search so that you can print it in a proper way.
Then create the new tree based on the rotating algorithm that you already know, and then just as the previous print it.

This post has been edited by PennyBoki: 24 March 2010 - 03:08 PM