Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.

Similar presentations


Presentation on theme: "CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees."— Presentation transcript:

1 CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees

2 all the keys in the yellow region are  20 all the keys in the blue region are  20 Splay Trees are Binary Search Trees BST Rules: entries stored only at internal nodes nodes in the left subtree have smaller key values nodes in right subtree have larger key values nodes with equal key value must all be in left or right subtree Inorder traversal returns entries in order of the key values (20,Z) (37,P)(21,O) (14,J) (7,T) (35,R)(10,A) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (8,N) (7,P) (36,L) (10,U) (40,X) note that two keys of equal value may be well- separated (§ 9.3)

3 Searching in a Splay Tree: Starts the Same as in a BST Search proceeds down the tree to found item or an external node. Example: Search for key 11 Since key not in tree, ends at external node (20,Z) (37,P)(21,O) (14,J) (7,T) (35,R)(10,A) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (8,N) (7,P) (36,L) (10,U) (40,X)

4 Example Searching in a BST, continued Search for key 8, ends at an internal node (20,Z) (37,P)(21,O) (14,J) (7,T) (35,R)(10,A) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (8,N) (7,P) (36,L) (10,U) (40,X)

5 Searching in BST If tree is balanced, search takes O(log n) time If tree is not balanced, search can take O(n) time Unfortunately, the worst case is quite common  What is this worst case?

6 Splay Trees do Rotations after Every Operation (Even Search) new operation: splay splaying moves a node to the root using rotations right rotation makes the left child (x) of a node y into y’s parent; y becomes the right child of x y x T1T1 T2T2 T3T3 y x T1T1 T2T2 T3T3 left rotation makes the right child (y) of a node x into x’s parent; x becomes the left child of y y x T1T1 T2T2 T3T3 y x T1T1 T2T2 T3T3 (structure of tree above y is not modified) (structure of tree above x is not modified) a right rotation about ya left rotation about x

7 Splaying: is x the root? stop is x a child of the root? right-rotate about the root left-rotate about the root is x the left child of the root? is x a left-left grandchild? is x a left-right grandchild? is x a right-right grandchild? is x a right-left grandchild? right-rotate about g, right-rotate about p left-rotate about g, left-rotate about p left-rotate about p, right-rotate about g right-rotate about p, left-rotate about g start with node x “ x is a left-left grandchild” means x is a left child of its parent, which is itself a left child of its parent p is x ’s parent; g is p ’s parent no yes no yes zig-zig zig-zag zig-zig zig

8 Visualizing the Splaying Cases zig-zag y x T2T2 T3T3 T4T4 z T1T1 y x T2T2 T3T3 T4T4 z T1T1 y x T1T1 T2T2 T3T3 z T4T4 zig-zig y z T4T4 T3T3 T2T2 x T1T1 zig x w T1T1 T2T2 T3T3 y T4T4 y x T2T2 T3T3 T4T4 w T1T1

9 Splaying Example let x = (8,N) x is right child of its parent, which is the left child of the grandparent left-rotate around p, then right-rotate around g (20,Z) (37,P)(21,O) (14,J) (7,T) (35,R) (10,A) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (8,N) (7,P) (36,L) (10,U) (40,X) x g p (10,A) (20,Z) (37,P)(21,O) (35,R) (36,L) (40,X) (7,T) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (14,J) (8,N) (7,P) (10,U) x g p (10,A) (20,Z) (37,P)(21,O) (35,R) (36,L) (40,X) (7,T) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (14,J) (8,N) (7,P) (10,U) x g p 1. (before rotating) 2. (after first rotation) 3. (after second rotation) x is not yet the root, so we splay again

10 Splaying Example, Continued now x is the left child of the root right-rotate around root (10,A) (20,Z) (37,P)(21,O) (35,R) (36,L) (40,X) (7,T) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (14,J) (8,N) (7,P) (10,U) x (10,A) (20,Z) (37,P)(21,O) (35,R) (36,L) (40,X) (7,T) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (14,J) (8,N) (7,P) (10,U) x 1. (before applying rotation) 2. (after rotation) x is the root, so stop

11 Example Result of Splaying tree might not be more balanced e.g. splay (40,X) before, the depth of the shallowest leaf is 3 and the deepest is 7 after, the depth of shallowest leaf is 1 and deepest is 8 (20,Z) (37,P)(21,O) (14,J) (7,T) (35,R)(10,A) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (8,N) (7,P) (36,L) (10,U) (40,X) (20,Z) (37,P) (21,O) (14,J) (7,T) (35,R) (10,A) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (8,N) (7,P) (36,L) (10,U) (40,X) (20,Z) (37,P) (21,O) (14,J) (7,T) (35,R) (10,A) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (8,N) (7,P) (36,L) (10,U) (40,X) before after first splay after second splay

