Red-black trees were first introduced by Guibas and SedgewickL. Guibas and R. Sedgewick. A dichromatic framework for balanced trees. In 19th Annual Symposium on Foundations of Computer Science, Ann Arbor, Michigan, 16–18 October 1978, Proceedings, pages 8–21. IEEE Computer Society, 1978.. Despite their high implementation complexity they are found in some of the most commonly used libraries and applications. Most algorithms and data structures textbooks discuss some variant of red-black trees.
AnderssonA. Andersson. Balanced search trees made simple. In F. K. H. A. Dehne, J.-R. Sack, N. Santoro, and S. Whitesides, editors, Algorithms and Data Structures, Third Workshop, WADS ’93, Montre ́al, Canada, August 11–13, 1993, Proceedings, volume 709 of Lecture Notes in Computer Science, pages 60–71. Springer, 1993. describes a left-leaning version of balanced trees that is similar to red-black trees but has the additional constraint that any node has at most one red child. This implies that these trees simulate 2-3 trees rather than 2-4 trees. They are significantly simpler, though, than the RedBlackTree structure presented in this chapter.
SedgewickR. Sedgewick. Left-leaning red-black trees, September 2008. URL: http://www.cs.princeton.edu/ ̃rs/talks/LLRB/LLRB.pdf [cited 2011-07-21]. describes two versions of left-leaning red-black trees. These use recursion along with a simulation of top-down splitting and merging in 2-4 trees. The combination of these two techniques makes for particularly short and elegant code.
A related, and older, data structure is the AVL treeG. Adelson-Velskii and E. Landis. An algorithm for the organization of information. Soviet Mathematics Doklady, 3(1259-1262):4, 1962.. AVL trees are height-balanced: At each node u, the height of the subtree rooted at u.left and the subtree rooted at u.right differ by at most one. It follows immediately that, if F(h) is the minimum number of leaves in a tree of height h, then F(h) obeys the Fibonacci recurrence
F(h)=F(h−1)+F(h−2)
with base cases F(0)=1 and F(1)=1. This means F(h) is approximate φh/5
, where φ=(1+5)/2≈1.61803399 is the golden ratio. (More precisely, ∣φh/5−F(h)∣≤1/2.) Arguing as in the proof of lemmaA 2-4 tree with n leaves has height at most log n., this implies
h≤logφn≈1.440420088logn
so AVL trees have smaller height than red-black trees. The height balancing can be maintained during add(x) and remove(x) operations by walking back up the path to the root and performing a rebalancing operation at each node u where the height of u’s left and right subtrees differ by two.
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy