开发者

Code with explanation for binary tree rotation (left OR right)

开发者 https://www.devze.com 2023-02-03 01:06 出处:网络
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 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.

Code with explanation for binary tree rotation (left OR right)


"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

Code with explanation for binary tree rotation (left OR right)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号