- Asked 2/14
- 2070 Magh · 2 marks
- 2068 Magh · 2 marks
Red-Black tree and its purpose
Short answer
A Red-Black tree is a self-balancing BST where each node is red or black; properties include a black root, no consecutive red nodes, and equal black-height on all paths. It guarantees O(log n) search, insertion, and deletion by cheap recoloring and rotations, used in C++ STL map and Java TreeMap.
Define Red-Black tree and mention its purpose.
Definition
A Red-Black tree is a self-balancing binary search tree in which every node has an extra bit of information, its colour (red or black). The colours obey these rules:
- Every node is either red or black.
- The root is black.
- Every leaf (NIL / null link) is black.
- A red node cannot have a red child (no two red nodes in a row).
- Every path from a node to its descendant leaves has the same number of black nodes (equal black-height).
These rules guarantee that the longest path from the root is at most twice the shortest path, so the tree height is at most 2·log₂(n + 1).
13(B)
/ \
8(R) 17(R)
/ \ / \
1(B) 11(B) 15(B) 25(B)
Purpose
- An ordinary BST can become skewed and degrade to O(n) search. A Red-Black tree keeps the height O(log n), so search, insertion and deletion all take O(log n) time in the worst case.
- It restores balance after an update using only recolouring and at most 2 rotations on insertion (3 on deletion), so updates are cheaper than in a strict AVL tree.
- It is used where frequent insertions and deletions occur, e.g.
map/setin the C++ STL, JavaTreeMap, and process scheduling in the Linux kernel.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Earthing purpose and points to be earthed · Industrial Power Distribution and Illumination, chapter 2 · asked 2×
- Role of parse tree in CFG · Theory of Computation, chapter 3
- Three urns; both balls from Urn B · Probability and Statistics, chapter 1
- Resistor value from colour code · Basic Electronics Engineering, chapter 1