Balanced BST
balanced-bst
Data Structures
Balanced BST (AVL / Red-Black)
Insert the keys 1, 2, 3, 4, 5 into a plain BST in that order and you do not get a tree at all; you get a right-leaning chain with `O(n)` search. The fix is to detect imbalance after every insert or delete and rotate the offending subtree back into shape, which is exactly what a **Balanced BST** does. AVL and Red-Black trees are the two most influential designs in this family. This lesson covers the balance factor (height of left subtree minus height of right subtree), AVL's strict `|bf| <= 1` invariant enforced by the four rotation cases (LL, RR, LR, RL), and Red-Black trees' five color and structural invariants enforced by recoloring plus rotations. You will trace insertions through both schemes, see why Red-Black trees do fewer rotations on average and so are preferred by C++ `std::map` and Java `TreeMap`, and analyze the height bound (`<= 2 log n`) that gives both their worst-case `O(log n)` guarantee. In **Binary Search Tree (BST)**, you implemented search, insert, and delete that respected the ordering invariant but said nothing about shape; the resulting `O(n)` worst case is precisely what self-balancing solves by maintaining a height invariant in addition to the ordering invariant. Next, **B-Trees and B+ Trees** generalize the same balanced-tree idea to nodes that hold many keys at once, an essential design choice for disk-resident data.
Not Started
0%
Practice Problems
Convert Sorted Array to Binary Search Tree
Given a sorted array, convert it into a height-balanced binary search tree using divide and conquer.
Community
Red-Black vs AVL: The Trade-off Nobody Explains
Why every standard library shipped red-black even though AVL is theoretically prettier: rotation counts on delete, color flips that beat pointer surgery, and the workloads where the difference actually shows up.
Trees and Binary Search Trees
What trees buy you over arrays and hash tables, why a textbook BST is almost never enough, and the balanced variants that actually run in production.
