ECE750-TXB Lecture 7: Red-Black Trees, Heaps, and Treaps Todd L. Veldhuizen [email protected]Red-Black Trees Heaps Treaps Bibliography ECE750-TXB Lecture 7: Red-Black Trees, Heaps, and Treaps Todd L. Veldhuizen [email protected]Electrical & Computer Engineering University of Waterloo Canada February 14, 2007 ECE750-TXB Lecture 7: Red-Black Trees, Heaps, and Treaps Todd L. Veldhuizen [email protected]Red-Black Trees Heaps Treaps Bibliography Binary Search Trees Recall that in a binary tree of height h the time required to find or insert an element is O (h). In the worst case h = n, the number of elements. To keep h ∈ O (log n) one needs a balancing strategy. Balancing strategies may be either: Randomized: e.g. a random insert order results in expected height of c log n with c ≈ 4.311. Deterministic (in the sense of not random). Today we will see an example of each: Red-black trees: deterministic balancing Treaps: randomized. Also demonstrate persistence and unique representation.
21
Embed
ECE750-TXB Lecture 7: Red-Black Trees, Heaps, and Treapsece750-ads/notes/lecture07.pdf · ECE750-TXB Lecture 7: Red-Black Trees, Heaps, and Treaps Todd L. Veldhuizen [email protected]
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
1. Base case: If x has height 0, then x is a leaf, andbh(x) = 0; the number of internal (non-leaf)descendents of x is 0 = 2bh(x) − 1.
2. Induction step: assume the hypothesis is true for height≤ h. Consider a node of height h + 1. From invariant(2), the children have black height either bh(x)− 1 (ifthe child is black) or bh(x) (if the child is red). Byinduction hypothesis, each child subtree has at least2bh(x)−1 − 1 internal nodes. The total number ofinternal nodes in the subtree rooted at x is therefore≥ (2bh(x)−1 − 1) + 1 + (2bh(x)−1 − 1) = 2bh(x) − 1.
TheoremA red-black tree with n internal nodes has height at most2 log2(n + 1).
Proof.Let h be the tree height. From invariant 1 (a red node musthave both children black), the black-height of the root mustbe ≥ h/2. Applying Lemma 1.1, the number of internalnodes n of the tree satisfies n ≥ 2h/2 − 1. Rearranging,h ≤ 2 log2(n + 1).
I Treaps are a randomized search tree that combineTRees and hEAPS.
I First, let’s look at heaps.I Consider determining the maximum element of a set.
I We could iterate through the array and keep track ofthe maximum element seen so far. Time taken: Θ(n).
I We could build a binary tree (e.g. red-black). We canobtain the maximum (minimum) element in O(h) timeby following rightmost (leftmost) branches. If tree isbalanced, requires O(n log n) time to build the tree, andO(log n) time to retrieve the maximum element.
I A heap is a highly efficient data structure formaintaining the maximum element of a set. It is arudimentary example of a dynamic algorithm/datastructure.
I A heap dynamically maintains the maximum element ina collection (or, dually, the minimum element). Abinary heap can:
I Obtain the maximum element in O(1) time;I Remove the maximum element in O(log n) time;I Insert new element in O(log n) time.
Heaps are a natural implementation of thePriorityQueue ADT.
I There are several flavours of heaps: binary heaps,binomial heaps, fibonacci heaps, pairing heaps. Themore sophisticated of these support merging (melding)two heaps.
I If priorities are chosen randomly, the tree is on averagebalanced, and insert, delete, search take O(log n) time
I Random priorities behave like a random insertion order:the structure of the treap is exactly that obtained byinserting the keys into a binary search tree indescending order of heap prioritity.
I If keys are unique (no duplicates), and priorities areunique, then the treap has the unique representationproperty
I Unique representation: each set is represented by aunique data structure [1, 13, 12]
I Most tree data structures do not have this property:depending on order of inserts, deletes, etc. the tree canhave different forms for the same set of keys.
I Recall there are Cn ∼ 4nn−3/2π−1/2 ways to place nkeys in a binary search tree (Catalan numbers). e.g.C20 = 6564120420.
I Deterministic (i.e., not randomized) uniquelyrepresented search trees are known to require Ω(
√n)
worst-case time for insert, delete, search [12].
I Treaps are randomized (not deterministic), and haveO(log n) average-case time for insert, delete, search
I If you memoize or cache the constructors of a uniquelyrepresented data structure, you can do equality testingin O(1) time by comparing pointers.
I The structure of the treap does not depend on the orderon which the operations are carried out.
I Treaps give a canonical form for sets: if A,B are sets,we can determine whether A = B by constructing treapscontaining the elements of A and B, and comparingthem. If the treaps are the same, the sets are equal.
I Treaps give an easy decision procedure for equality ofterms modulo associativity, commutativity, andidempotency.
I Treaps are very useful in program analysis (e.g., forcompilers) for solving fixpoint equations on sets.
I Partially persistent: Can access previous versions of adata structure, but cannot derive new versions fromthem (read-only access to a linear past.)
I Fully persistent: Can make changes in previous versionsof the data structure: versions can “fork.”
I Any linked data structure with constant boundedin-degree can be made fully persistent with amortizedO(1) space and time overhead, and worst case O(1)overhead for access [7]
I Confluently persistent: Can branch into two versions ofthe data structure, and later reconcile these branches
[1] A. Andersson and T. Ottmann.Faster uniquely represented dictionaries.In IEEE, editor, Proceedings: 32nd annual Symposiumon Foundations of Computer Science, San Juan, PuertoRico, October 1–4, 1991, pages 642–649, 1109 SpringStreet, Suite 300, Silver Spring, MD 20910, USA, 1991.IEEE Computer Society Press. bib pdf
[2] Rudolf Bayer.Symmetric binary B-trees: Data structure andmaintenance algorithms.Acta Inf, 1:290–306, 1972. bib
[3] Guy E. Blelloch and Margaret Reid-Miller.Fast set operations using treaps.In Proceedings of the 10th Annual ACM Symposium onParallel Algorithms and Architectures, pages 16–26,Puerto Vallarta, Mexico, June 1998. bib ps
[4] Adam L. Buchsbaum and Robert E. Tarjan.Confluently persistent deques via data-structuralbootstrapping.In Proceedings of the fourth annual ACM-SIAMSymposium on Discrete algorithms, pages 155–164.ACM Press, 1993. bib pdf ps
[5] Thomas H. Cormen, Charles E. Leiserson, andRonald R. Rivest.Intoduction to algorithms.McGraw Hill, 1991. bib
[6] P. F. Dietz.Fully persistent arrays.In F. Dehne, J.-R. Sack, and N. Santoro, editors,Proceedings of the Workshop on Algorithms and DataStrucures, volume 382 of LNCS, pages 67–74, Berlin,August 1989. Springer. bib
[7] James R. Driscoll, Neil Sarnak, Daniel Dominic Sleator,and Robert Endre Tarjan.Making data structures persistent.In ACM Symposium on Theory of Computing, pages109–121, 1986. bib pdf
[8] Amos Fiat and Haim Kaplan.Making data structures confluently persistent.In Proceedings of the Twelfth Annual ACM-SIAMSymposium on Discrete Algorithms (SODA-01), pages537–546, New York, January 7–9 2001. ACM Press.bib pdf
[9] Leonidas J. Guibas and Robert Sedgewick.A dichromatic framework for balanced trees.In FOCS, pages 8–21. IEEE, 1978. bib
[10] Chris Okasaki.Purely Functional Data Structures.Cambridge University Press, Cambridge, UK, 1998. bib
[11] Raimund Seidel and Cecilia R. Aragon.Randomized search trees.Algorithmica, 16(4/5):464–497, 1996. bib pdf ps
[12] Lawrence Snyder.On uniquely representable data structures.In 18th Annual Symposium on Foundations ofComputer Science, pages 142–146, Long Beach, Ca.,USA, October 1977. IEEE Computer Society Press. bib
[13] R. Sundar and R. E. Tarjan.Unique binary search tree representations andequality-testing of sets and sequences.In Baruch Awerbuch, editor, Proceedings of the 22ndAnnual ACM Symposium on the Theory of Computing,pages 18–25, Baltimore, MY, May 1990. ACM Press.bib pdf