MCS-208 · Unit 7

Advanced Trees

Section 8 of 9

Block 3 · Unit 7Advanced Trees20 min · Section 8 of 9

7.8Red-Black Trees

Covers IGNOU MCS-208 §7.6, 7.6.1, 7.6.2, 7.6.3

A red-black tree is a binary search tree with one extra bit per node, its colour. A few simple rules about colours keep the tree roughly balanced: its height is at most 2log⁡2(n+1)2 \log_2 (n + 1). Red-black trees are used in many standard libraries, for example C++ std::map, Java TreeMap and the Linux kernel’s scheduler.

Properties

The black-height bh(x)bh(x) is the number of black nodes on any path from xx (not counting xx) down to a NIL leaf. Properties 3 and 4 together give the height bound: a path can at most alternate red and black, so it is at most twice as long as the all-black path.

Rotations

Red-black trees use the same left and right rotations as AVL trees (see the AVL section above): a left rotation at xx makes xx‘s right child yy the root of the subtree, xx becomes yy‘s left child, and yy‘s old left subtree becomes xx‘s right subtree. A right rotation is the mirror image. Both keep the inorder order and take O(1)O(1) time.

Insertion

  1. Insert the new node zz as in a BST and colour it red (this cannot break property 4).
  2. If zz‘s parent is black, nothing else is needed. If zz is the root, colour it black.
  3. If zz‘s parent is red (property 3 is broken), look at zz‘s uncle yy (the parent’s sibling). With zz‘s parent a left child:
    • Case 1 — uncle red: colour the parent and uncle black and the grandparent red, then repeat the check with zz = grandparent.
    • Case 2 — uncle black, zz a right child: set zz = parent and left-rotate at zz; this turns the bend into a line (case 3).
    • Case 3 — uncle black, zz a left child: colour the parent black and the grandparent red, then right-rotate at the grandparent.
  4. The mirror cases apply when zz‘s parent is a right child. Finally, colour the root black.

Case 1 may repeat up the tree, but cases 2 and 3 end the fix-up, so an insertion needs at most two rotations.

Deletion (outline)

Deletion first removes the node as in a BST. If the node actually removed (the deleted node or its successor) was black, some paths have lost a black node. The fix-up gives the node xx that took its place an “extra black” and pushes it up the tree while xx is not the root and xx is black, using xx‘s sibling ww:

  • Case 1: ww red → recolour and rotate to get a black sibling (then case 2, 3 or 4).
  • Case 2: ww black with two black children → colour ww red and move the extra black up to the parent.
  • Case 3: ww black, ww‘s far child black and near child red → rotate at ww to reach case 4.
  • Case 4: ww black with a red far child → recolour and rotate at the parent; the extra black disappears and the loop ends.

Finally xx is coloured black. At most three rotations are needed.

Key takeaways

  • Properties: red/black, root black, NIL leaves black, red nodes have black children, equal black counts.
  • Height ≤ 2log⁡2(n+1)2 \log_2 (n + 1), so operations are O(log⁡n)O(\log n) worst case.
  • Insert red; fix up: uncle red → recolour and move up; uncle black → rotate (case 2 then 3).
  • Deletion fix-up handles an “extra black” with four sibling cases; at most three rotations.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which colour is a newly inserted node given in a red-black tree?

Show answer

Answer: B. Red

A red node does not change any path’s black count, so property 4 stays true; only property 3 (or 2) may need repair.

Question 2 · multiple choice

A new red node’s parent is red and its uncle is red. What does the fix-up do?

Show answer

Answer: C. Colour parent and uncle black, grandparent red, and continue from the grandparent

This is case 1: pure recolouring, which may push the problem up two levels.

Question 3 · multiple choice

What is the maximum height of a red-black tree with nn internal nodes?

Show answer

Answer: D. 2log⁡2(n+1)2 \log_2 (n + 1)

At least half the nodes on any root-to-leaf path are black, and all paths have the same number of black nodes. 1.44log⁡2n1.44 \log_2 n is the (tighter) AVL bound.

Question 4 · multiple choice

Which of these breaks a red-black property?

Show answer

Answer: B. A red node with a red child

Two reds in a row break property 3. NIL children count as black, so a red leaf node is fine.

Question 5 · multiple choice

In the example, how many rotations did the insertion of 4 need?

Show answer

Answer: C. 2

Case 1 only recolours; case 2 rotates left at 2 and case 3 rotates right at 11: two rotations, the maximum for one insertion.

Question 6 · short answer

Compare AVL trees and red-black trees (at least four points).

Show worked answer

(1) Balance rule: AVL keeps subtree heights within 1 at every node; red-black trees use colour rules (no red-red, equal black counts). (2) Height: AVL ≤ about 1.44log⁡2n1.44 \log_2 n; red-black ≤ 2log⁡2(n+1)2 \log_2(n + 1), so AVL trees are more tightly balanced and slightly faster to search. (3) Updates: AVL insertion needs at most one (single or double) rotation, but deletion may need O(log⁡n)O(\log n) rotations; red-black insertion needs at most 2 rotations and deletion at most 3, plus recolouring. (4) Extra storage: AVL stores a balance factor or height; red-black stores one colour bit. (5) Use: AVL for search-heavy workloads; red-black for update-heavy ones and in standard libraries (C++ std::map, Java TreeMap).

↑ ↓ move↵ openTab switch type