Presentation is loading. Please wait.

Presentation is loading. Please wait.

10/22/2002CSE 202 - Red Black Trees CSE 202 - Algorithms Red-Black Trees Augmenting Search Trees Interval Trees.

Similar presentations


Presentation on theme: "10/22/2002CSE 202 - Red Black Trees CSE 202 - Algorithms Red-Black Trees Augmenting Search Trees Interval Trees."— Presentation transcript:

1 10/22/2002CSE 202 - Red Black Trees CSE 202 - Algorithms Red-Black Trees Augmenting Search Trees Interval Trees

2 CSE 202 - Red Black Trees2 Binary Search Trees A binary tree in which for each node y: If x is in y’s left subtree, then x  y. If z is in y’s right subtree, then y  z. Binary Search Trees can do: –Insert(S,x), Delete(S,x) –Search(S,k), Minimum(S), Maximum(S) –Successor(S,x), Predecessor(S,x) Combine Min- and Max-Priority Queue & Dictionary –Can you do “Increase-Key(S,x,k)” in a BST? “Obvious” implementation BST can be unbalanced. O(lg n) expected, O(n) worst case (n random requests). y x z...

3 CSE 202 - Red Black Trees3 Balanced Search Trees Basic idea: O(lg n) dynamic set operations t by ensuring tree height is O(lg n) n = size of the set Various implementations: Red-Black trees, B – trees (featured in CLRS) AVL trees, 2-3 trees, Splay trees (other methods) Binomial heaps, Fibonacci heaps (allow merging) Patricia trees ( O(k) for length-k keys)

4 CSE 202 - Red Black Trees4 Red-Black Trees BST, guaranteed to be nearly balanced. 1.Nodes have key, pointer, & two (possibly NIL) children. “NIL” is rally a black leaf – we’ll gloss over this. 2.Every nodes is colored “red” or “black”. 3.Root is black. 4.Red node can’t have red parent. 5.Paths from root to NIL’s all have the same number of black nodes (the “black-height”). =black = red

5 CSE 202 - Red Black Trees5 Node depth is O(lg n) in R-B tree Black Subtree: ignore red nodes –Every non-leaf has 2, 3, or 4 children. –Every leaf has the same depth, k. –Thus # black nodes  2 k+1 – 1 ( “=“ for full binary tree). In full red-black tree, n  2 k+1 – 1 (n = # nodes) –So lg n  lg (2 k+1 – 1)  lg 2 k = k; that is, k < lg n. –Longest path  2 k (obvious from picture). –So longest path  2 lg n.

6 CSE 202 - Red Black Trees6 Insertions into R-B tree Insert as new red leaf in correct spot. May create “red menace” (red node with red parent) Eliminate, promote, or straighten red menace. –Details on next page. After at most 2 x node depth moves, red menace will be gone If root is red, color it black bent straight

7 CSE 202 - Red Black Trees7 Moving Red Menaces Case 1: Uncle (grandparents other child) is red. –Recolor parent, uncle & grandparent. –Grandparent may be a red menace now. Case 2: Uncle is black, red menace is bent. –Rotate red menace with parent. –Red menace is now straight. –Proceed to Case 3 uncle red menace 1 2 3 4 5 6 6 5 1 3 4 2 8 8 5 8 4 2 6 6 5 4 2 8 Case 3: Uncle black, red menace straight. –Rotate & recolor parent & grandparent. –Red menace disappears.

8 CSE 202 - Red Black Trees8 Your turn Insert 1, 2, 3,... into empty tree. Other Operations Search Compare to root, go obvious direction, recursively. Successor If node has non-empty right subtree, find its MIN. Else, return closest ancestor that’s bigger (if any). Delete Complicated (like insert).

9 CSE 202 - Red Black Trees9 Augmenting Search Trees (chapter 14) Basic idea: Suppose we want to maintain additional information about data in a dynamic set. suppose this information can be computed at each node using only the data at that node and its immediate children. Such information is called a synthesized attribute. We can do so without increasing the big-O complexity of Insert or Delete. Why? Because the only information that is modified is on o the (original) path from leaf to root. the tree may shift a bit when we Insert or Delete

10 CSE 202 - Red Black Trees10 (Trivial) Example Suppose satellite data includes a “size” field At each node n we can maintain: 1.maximum size of any node in n’s subtree, and/or 2.total size of all nodes in n’s subtree. 5 1 9 1 2 5 6 3 3 key size total ( 5+1+3 = 9 )...

11 CSE 202 - Red Black Trees11 Total Size Example 12 3 23 0 1 1 6 2 5 1 2 6 4 3 14 17 1 6 3 3 3 5 1 1 9 2 2 7 3 - 13 5 5 Insert 12 3 26 0 1 1 6 2 17 1 2 6 4 3 10 17 1 6 3 3 3 5 1 1 9 2 5 7 3 3 13 5 5 * total needed * updating * * * redred redred redred redred redred redred redred redred redred redred redred redred several steps later

12 CSE 202 - Red Black Trees12 Example (continued) 12 3 26 0 1 1 6 2 17 1 2 6 4 3 10 17 1 6 3 3 3 5 1 1 9 2 5 7 3 3 13 5 5 * * * * * 12 3 14 0 1 1 6 2 26 1 2 6 4 3 10 17 1 6 3 3 3 5 1 1 9 2 5 7 3 3 13 5 5 * * * * * redred redred redred redred redred redred redred redred redred redred redred redred Previous slide. Still has red menace.

13 CSE 202 - Red Black Trees13 Is rank a synthesized attribute? (“Rank” means place in sorted list) In both these trees, root has rank-1 and rank- 4 child. But it’s rank is different. So rank can’t be computed just from children’s ranks. 2 3 4 1 3 2 4 1 But...

14 CSE 202 - Red Black Trees14 Search Tree & Rank in O(lg n) time “Number of nodes in subtree” is synthesized. Rank can be computed by adding 1 + nodes in left subtree + # of “left ancestors” + # nodes in their left subtrees. while going from root to the node. 18 1 5 3 1 1 13 1 11 4 1 3 8 1 5 21 1 2 6 1 1 9 1 1 17 1 2 14 1 1 19 1 1 key 1 per node nodes in subtree Rank of node “17” is the number of nodes inside dotted line.

15 CSE 202 - Red Black Trees15 Interval Trees (Closed) interval [a,b] is { x   | a  x  b }. Interval tree is a search tree with: key = left-hand endpoint of interval satellite data = right-hand endpoint of interval synthesized attribute = max r-h endpoint of subtree [1,4] 4 [6,7] 7 [3,8] 8 [11,16] 21 [15-16] 21 [19-20] 20 [14-21] 21

16 CSE 202 - Red Black Trees16 Overlapping Interval Problem (Two intervals overlap if they have any points in common.) Return an interval in tree that [x,y] overlaps, or “none”. Case 1: [x,y] overlaps root: you’re done. Case 2: [x,y] is entirely left of root: search left subtree. It can’t possibly overlap root or anything in right subtree. (Try [6,10] in previous example.) Case 3: [x,y] is entirely right of root & max right-hand endpoint in root’s left subtree: search right subtree. It can’t overlap root or anything in left subtree. (Try [17,18].) Case 4: [x,y] is right of root, but not of max right-hand endpoint in root’s left subtree: overlap in left subtree. Continue with [17,18] example – this case holds at second step.


Download ppt "10/22/2002CSE 202 - Red Black Trees CSE 202 - Algorithms Red-Black Trees Augmenting Search Trees Interval Trees."

Similar presentations


Ads by Google