CSE 23315331 Topic 7 Balanced search trees Rotate
CSE 2331/5331 Topic 7: Balanced search trees Rotate operation Red-black tree Augmenting data struct. CSE 2331/5331
Set Operations n n n n Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor CSE 2331/5331
Motivation n CSE 2331/5331
Balanced Search Tree CSE 2331/5331
Un-balanced Search Tree CSE 2331/5331
Goal: n CSE 2331/5331
Rotation Operation n A key operation in maintaining the “balance” of a binary search tree n Locally rotate subtree, while maintain binary search tree property x y right rotation y C A B left rotation x A B C CSE 2331/5331
Right Rotation Examples n Right rotation at 9. Rightrotationatat 13 7 ? ? Does it change Binary search tree property? CSE 2331/5331
Left Rotation Examples n Left rotation at node 3. what if we then right-rotate at node 6 ? left and right rotations are inverse of each other. CSE 2331/5331
Recall: Transplant CSE 2331/5331
Implementation of Right Rotation CSE 2331/5331
Implementation of Left Rotation CSE 2331/5331
n Rotation does not change binary search tree property! x y right rotation y C A B left rotation x A B C CSE 2331/5331
Rotation to Re-balance n Apply rotation to node 5 Reduce height to 3! CSE 2331/5331
Rotation to Re-balance n Apply rotation at node 5 Height not reduced! Need a double-rotation (first at 16, then at 5) In general, how and when? CSE 2331/5331
Red-Black Trees n Compared to an ordinary binary tree, a red-black binary tree: n n n A leaf node is NIL Hence all nodes are either NIL or have exactly two children! Each node will have a color, either red or black CSE 2331/5331
Definition n A Red-Black tree is a binary tree with the following properties: n n n Every node is either red or black Root is black Every leaf is NIL and is black If a node is red, then both its children are black For each node, all simple paths from this node to its decedent leaves contain same number of black nodes CSE 2331/5331
An Example n Double nodes are black. No two consecutive red nodes. CSE 2331/5331
Skipping Leaves CSE 2331/5331
Is This a Red-Black Tree? CSE 2331/5331
Exercise n Color the following tree to make a valid red-black tree CSE 2331/5331
n Given a tree node x n n size(x): the total number of internal nodes contained in the subtree rooted at x bh(x): the number of black nodes on the path from x to leaf (not counting x) CSE 2331/5331
Balancing Property of RB-tree n CSE 2331/5331
Balancing Property of RB-tree n CSE 2331/5331
Implication of the Theorem How to maintain RB-tree under Operations? CSE 2331/5331
Set Operations n n n n Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor Only need to consider Insert / delete CSE 2331/5331
Insertion Example Insert 36? 2? 24? CSE 2331/5331
Red-Black Tree Insert CSE 2331/5331
Insert Fixup: Case 3 (z is the new node) CSE 2331/5331
Example: Case 3 Insert 12? CSE 2331/5331
Implementation of Fixup Case 3 CSE 2331/5331
Remarks n CSE 2331/5331
Insert Fixup: Case 2 CSE 2331/5331
Example: Case 2 Insert 14? CSE 2331/5331
Implementation of Case 2 CSE 2331/5331
Insert Fixup: Case 1: (z is new node) n The parent and uncle of z are red: n n n Color the parent and uncle of z both black Color the grandparent of z red Continue with the grandparent of z CSE 2331/5331
Sibling CSE 2331/5331
Implementation of Case 1 When does this procedure terminate? CSE 2331/5331
Red-black Tree Insert Fixup n CSE 2331/5331
Example Insert 21? CSE 2331/5331
Augmenting Data Structure CSE 2331/5331
Balanced Binary Search Tree n n n n Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor Also support Select operation ? CSE 2331/5331
Augment Tree Structure n n Select ( T, k ) Goal: n n Ordinary binary search tree n n Augment the binary search tree data structure so as to support Select ( T, k ) efficiently O(h) time for Select(T, k) Red-black tree (balanced search tree) n O(lg n) time for Select(T, k) CSE 2331/5331
How To Augment Tree Structure? n CSE 2331/5331
Example M 9 C 5 P 3 T 2 F 3 A 1 D 1 H 1 Q 1 CSE 2331/5331
How to Setup Size Information? n Postorder traversal of the tree ! CSE 2331/5331
Augmented Binary Search Tree n n Let T be an augmented binary search tree OS-Select(x, k): n n Return the k-th smallest element in the subtree rooted at x OS-Select(T. root, k) returns the k-th smallest elements in the entire tree. OS-Select(T. root, 5) ? CSE 2331/5331
n n Correctness? Running time? n O(h) CSE 2331/5331
n OS-Rank(T, x) n Return the rank of the element x in the linear order determined by an inorder walk of T CSE 2331/5331
Example M 9 C 5 P 3 OS-Rank(T, M) D) ? T 2 F 3 A 1 D 1 H 1 Q 1 CSE 2331/5331
n n Correctness ? Time complexity? n O(h) CSE 2331/5331
Are we done ? n n Need to maintain augmented information under dynamic operations Insert / delete n Extract-Max can be implemented with delete CSE 2331/5331
Example M 9 C 5 P 3 Insert(J) ? T 2 F 3 A 1 D 1 H 1 Q 1 CSE 2331/5331
n n During the downward search, increase the size attribute of each node visited along the path from root to the final insert location. Time complexity: n n O(h) However, if we have to maintain balanced binary search tree, say Red-black tree n Also need to adjust size attribute after rotation CSE 2331/5331
Left-Rotate n n n y. size = x. left. size + x. right. size + 1 O(1) time per rotation CSE 2331/5331
n Right-rotate can be done similarly. n Overall: Two phases: n Update size for all nodes along the path from root to insertion location n n Update size for the Fixup stage involving O(1) rotations n n O(h) = O(lg n) time O(1) + O(lg n) = O(lg n) time O(h) = O(lg n) time to insert in a Red-Black tree n Same asymptotic time complexity as the non-augmented version CSE 2331/5331
Delete n Two phases: n Decrement size in each node on the path from the root to the node to be deleted n n During Fixup (to maintain balanced binary search tree property), update size for O(1) rotations n n O(h) = O(lg n) time O(1) + O(lg n) time Overall: n O(h) = O(lg n) time CSE 2331/5331
Summary n n Simple example of augmenting data structures In general, the augmented information can be quite complicated n n Can be a separate data structure! Need to consider how to maintain such information under dynamic changes CSE 2331/5331
- Slides: 58