Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct.

Similar presentations


Presentation on theme: "CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct."— Presentation transcript:

1 CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct.

2 CSE 2331/5331 Set Operations Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor

3 Motivation CSE 2331/5331

4 Balanced Search Tree CSE 2331/5331

5 Un-balanced Search Tree CSE 2331/5331

6 Goal: CSE 2331/5331

7 Rotation Operation A key operation in maintaining the “balance” of a binary search tree Locally rotate subtree, while maintain binary search tree property CSE 2331/5331 x y AB C y x A BC right rotation left rotation

8 Right Rotation Examples Right rotation at 9. CSE 2331/5331 Right rotation at 7 ?Right rotation at 13 ? Does it change Binary search tree property?

9 Left Rotation Examples Left rotation at node 3. CSE 2331/5331 what if we then right-rotate at node 6 ? left and right rotations are inverse of each other.

10 Recall: Transplant CSE 2331/5331

11 Implementation of Right Rotation CSE 2331/5331

12 Implementation of Left Rotation CSE 2331/5331

13 Rotation does not change binary search tree property! CSE 2331/5331 x y AB C y x A BC right rotation left rotation

14 Rotation to Re-balance Apply rotation to node 5 CSE 2331/5331 Reduce height to 3!

15 Rotation to Re-balance Apply rotation at node 5 CSE 2331/5331 Height not reduced! Need a double-rotation (first at 16, then at 5) In general, how and when?

16 Red-Black Trees Compared to an ordinary binary tree, a red-black binary tree: 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

17 Definition A Red-Black tree is a binary tree with the following properties: 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

18 An Example Double nodes are black. CSE 2331/5331 No two consecutive red nodes.

19 Skipping Leaves CSE 2331/5331

20 Is This a Red-Black Tree? CSE 2331/5331

21 Exercise Color the following tree to make a valid red-black tree CSE 2331/5331

22 Given a tree node x 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

23 Balancing Property of RB-tree CSE 2331/5331

24 Proof Cont. CSE 2331/5331

25 Balancing Property of RB-tree CSE 2331/5331

26 Given a tree node x 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

27 Balancing Property of RB-tree CSE 2331/5331

28 Balancing Property of RB-tree CSE 2331/5331

29 Implication of the Theorem CSE 2331/5331 How to maintain RB-tree under Operations?

30 CSE 2331/5331 Set Operations Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor Only need to consider Insert / delete

31 Insertion Example CSE 2331/5331 Insert 24? Insert 2?Insert 36?

32 Red-Black Tree Insert CSE 2331/5331

33 Insert Fixup: Case 3 (z is the new node) CSE 2331/5331

34 Example: Case 3 CSE 2331/5331 Insert 12?

35 Implementation of Fixup Case 3 CSE 2331/5331

36 Remarks CSE 2331/5331

37 Insert Fixup: Case 2 CSE 2331/5331

38 Example: Case 2 CSE 2331/5331 Insert 14?

39 Implementation of Case 2 CSE 2331/5331

40 Insert Fixup: Case 1: (z is new node) The parent and uncle of z are red: Color the parent and uncle of z both black Color the grandparent of z red Continue with the grandparent of z CSE 2331/5331

41 Sibling CSE 2331/5331

42 Implementation of Case 1 CSE 2331/5331 When does this procedure terminate?

43 Red-black Tree Insert Fixup CSE 2331/5331

44 Example CSE 2331/5331 Insert 21?

45 Augmenting Data Structure CSE 2331/5331

46 Balanced Binary Search Tree Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor Also support Select operation ?

47 Augment Tree Structure Select ( T, k ) Goal: Augment the binary search tree data structure so as to support Select ( T, k ) efficiently Ordinary binary search tree O(h) time for Select(T, k) Red-black tree (balanced search tree) O(lg n) time for Select(T, k) CSE 2331/5331

48 How To Augment Tree Structure? CSE 2331/5331

49 Example CSE 2331/5331 M9M9 C5C5 A1A1 F3F3 D1D1 H1H1 P3P3 T2T2 Q1Q1

50 How to Setup Size Information? CSE 2331/5331 Postorder traversal of the tree !

51 Augmented Binary Search Tree Let T be an augmented binary search tree OS-Select(x, k): 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. CSE 2331/5331 OS-Select(T.root, 5) ?

52 Correctness? Running time? O(h) CSE 2331/5331

53 OS-Rank(T, x) Return the rank of the element x in the linear order determined by an inorder walk of T CSE 2331/5331

54 Example CSE 2331/5331 M9M9 C5C5 A1A1 F3F3 D1D1 H1H1 P3P3 T2T2 Q1Q1 OS-Rank(T, M) ? OS-Rank(T, D) ?

55 Correctness ? Time complexity? O(h) CSE 2331/5331

56 Need to maintain augmented information under dynamic operations Insert / delete Extract-Max can be implemented with delete CSE 2331/5331 Are we done ?

57 Example CSE 2331/5331 M9M9 C5C5 A1A1 F3F3 D1D1 H1H1 P3P3 T2T2 Q1Q1 Insert(J) ?

58 During the downward search, increase the size attribute of each node visited along the path from root to the final insert location. Time complexity: O(h) However, if we have to maintain balanced binary search tree, say Red-black tree Also need to adjust size attribute after rotation CSE 2331/5331

59 Left-Rotate y.size = x.size x.size = x.left.size + x.right.size + 1 O(1) time per rotation CSE 2331/5331

60 Right-rotate can be done similarly. Overall: Two phases: Update size for all nodes along the path from root to insertion location O(h) = O(lg n) time Update size for the Fixup stage involving O(1) rotations O(1) + O(lg n) = O(lg n) time O(h) = O(lg n) time to insert in a Red-Black tree Same asymptotic time complexity as the non-augmented version CSE 2331/5331

61 Delete Two phases: Decrement size in each node on the path from the root to the node to be deleted O(h) = O(lg n) time During Fixup (to maintain balanced binary search tree property), update size for O(1) rotations O(1) + O(lg n) time Overall: O(h) = O(lg n) time CSE 2331/5331

62 Summary Simple example of augmenting data structures In general, the augmented information can be quite complicated Can be a separate data structure! Need to consider how to maintain such information under dynamic changes CSE 2331/5331


Download ppt "CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct."

Similar presentations


Ads by Google