12 Splay Tree Definition Splay tree is a binary search tree where node is splayed any time it is accessed Each search or update, splay only one node Splay internal node furthest from root Splaying costs O(h), where h is tree height  What is big-Oh for BST?  What is total cost of splaying?

13 Splay Trees & Ordered Dictionaries Which nodes are splayed after each operation? use parent of the internal node actually removed from the tree (e.g., parent of the node whose Entry was moved to node that was removed) remove(k) splay the new node containing the inserted entry insert(k,v) if key found, use that node if key not found, use last internal node in search find(k) splay nodemethod

14 Performance of Splay Trees Amortized cost of any splay operation is O(log n) Splay trees actually adapt to perform searches on frequently-requested items much faster than O(log n)!

15 Your Turn Insert 1, 3, 5, 7, 9, 11, 13, 15 into splay tree Then search for 1, 2, 16, 15 within tree

16 Multi-Way Search Tree (§ 9.4.1) Multi-way search tree is an ordered tree such that Each internal node has at least two children and stores d  1 entries e i = (k i, v i ), where d is the number of children For a node with children v 1 v 2 … v d storing entries e 1 e 2 … e d  1  keys in the subtree of v 1 are less than k 1  keys in the subtree of v i are between k i  1 and k i (i = 2, …, d  1)  keys in the subtree of v d are greater than k d  1 External nodes still used to mark when tree has ended 11 24 2 6 815 30 27 32

17 Multi-Way Inorder Traversal We can extend inorder traversals to multi-way search trees Namely, visit entry i within node v between recursive traversals of the subtrees of v rooted at children v i and v i    1 Like with a BST, inorder traversal of a multi-way search tree visits keys in increasing order 11 24 2 6 815 30 27 32 13579111319 1517 2461418 812 10 16

18 Multi-Way Searching Similar to search in a binary search tree At internal node with children v 1 v 2 … v d and entries e 1 e 2 … e d  1 If k  k i (i = 1, …, d  1) : the search terminates successfully If k  k 1 : we continue the search in child v 1 If k i  1  k  k i (i = 2, …, d  1) : we continue the search in child v i If k  k d  1 : we continue the search in child v d Key is not in tree if we reach an external node Example: search for 30 11 24 2 6 815 30 27 32 30

19 (2,4) Trees (§ 9.4.2) A (2,4) tree is multi-way search tree with following properties Node-Size Property: every internal node has at most four children Depth Property: all the external nodes have the same depth Depending on the number of children, internal node is called a 2-node, 3-node or 4-node 10 15 24 2 81227 3218

20 Height of a (2,4) Tree (2,4) tree of size n has height O(log n) At worst, each node in (2,4) tree has only 2 children But (2,4) tree must be balanced So, this worst case scenario is similar to a balanced binary tree! 1 2 2h12h1 0 items 0 1 h1h1 h depth

21 Insertion We insert a new entry (k, o) at the parent v of the last leaf reached searching for k Preserve the depth property, but… this may cause an overflow (i.e., node v becomes a 5-node) Example: inserting key 30 causes an overflow 27 32 35 10 15 24 2 81218 10 15 24 2 812 27 30 32 35 18 v v 27 32 35

22 Overflow and Split Handle overflow at a 5-node v with split operation: Let v 1 … v 5 be children of v and e 1 … e 4 be entries at v replace node v with nodes v' and v"  v' is a 3-node with keys e 1 e 2 and children v 1 v 2 v 3  v" is a 2-node with key e 4 and children v 4 v 5 Promote e 3 to the parent, u, of node v (may create new root) Overflow may propagate to the parent node ( u ) 15 24 12 27 30 32 35 18 v u v1v1 v2v2 v3v3 v4v4 v5v5 15 24 32 12 27 30 18 v'v' u v1v1 v2v2 v3v3 v4v4 v5v5 35 v"v"

23 Analysis of Insertion Algorithm insert(k, o) 1.Search for key k to find insert node v 2.Add the new entry (k, o) at node v 3. while overflow(v) if isRoot(v) create a new empty root above v split(v) v  parent(v) What is big-Oh time for: Step 1? Step 2? Checking overflow(v)? Calling isRoot(v)? Calling split(v)? Step 3 ? insert(k,o)?

24 Deletion Consider deletion of entry where item is at the node with leaf children Otherwise, replace entry with inorder successor and delete the latter entry Example: delete key 24 by replacing it with 27 27 32 35 10 15 24 2 81218 32 35 10 15 27 2 81218

25 Underflow and Fusion Deleting entry from node v may cause underflow Node v is 1-node (has one child, but no entries) Handle underflow at v with parent u, in two cases: Case 1: v has adjacent siblings that is a 2-node Fusion operation: merge v with adjacent sibling w and move entry from u into merged node v' Fusion may propagate underflow to the parent node ( u ) 9 14 2 5 710 u v 9 10 14 u v'v'w 2 5 7

