Loading lessons...
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
- Every node is red or black.
- The root is black.
- Every leaf (the NULL pointers, drawn as NIL) is black.
- A red node cannot have a red child (no two reds in a row).
- 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):
| Case | Situation | Fix |
|---|---|---|
| 1 | Uncle is red | Recolour: parent and uncle black, grandparent red; continue the check from the grandparent |
| 2 | Uncle is black, new node is an "inner" grandchild (zig-zag) | Rotate at the parent to turn it into case 3 |
| 3 | Uncle 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
| Insert | What happens | Tree (B = black, R = red) |
|---|---|---|
| 10 | root → black | 10B |
| 20 | parent 10 is black-fine | 10B(−, 20R) |
| 30 | parent 20 red, uncle NIL (black), straight line → case 3: left-rotate at 10, recolour | 20B(10R, 30R) |
| 15 | parent 10 red, uncle 30 red → case 1: recolour 10, 30 black and 20 red; 20 is the root, so make it black again | 20B(10B(−, 15R), 30B) |
| 25 | parent 30 black-fine | 20B(10B(−, 15R), 30B(25R, −)) |
| 5 | parent 10 black-fine | 20B(10B(5R, 15R), 30B(25R, −)) |
AVL vs Red-Black
| AVL tree | Red-black tree | |
|---|---|---|
| Balance | Strict (BF is −1, 0 or +1) | Looser (longest path ≤ 2 × shortest) |
| Search | Slightly faster (shorter tree) | Slightly slower |
| Insert/delete | More rotations | At most 2 rotations per insert, 3 per delete |
| Best for | Lookup-heavy workloads | Frequent 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.
| Operation | Trie | Hash table | Balanced BST |
|---|---|---|---|
| Search a word of length L | O(L) | O(L) average | O(L log n) |
| All words with a prefix | Natural-walk the subtree | Not supported | Range search |
| Memory | High (26 pointers per node) | Moderate | Moderate |
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.