Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 422
user560871 Avatar asked Jan 04 '11 19:01

user560871


People also ask

How do you left rotate a binary tree?

Tree rotation In a binary search tree, a left rotation is the movement of a node, X, down to the left. This rotation assumes that X has a right child (or subtree). X's right child, R, becomes X's parent node and R's left child becomes X's new right child.

What is rotation in binary search tree?

In discrete mathematics, tree rotation is an operation on a binary tree that changes the structure without interfering with the order of the elements. A tree rotation moves one node up in the tree and one node down.

What determines the direction of rotation of a binary search tree?

The direction of a rotation depends on the side which the tree nodes are shifted upon whilst others say that it depends on which child takes the root’s place. This is a C++ program to perform Left Rotation on a Binary Search Tree.

What is binary tree in C?

A tree in which each node (parent) has at most two-child nodes (left and right) is called binary tree. The top most node is called the root node. In a binary tree a node contains the data and the pointer (address) of the left and right child node.

How to print boundary nodes of binary tree anti-clockwise?

Given a binary tree, print boundary nodes of the binary tree Anti-Clockwise starting from the root. The boundary includes left boundary, leaves, and right boundary in order without duplicate nodes. (The values of the nodes may still be duplicates.) The left boundary is defined as the path from the root to the left-most node.

What is the timetime complexity of binary tree?

Time Complexity: O (n) where n is the number of nodes in binary tree. Right Boundary – Go Right Right until no Right. Dont Include Leaf nodes. (as it leads to duplication) Left Boundary – Go Left Left until no Left. Dont Include Leaf nodes. (as it leads to duplication) Leaf Boundary – Do inorder, if leaf node add to the List.


1 Answers

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.

alt text

like image 179
Schultz9999 Avatar answered Nov 15 '22 05:11

Schultz9999