7.4Other Balanced Trees
Inserting into a red-black tree
The rules of a red-black tree can break after an insertion. Fixing them takes two tools: changing colours and rotating. Which one is used is decided by the new node's uncle. This lesson shows the three cases and an insertion trace of the kind exams ask for, step by step.
- Why a new node is inserted red
- The three cases, by the uncle's colour and the new node's position
- How to insert a sequence of values into a red-black tree step by step
1A new node enters red
Insertion starts like inserting into a binary search tree: you go down from the root and put the value into an empty place as a leaf. The new node is coloured red. A red node does not change the number of blacks on any path, so the fifth rule cannot break. The only rule that can break is the fourth: if the new node's parent is red too, two reds come one after the other. If the parent is black, the work is done.
If the parent is red, what to do is decided by the parent's sibling, the new node's uncle. The grandparent is always black, because the parent is red and the tree followed the rules before the insertion.
2Three cases
| Case | Condition | What is done |
|---|---|---|
| 1 | The uncle is red | The parent and uncle become black and the grandparent red. The grandparent is treated as the new node and checking goes on above. |
| 2 | The uncle is black (or NIL), the new node is on the inside (a triangle) | A rotation at the parent: the new node moves up and the case turns into case 3. |
| 3 | The uncle is black (or NIL), the new node is on the outside (a line) | A rotation at the grandparent; the parent and grandparent swap colours. Done. |
"On the inside" means: the parent is the grandparent's left child while the new node is the parent's right child, or the other way round. This is like the LR and RL cases of AVL; "on the outside" is like LL and RR. At the very end, if the root is red, it is painted black. Case 1 can climb up by recolouring, but cases 2 and 3 finish the insertion with at most two rotations.
3Example: 10, 20, 30, 15, 13
10 is inserted into the empty tree; it is the root, so it is painted black. 20 enters red on the right of 10; the parent is black, no problem. 30 enters red on the right of 20:
Had the same five values gone into an ordinary BST, 10, 20, 30 would have built a chain. An AVL tree would also rotate when 30 arrives; but when 15 was inserted, the red-black tree only changed colours. Fewer rotations are why this tree is chosen where insertions and deletions are frequent.
4Deletion
Deletion splits into more cases. Deleting a red leaf breaks no rule. Deleting a black node lowers the black count on its paths by one and breaks the fifth rule; this missing black is fixed by recolouring and at most three rotations, depending on the colour of the sibling and its children. Exams ask about red-black deletion less often; usually they want an insertion trace.
5Try it
A newly inserted red node has a red parent and a red uncle. What is done?
The answer is B. This is case 1: recolouring is enough. Because the grandparent is now red, it may clash with its own parent; checking goes on from the grandparent upwards.
Show the answer
The answer is B. This is case 1: recolouring is enough. Because the grandparent is now red, it may clash with its own parent; checking goes on from the grandparent upwards.
After inserting 1, 2, 3 into an empty red-black tree, what are the root and the colours?
The answer is B. When 3 arrives, 2 and 3 are red one after the other, the uncle is NIL and 3 is on the outside: case 3. The grandparent 1 is rotated left, 2 becomes the black root and 1 and 3 stay red.
Show the answer
The answer is B. When 3 arrives, 2 and 3 are red one after the other, the uncle is NIL and 3 is on the outside: case 3. The grandparent 1 is rotated left, 2 becomes the black root and 1 and 3 stay red.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.