We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Rotation

"Rotations" are what actually keep a red-black tree balanced. Every time one branch of the tree starts to get too long, we will "rotate" those branches to keep the tree shallow. A shallow tree is a healthy (fast) tree!

  • A properly-ordered tree pre-rotation remains a properly-ordered tree post-rotation
  • Rotations are O(1) operations
  • When rotating left:
    • The "pivot" node's initial parent becomes its left child
    • The "pivot" node's old left child becomes its initial parent's new right child

Here's the pointer-update order for a left rotation:

Assignment

Now that we can add users to our new Red Black Tree, we need to add the rotation functionality that will keep it balanced and running fast!

Use the exact same variables as specified in the instructions. For example, pivot_parent and pivot.parent are not interchangeable as they hold state that changes throughout the algorithm's steps.