I have been trying to wrap my brain around how to write code for rotation of binary tree. I looked at http://en.wikipedia.org/wiki/Tree_rotation and enfuzzled.com I have been staring at this for 2 hours and have looked at it multiple times earlier. I still see problems in the wikipedia article and cannot understand the开发者_高级运维 other one completely e.g.
Both these lines mentioned in the wikipedia article cannot be true at once
Let P be Q's left child. Set P to be the new root.
Can anyone please help?Thanks
According to your comments to the question you are looking for the guidance for the rotation algorithm. Here is LEFT-ROTATE algorithm from an excellent book http://www.amazon.com/Introduction-Algorithms-Third-Thomas-Cormen/dp/0262033844.
"Let P be Q's left child. Set P to be the new root." Basically that's the description of the rotation to the right or clockwise:
Q P
/ => \
P Q
Here's the LEFT-ROTATE in my edition of the Corman book
精彩评论