

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 10
click for more info
Not enough gems
Cost: 6 gems
1: Unbalanced Trees
incomplete
2: Red-Black Tree
incomplete
3: Rules
incomplete
4: Rotation
incomplete
5: Fix Insert
incomplete
6: Quiz
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Rotations are only useful if we can use them. When new nodes are inserted into the tree, they can break the red-black properties. We'll fix that by rotating the tree as new nodes are inserted, ensuring the tree remains balanced.
When we're done here, we will have a fully functional (albeit insert-only) red-black tree. As you can see if you look at the bottom of the test suite, we'll be inserting numbers into our tree in order. A normal binary tree would break down into a single unruly branch:
> 7
> 6
> 5
> 4
> 3
> 2
> 1
But our red-black tree will remain balanced:
> 7
> 6
> 5
> 4
> 3
> 2
> 1
In our implementation, we'll perform a "normal" insert, and then call a fix_insert method that will recolor and rotate the tree as necessary. This will ensure that the red-black properties are maintained.