Red-black Tree: Difference between revisions
Line 25: | Line 25: | ||
'''Claim''': every red-black tree with n nodes has height ≤ 2 log(n+1) (O(log n)). | '''Claim''': every red-black tree with n nodes has height ≤ 2 log(n+1) (O(log n)). | ||
'''Proof'''. | '''Proof'''. We will prove that a red-black tree must look like a perfectly balanced tree with at most factor 2 inflation. |
Revision as of 18:05, 13 October 2021
External
- https://www.coursera.org/learn/algorithms-graphs-data-structures/lecture/8acpe/red-black-trees
- https://www.youtube.com/watch?v=scfDOof9pww
- https://en.wikipedia.org/wiki/Red%E2%80%93black_tree
Internal
Overview
Red-black trees were invented by Bayer (1972) and Guibas, Sedgewick (1978). A red-black tree is a type of binary search tree that self-balances on insertion and deletion, thus maintaining its height to a minimum, which allows for efficient operations. Almost all binary search tree operations have a running time bounded by the tree height, and in this case the tree height stays constant at log n, yielding O(log n) operations.
Red-black Tree Invariants
- Each node is either red or black.
- The root is always black.
- There are never 2 red nodes in a row (a red node has only black children).
- Every path taken from root to a NULL pointer - unsuccessful search - has the same number of black nodes (red makes a node "invisible" to the invariant 4).
These invariants are in addition to the fact that the red-black tree is a binary search tree, so it has the Binary Search Tree Property.
Red-black Trees Maintain Small Heights
If all these invariants are satisfied at all times, the height of the tree is going to be small. For example, a chain with three nodes cannot be a red-black tree.
Claim: every red-black tree with n nodes has height ≤ 2 log(n+1) (O(log n)).
Proof. We will prove that a red-black tree must look like a perfectly balanced tree with at most factor 2 inflation.