Skip to main content

Section 32.3 Double Rotations

A rotation can help to improve the balance of a tree when one subtree is taller than the other. However, there are cases where a single rotation is not enough to restore balance. This happens when the taller subtree itself is unbalanced in the opposite direction.
To see why this is the case, consider a right rotation on the following tree. At the start, it is unbalanced because the left subtree has a height of 2 while the right subtree has a height of 0. After the right rotation, the tree is still unbalanced because the new left subtree has a height of 0 while the new right subtree has a height of 2.
A right rotation is performed on the node with value 5. The 5 node has a left child (3). It has a right child (4). The left child (3) becomes the new root of the subtree, and 5 becomes the right child of 3 and 4 becomes the left child of 5.πŸ”—
Figure 32.3.1. Right Rotation of 5 does not restore balance.
The key to identifying this situation is to notice that the path to the deeper subtree is a β€œzig-zag” pattern. In our example, we went left from 5 to 3, and then right from 3 to 4. When we have this zig-zag pattern, a single rotation will not be enough to restore balance. Instead, we need to perform two rotations: one on the child node to straighten out the zig-zag, and then one on the original pivot node to complete the balancing.
In this example, we first perform a left rotation on the 3 node. This straightens out the zig-zag by making 4 the new child of 5. Then, we perform a right rotation on the 5 node. This completes the balancing by making 4 the new root of the subtree, with 3 as its left child and 5 as its right child.
The root node (5) has a left child (3). That has a right child (4). First a left rotation is performed on 3, making 4 the new left child of 5 and 3 the left child of 4. Then a right rotation is performed on 5, making 4 the new root of the subtree, with 3 as its left child and 5 as its right child.πŸ”—
Figure 32.3.2. A left rotation on 3 followed by a right rotation on 5.
We will refer to this sequence of two rotations as a double rotation. The first rotation is performed on the child of the pivot node, and the second rotation is performed on the pivot node itself. The order of the rotations depends on the direction of the zig-zag pattern. In our example, we had a left-right zig-zag, so we performed a left rotation followed by a right rotation. If we had a right-left zig-zag, we would perform a right rotation followed by a left rotation.

Activity 32.3.1. Double Rotations.

The tree in this animation below is out of balance. The left side of the root is deeper than the right side. But a simple right rotation of the root won’t fix it. (Try if you like and then reset the animation.)
The path from the root where we want to perform the rotation is a zig-zag pattern (first it goes left and then it goes right). Fix it by first doing a left rotation of 10 and then a right rotation of 18. Click on a node (or type its value into the text field) to select it, then press Rotate Left or Rotate Right to start the rotation and then > to step through the animation.

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...
We don’t always have to rotate at the root of the tree. We generally want to perform rotations at the lowest unbalanced node in the tree. This minimizes the amount of restructuring we have to do and helps to keep the tree as balanced as possible.

Activity 32.3.2. Fixing a Tree.

The tree below can’t be perfectly balanced. But it can be made more balanced at the 10 node. Perform a double rotation to improve the balance at that subtree.

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...
Hint 1.
You should end up with 12 at the root of the subtree where 10 was.
Hint 2.
This is a zig-zag pattern. You need to start by straightening out the chain containing 10, 12, 15.

Checkpoint 32.3.1.

How could we balance the subtree rooted at A?
H is the root node. It has left child D and right child N. D has a left child A and a right child G. A has a right child C. C has a left child B. G has a left child E. E has a right child F. N has a left child J and a right child Q. J has a left child I.πŸ”—
  • Rotate C right, then A left
  • Rotate A right
  • Rotate C left, then A right
  • Rotate A left
  • Rotate A right, then C left
  • Rotate A left, then C right

Checkpoint 32.3.2.

How could we balance the subtree rooted at G?
H is the root node. It has left child D and right child N. D has a left child A and a right child G. A has a right child C. C has a left child B. G has a left child E. E has a right child F. N has a left child J and a right child Q. J has a left child I.πŸ”—
  • Rotate E left, then G right
  • Rotate G right
  • Rotate E right, then G left
  • Rotate G left
  • Rotate G right, then E left
  • Rotate G left, then E right

Checkpoint 32.3.3.

How could we move J to the root of the tree? (This would not make it more balanced.)
H is the root node. It has left child D and right child N. D has a left child A and a right child G. A has a right child C. C has a left child B. G has a left child E. E has a right child F. N has a left child J and a right child Q. J has a left child I.πŸ”—
  • Rotate N right, then H left
  • Rotate J left
  • Rotate J left, then N right
  • Rotate N left, then H left
You have attempted of activities on this page.