Skip to main content

Section 32.2 Rotations

There are different strategies for tracking and maintaining balance in a binary tree. But all of the strategies rely on a fundamental operation called a rotation. A rotation is a local restructuring of a small part of the tree that changes the heights of the left and right subtrees of a node. By performing rotations during insertions and deletions, we can keep the overall tree balanced.
A rotation can be named in multiple ways, but the most common is to name it based on the direction that the subtree is rotated. A right rotation is one where a node is rotated down to the right, and its left child is rotated up to take its place. A left rotation is the opposite: a node is rotated down to the left, and its right child is rotated up to take its place.
A right rotation is performed on the node with value 5. The left child (3) becomes the new root of the subtree, and 5 becomes the right child of 3.πŸ”—
Figure 32.2.1. Right Rotation of 5. On the left is the initial state. On the right is the state after the rotation.

Note 32.2.1.

The other common way to name rotations is based on the child that is rotated up. In that naming scheme, the above rotation would be a rotation of the 3 node (because it is the left child that is rotated up). Both naming schemes are widely used, so be sure to understand which one is being used in any particular context. We will use the direction-based naming scheme in this book.
This rotation improves the balance of the tree by reducing the height of the left subtree and increasing the height of the right subtree of the rotated node. In the simple example above, the heights of the left and right subtrees of the root node went from 2 and 0 to 1 and 1.
Of course, not every tree is as simple as that one. Often times there will be children below the nodes being rotated. The rotation operation must preserve the binary search tree property, so we need to be careful about where those children end up after the rotation.
When rotating right, the two nodes involved in the rotation are the pivot (the node being rotated down) and its left child (the node being rotated up). The left child may have both left and right children of its own. The pivot may have a right child. We will refer to the left child’s left child as \(A\text{,}\) its right child as \(B\text{,}\) and the pivot’s right child as \(C\text{.}\)
A right rotation is performed on the node with value 5. The left child (3) becomes the new root of the subtree, and 5 becomes the right child of 3. The left child of 3 is A. The right child of 3 is B. The right child of 5 is C.πŸ”—
Figure 32.2.2. Right Rotation of 5 with children of the pivot and left child labeled.
\(A\) and \(C\) can stay where they are in the rotation. \(B\) however presents a problem. The old left child (3 in our diagram) will need to use its right pointer to point at the pivot (the 5 node). So we need to do something with \(B\text{.}\)
Note that whatever value \(B\) has, it must be less than the pivot or it would not have ended up in the left subtree. Also, it must be greater than the left child or it would not have ended up as the right child of the left child. So \(B\) needs to remain between the left child and the pivot after the rotation.
Fortunately, as the pivot rotates down, it no longer needs to use its left pointer. The node it was pointing at is now its parent. So we can have the pivot use its left pointer to point at \(B\text{.}\) This keeps \(B\) in the correct position in the tree and preserves the binary search tree property.
A right rotation is performed on the node with value 5. The left child (3) becomes the new root of the subtree, and 5 becomes the right child of 3. The right child of 3 (4) becomes the left child of 5.πŸ”—
Figure 32.2.3. The pivot (5) adopts its middle grandchild (4) as a new child.

Insight 32.2.2.

During a rotation, the original parent adopts its β€œmiddle grandchild” as a new child.
Left rotations are simply a mirror image of right rotations. In a left rotation, the pivot is rotated down to the left, and its right child is rotated up to take its place. The pivot adopts its middle grandchild (the left child of the right child) as a new right child.

Activity 32.2.1. Rotations.

The animation is set to perform a right rotation on the node with value 5. Use the > button to step through the rotation process.
Once it is done, try doing a left rotation of 3 (click on the 3 node and then press Rotate Left).

Instructions.

When the > button is highlighted, an animation is prepared to run. Use the Step button to step through the animation one step at a time.
When no animation is in progress, you can use the data controls to start a new animation.
You can click and drag on the animation area to pan around. Use the Ctrl + mouse wheel (or touchpad scroll gesture) while over the animation area to zoom in and out.
  • Core Controls.
    • << : Skip to the beginning of the animation.
    • < : Step backwards one step in the animation.
    • > : Step forward one step in the animation.
    • >> : Skip to the end of the animation.
    • Auto Step Speed Set to anything other than off to automatically step through the animation at the selected speed.
    • Zoom Set the zoom level. You can also use Ctrl + Mouse Wheel to zoom in and out.
  • Data Controls.
    • Value Use this area to enter a value when inserting, finding, deleting, etc...
The code to do a rotation like this is relatively simple. Given a pointer to the pivot node, we can perform the rotation as follows:
Listing 32.2.4.
Node* rotateRight(Node* pivot) {
    Node* leftChild = pivot->left;
    Node* middleGrandchild = leftChild->right;  // may be null, doesn't matter
    pivot->left = middleGrandchild;             // Pivot adopts middle grandchild
    leftChild->right = pivot;                   // Left child adopts pivot
    return leftChild;                           // New root of the subtree
}
Note that we return the new root of the subtree after the rotation. We would need to update the parent node’s pointer to point to this new root.
...
// left child of "parent" need to rotate right
parent->left = rotateRight(parent->left);
...

Checkpoint 32.2.1.

In this tree, if we rotate K to the left, what changes will be made?
H is the root node. It has left child E and right child K. E has a left child C and a right child G. C has a left child B and a right child D. B has a left child A. G has a right child F. K has a left child J and a right child L. J has a left child I.πŸ”—
  • H’s right child will become L
  • L’s left child will become K
  • K’s right child will become null
  • L’s left child will become J
  • L’s right child will become K
  • H’s right child will become J

Checkpoint 32.2.2.

In this tree, if we rotate E to the right, what changes will be made?
H is the root node. It has left child E and right child K. E has a left child C and a right child G. C has a left child B and a right child D. B has a left child A. G has a right child F. K has a left child J and a right child L. J has a left child I.πŸ”—
  • H’s left child will become C
  • E’s left child will become D
  • C’s right child will become E
  • D’s right child will become E
  • H’s left child will become G
You have attempted of activities on this page.