What is a Red-Black tree?

Answer

A Red-Black tree is a self-balancing BST where each node has a color (red or black) and satisfies: (1) Root is black; (2) Leaves (NIL nodes) are black; (3) Red nodes have black children (no consecutive red nodes); (4) Every path from a node to its descendant NIL nodes has the same number of black nodes (black-height). These properties guarantee the tree height is at most 2log₂(n+1) — O(log n). Operations: O(log n) for search, insert, delete. Fewer rotations than AVL trees: at most 3 rotations per insert (0-2 for delete), vs AVL which may need O(log n) rotations. Rebalancing: insertion may require recoloring and at most 2 rotations; deletion may require recoloring and at most 3 rotations. Memory: 1 extra bit per node (color). Applications: Java TreeMap and TreeSet, C++ std::map, std::set, std::multimap. Linux kernel completely fair scheduler (CFS) uses red-black trees for task queuing. Linux memory management uses red-black trees for tracking virtual memory areas. Comparison: AVL = faster search (stricter balance), Red-Black = faster insert/delete (fewer rotations). Red-Black is preferred in standard library implementations due to better write performance.