algoritmauzmanı
Sign in
TREN
7.4Other Balanced Trees
Lesson 7.4.2

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.

What you will learn
  • 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
8 min

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

CaseConditionWhat is done
1The uncle is redThe parent and uncle become black and the grandparent red. The grandparent is treated as the new node and checking goes on above.
2The 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.
3The 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:

30R20R10B
30 inserted: 20 and 30 are red one after the other. The uncle (the left of 10) is NIL, that is black; 30 is on the outside: case 3.
10R30R20B
A left rotation at the grandparent 10; 20 and 10 swap colours. 20 is the black root with two red children.
15R10R30R20B
15 inserted: red, on the right of 10. The parent 10 is red and the uncle 30 is red too: case 1.
15R10B30B20B
10 and 30 became black and 20 red. 20 is the root, so at the end it was painted black again. No rotation.
13R15R10B30B20B
13 inserted: red, on the left of 15. The parent 15 is red and the uncle (the left of 10) is NIL. 15 is on the right of 10 and 13 on the left of 15: the inside, case 2.
15R13R10B30B20B
A right rotation at the parent 15: 13 moved up. Now 10, 13, 15 form a line: case 3.
10R15R13B30B20B
A left rotation at the grandparent 10; 13 becomes black and 10 red. All rules hold: 2 black nodes on every path.

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?

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?

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.