26 Underflow and Transfer Case 2: v has adjacent sibling w that is 3- or 4-node Transfer operation: 1. move child of w to v 2. move entry from u to v 3. move entry from w to u After transfer, no more underflows exist 4 9 6 82 u vw 4 8 62 9 u vw

27 Analysis of Deletion What is big-Oh time needed for deletion? Height of tree = Time to find entry to delete Time needed for underflow Maximum possible number of underflows Time needed for transfer Maximum possible number of transfers Total time needed for deletion

28 Your Turn Insert 1, 2, 3, 4, 5, 6, 7, 8 into (2,4) tree Then delete 1, 2, 3, 4, 5, 6, 7, 8 from tree

29 From (2,4) to Red-Black Trees Red-black tree is a representation of (2,4) tree using a binary tree whose nodes are colored red or black Compared with (2,4) tree, red-black tree has same performance, but simpler implementation! 2 6 73 54 4 6 27 5 3 3 5 OR

30 Red-Black Trees (§ 9.5) Red-black tree is a binary search tree satisfying following properties: Root Property: root is black External Property: every leaf is black Internal Property: children of red nodes are black Depth Property: all leaves have the same black depth 9 154 6212 7 21

31 Height of a Red-Black Tree Red-black tree storing n entries has height O(log n) Red-black tree is functionally equivalent to (2,4) tree which has height O(log n) The search algorithm for red-black tree is same as for binary search tree So, searching in a red-black tree takes O(log n) time

32 Insertion insert (k, o) : execute BST insertion algorithm and color z, the new node, red (except when inserting a new root) preserve root, external, and depth properties If parent, v, of z is black, internal property is also preserved Else ( v is red) have double red (violation of internal property) and need to reorganize the tree Example where an insertion (inserting 4) causes double red: 6 3 8 6 3 8 4 z vv z

33 Remedying a Double Red Double red with child z, parent v, and sibling of v, w 4 6 7 z vw 2 4 6 7.. 2.. Case 1: w is black Double red is incorrect replacement of 4-node Restructuring: change 4-node replacement Case 2: w is red Double red corresponds to an overflow Recoloring: split equivalent 4 6 7 z v 2 4 6 7 2 w

34 Restructuring Restructuring remedies child-parent double red when parent has a black sibling Equivalent to restoring correct replacement of 4-node Restores internal property and preserves other properties 4 6 7 z v w 2 4 6 7.. 2.. 4 6 7 z v w 2 4 6 7.. 2..

35 Restructuring (cont.) Four restructuring possibilites depending on whether double red nodes are left or right children 2 4 6 6 2 4 6 4 2 2 6 4 2 6 4

36 Recoloring Recoloring remedies double red when parent node has red sibling Parent v and its sibling w become black and the grandparent u becomes red (unless u is the root) Equivalent to performing split on 5-node Double red violation may propagate to the grandparent u 4 6 7 z v 2 4 6 7 2 w 4 6 7 z v 6 7 2 w … 4 … 2

37 Analysis of Insertion Red-black tree has O(log n) height Step 1 takes O(log n) time Step 2 takes O(1) time Step 3 takes O(log n) time because we perform at most O(log n) recolorings, each taking O(1) time, and at most one restructuring taking O(1) time Insertion takes O(log n) time! Algorithm insert(k, o) 1.Search for k to find insert node z 2.Add new entry at node z and color z red 3. while doubleRed(z) if isBlack(sibling(parent(z))) z  restructure(z) return else // isRed(sibling(parent(z)) recolor(z) z  parent(z)

38 Deletion remove (k) begins with deletion algorithm for binary search trees Let v be node removed, w the external node removed, and r the sibling of w If either v or r was red, color r black Else ( v and r were both black), color r double black, violating internal property and triggering reorganization of the tree Example: deleting of 8 causes double black: 6 3 8 4 v rw 6 3 4 r

39 Remedying a Double Black Remedying double black node w with sibling y has three cases: Case 1: y is black and has a red child Perform restructuring and then we are done Case 2: y is black and children are both black Perform recoloring, which may propagate up double black violation Case 3: y is red Perform adjustment, equivalent to choosing different representation of 3-node, after which Case 1 or Case 2 will apply Deletion also takes O(log n) time!

40 Red-Black Tree Reorganization Insertion remedy double red Red-black tree action(2,4) tree actionresult restructuring change of 4-node representation double red removed recoloringsplit double red removed or propagated up Deletion remedy double black Red-black tree action(2,4) tree actionresult restructuringtransferdouble black removed recoloringfusion double black removed or propagated up adjustment change of 3-node representation restructuring or recoloring follows

41 Your Turn Insert 1, 2, 3, 4, 5, 6, 7, 8 into (2,4) tree Then delete 1, 2, 3, 4, 5, 6, 7, 8 from tree

42 Daily Quiz Write node class for Red-Black tree


Download ppt "CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees."

Similar presentations


Ads by Google