Download presentation
Presentation is loading. Please wait.
1
CS200: Algorithms Analysis
2
RED BLACK TREES Red black trees maintain a balanced tree structure so that height of tree = θ(log n). Idea is to add a color(r, b) to a BST. RED-BLACK PROPERTIES 1. every node is r or b. 2. every leaf (nil) is black. 3. if a node is red then both its children are black => there can’t be two successive red nodes in a tree. 4. every simple path from a node to a leaf contains the same # of b nodes. 5. root is black (simplifying assumption for inserts).
3
Black-Height of a node : bh(x) = # of b nodes on path to leaf not counting x.
Show an example tree, label with r or b, and h
4
Example RB Tree Label with bh (4. every simple path from a node to a leaf contains the same # of b nodes and bh(x) = # of b nodes on path to leaf not counting x.) A height-h node has bh >= h/2. Why?
15
Nodes
16
Inductive Proof of Lemma 13.1 (Go over on own)
Prove: a red-black tree with n internal nodes has h <=2log(n+1). Proof is based on claim that a sub-tree rooted at node x contains >= 2bh(x)-1 internal nodes. Proof is by induction on height h of node x. basis: h=0 => x is nil; black leaf => 2bh(x)-1 = 20-1 = 0 inductive step: x is an internal node with 2 children.
17
A sub-tree rooted at x has at least 2bh(x)-1 internal nodes.
bh of children of x have at least bh(x) -1 (if child is black). subtree has at least (2bh(x)-1 -1) + (2bh(x)-1 -1) +1 internal nodes. (2bh(x)-1 -1) + (2bh(x)-1 -1) +1 = 2(2bh(x)-1 -1) +1= 2bh(x)-1 internal nodes. Let h be the height of tree then n >=2bh(root) -1 >= 2h/2 -1 (using property 3 of red-black trees). n >= 2h/2-1 n+1>= 2h/2 log(n+1)>=h/2 2log(n+1)>=h
18
Query Operations Corollary. The queries SEARCH, MIN, MAX, SUCCESSOR, and PREDECESSOR all run in O(lg n) time on a red-black tree with n nodes.
19
Modifying Operations The operations INSERT and DELETE cause modifications to the red-black tree: • the operation itself, • color changes, • restructuring the links of the tree via “rotations”.
20
Rotations
21
Insertions
22
Insertions
23
Insertions
24
Insertions
25
Insertions
27
Pseudo Code
28
Graphical Notation
29
Case 1
31
Case 2
32
Case 3
34
Analysis : Given line 1., what property of RB trees can be violated by above code?
35
Summary RB Tree properties Balancing algorithms (rotations) analysis
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.