Lesson 27 +30 XP

Red-Black Trees & Tries (Beyond Syllabus)

Red-Black Trees & Tries (Beyond Syllabus)

🧭 Beyond syllabus. These two structures appear in the practice-question slide of the CIC-209 notes but are not in the official syllabus. They are common in interviews and later courses (DBMS, compilers, competitive programming).

Part A-Red-Black Trees

A red-black tree is a BST in which every node is coloured red or black so that the tree stays roughly balanced. It is used inside C++ std::map, Java TreeMap and the Linux scheduler.

The Five Properties

  1. Every node is red or black.
  2. The root is black.
  3. Every leaf (the NULL pointers, drawn as NIL) is black.
  4. A red node cannot have a red child (no two reds in a row).
  5. Every path from a node down to its NIL leaves has the same number of black nodes (the black-height).

Together these guarantee that the longest root-to-leaf path is at most twice the shortest, so the height is at most 2 log2(n + 1) and search, insert and delete are all O(log n).

Insertion

Insert as in a BST and colour the new node red (this never breaks property 5). If its parent is also red, fix it by looking at the uncle (the parent's sibling):

CaseSituationFix
1Uncle is redRecolour: parent and uncle black, grandparent red; continue the check from the grandparent
2Uncle is black, new node is an "inner" grandchild (zig-zag)Rotate at the parent to turn it into case 3
3Uncle is black, new node is an "outer" grandchild (straight line)Rotate at the grandparent and swap the colours of parent and grandparent

Finally, always make the root black.

Worked Example: insert 10, 20, 30, 15, 25, 5

InsertWhat happensTree (B = black, R = red)
10root → black10B
20parent 10 is black-fine10B(−, 20R)
30parent 20 red, uncle NIL (black), straight line → case 3: left-rotate at 10, recolour20B(10R, 30R)
15parent 10 red, uncle 30 red → case 1: recolour 10, 30 black and 20 red; 20 is the root, so make it black again20B(10B(−, 15R), 30B)
25parent 30 black-fine20B(10B(−, 15R), 30B(25R, −))
5parent 10 black-fine20B(10B(5R, 15R), 30B(25R, −))

AVL vs Red-Black

AVL treeRed-black tree
BalanceStrict (BF is −1, 0 or +1)Looser (longest path ≤ 2 × shortest)
SearchSlightly faster (shorter tree)Slightly slower
Insert/deleteMore rotationsAt most 2 rotations per insert, 3 per delete
Best forLookup-heavy workloadsFrequent insertions and deletions

Part B-Tries (Prefix Trees)

A trie stores strings character by character. Each edge is a letter; a path from the root spells a prefix; a flag marks where a complete word ends.

Words: cat, car, cart, dog
            (root)
            /    \
           c      d
           |      |
           a      o
          / \     |
        t*   r*   g*
             |
             t*          (* = end of a word)

startsWith(prefix) is the same loop as search but returns 1 at the end without checking isEnd.

OperationTrieHash tableBalanced BST
Search a word of length LO(L)O(L) averageO(L log n)
All words with a prefixNatural-walk the subtreeNot supportedRange search
MemoryHigh (26 pointers per node)ModerateModerate

Applications: autocomplete and search suggestions, spell checkers, IP routing (longest-prefix match), word games, dictionary storage.

📝 Practice Questions

  • Q1. State the properties of a red-black tree and insert 10, 20, 30, 15, 25, 5 showing every recolouring and rotation.
  • Q2. Construct a trie for the words cat, car, cart, dog, do and write the search algorithm.
  • Q3. Compare AVL trees and red-black trees.