Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 310 – Data Structures All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley. All rights reserved. photo ©Oregon Scenics used.

Similar presentations


Presentation on theme: "1 CS 310 – Data Structures All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley. All rights reserved. photo ©Oregon Scenics used."— Presentation transcript:

1 1 CS 310 – Data Structures All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley. All rights reserved. photo ©Oregon Scenics used with permissionOregon Scenics Trees Part II

2 2 Balanced binary search trees Binary search trees with an extra condition –Left and right subtrees must have the same height. –In practice, we use the condition that left and right subtrees can differ in height by no more than one. –Note: It is convenient to denote the hight of an empty tree as -1.

3 3 Keeping the tree balanced results in logarithmic search time. AVL trees Adelson-Velskii & Landis First balanced tree – 1962

4 4 Unbalanced tree insert(tree, 1) – results in an unbalanced tree

5 5 Which nodes to rebalance? Nodes along the path from the insertion point to the root might need rebalancing.

6 6 How can we get into trouble? Insertion into left subtree of left child Insertion into right subtree of left child

7 7 Where else? Symmetric problems when inserting into the right subtree –Insertion in the right subtree of the left child of the node in question. –Insertion in the right subtree of the right child of the node in question.

8 8 The outside cases When the unbalance comes from inserting on the outside of the tree, we can fix the problem with one rotation.

9 9 Single rotation

10 10 Concrete example

11 11 Symmetric case for single rotation

12 12

13 13 The inside cases Rotations on the inside are more difficult.

14 14 Double rotation rotation 1 swap child and grandchild

15 15 Double Rotation rotation 2 rotation between grandparent and new parent

16 16 Practice makes perfect Practice with http://webpages.ull.es/users/jriera/Docencia/AVL/AVL%20tree%20applet.htm http://webpages.ull.es/users/jriera/Docencia/AVL/AVL%20tree%20applet.htm

17 17 AVL implementation Implementation difficult Basic idea –For insertion into tree T, insert into appropriate left/right T lr subtree. –If height of T lr remains the same, all done. –Otherwise, we need to see if T has become unbalanced. If so, perform appropriate repairs with root T.

18 18

19 19 AVL In practice, requires two passes through tree –down: insertion –up: repair Better schemes have been proposed.

20 20 Red-Black trees Single top down pass for insertion & deletion Binary search trees with: –Colored nodes: red or black (null nodes treated as black) –Root is always black –If node is red  children are black –Constant black depth. Every path from node to null link has the same number of black nodes.

21 21 Sentinels Many implementations of red-black trees sometimes create special nodes called sentinels. Sentinel nodes are used in place of the null link to indicate leaves. In red-black trees, sentinels are always colored black.

22 22 Properties If there are B black nodes along each of the paths, the tree must have at least 2 B -1 black nodes. As there are never two consecutive red nodes: –height is at most 2log(N+1) –which implies logarithmic search

23 23 Sample red-black tree

24 24 Insertion New nodes are always inserted as leaves. What color? – black?  Other paths will no longer have the same number of black nodes. –red? If parent is black, then we are okay:

25 25 Inserting when the parent is red What color is the parent’s sibling? – black and inserted leaf is an outside child relative to grandparent: single rotation & recolor Color change ensures we do not have two consecutive red nodes. Note: Figure does not assume X is a leaf.

26 26 Red parent & black sibling We saw outside children  single rotation Inside children  double rotation

27 27 Inserting when the parent is red Parent’s sibling is also red?

28 28 Insertions with red parent & sibling Before rotationsSingle rotation Problems –consecutive red nodes –recoloring doesn’t help insert 95

29 29 Insertions with red parent & sibling Double rotation insert 79after first rotationafter second rotation What if the 80’s parent (originally node 70’s parent) had been red? Remember: This is a subtree, you could not construct a tree that looked like this.

30 30 What if 80’s parent had been red? We could try to propagate this up the tree, applying the rotations to the next higher level. Unfortunately, this puts us in the same situation as the AVL tree which requires two traversals.

31 31 Red-black tree Top down insertion We only get into trouble when the parent of the inserted node has a red sibling. By recognizing this, we can prevent it from happening.

32 32 Swapping colors On our way down, if we see two red children: we swap the parent and child colors:

33 33 Swapping colors Is this all right? –black depth preserved –What if the new red node is the root? It is a problem, but we can just recolor it black. –What if the parent is red? let us think about this…

34 34 Swapping colors when the parent is also red Parent’s sibling is black? –Swap colors. –Repair with single (slide 28) or double rotation (slide 29) Parent’s sibling is red? –Can’t happen! –Why not?

35 35 Top-down red-black tree Implementation is complicated by some special cases. Two tricks to ease implementation: –Instead of null links, we have a sentinel node which is always black. –The root pointer points to a pseudoroot node Contains a smaller than any other value (-∞). Right pointer points to real root.

36 36

37 37

38 38

39 39

40 40

41 41

42 42

43 43

44 44 insert 76 while (compare( item, current ) != 0 ) { great = grand; grand = parent; parent = current; current = compare( item, current ) < 0 ? current.left : current.right; // Check if two red children; // fix if so if (current.left.color == RED && current.right.color == RED) handleReorient( item ); }

45 45 insert 76 private void handleReorient(T item) { // flip color current.color = RED current.left.color = BLACK; current.right.color = BLACK; … }

46 46 insert 76 private void handleReorient(T item) { … // slightly rewritten from Weiss boolean leftOfGrand = compare(item, grand) < 0; boolean leftOfParent = compare(item, parent) < 0; if (parent.color == RED) { grand.color = RED; if (leftOfGrand != leftOfParent) { // double rotation parent = rotate(item, grand); } current = rotate(item, great); current.color = BLACK; } header.right.color = BLACK; }

47 47 insert 76 private void handleReorient(T item) { … // slightly rewritten from Weiss boolean leftOfGrand = true; boolean leftOfParent = true; if (parent.color == RED) { grand.color = RED; if (leftOfGrand != leftOfParent) { // double rotation parent = rotate(item, grand); } current = rotate(item, great); current.color = BLACK; } header.right.color = BLACK; }

48 48 insert 76 rotate(T item, RedBlackNode parent) { if (compare(item, parent) < 0) { … } else { return parent.right = compare(item, parent.right) < 0 ? rotateWithLeftChild(parent.right) : rotateWithRightChild(parent.right); }

49 49 insert 76 rotate(T item, RedBlackNode parent) { if (compare(item, parent) < 0) { … } else { return parent.right = compare(item, parent.right) < 0 ? rotateWithLeftChild(parent.right) : rotateWithRightChild(parent.right); }

50 50 insert 76 private void handleReorient(T item) { … // slightly rewritten from Weiss boolean leftOfGrand = true; boolean leftOfParent = true; if (parent.color == RED) { grand.color = RED; if (leftOfGrand != leftOfParent) { // double rotation parent = rotate(item, grand); } current = rotate(item, great); current.color = BLACK; } header.right.color = BLACK; }

51 51 insert 76 insert continues until current is sentinel parent and now we can insert 76 as a red node.

52 52 Red/Black tree applets http://gauss.ececs.uc.edu/RedBlack/redblack.html or http://webpages.ull.es/users/jriera/Docencia/AVL/AVL%20tree%20applet.htmhttp://webpages.ull.es/users/jriera/Docencia/AVL/AVL%20tree%20applet.htm

53 53 Red/Black tree deletion More complicated than insertion. We will cover the basic ideas and omit the implementation: –Deleting black nodes causes problems  make sure we delete red nodes. –We replace the values in internal nodes.

54 54 Red/Black tree deletion X – current node S – sibling P – parent Assume sentinel red Consider what we can do when X’s children are black.

55 55 Case 1 If S has black children, we can perform a color flip.

56 56 Case 2 If sibling’s outer child is red, perform a single rotation.

57 57 Case 3 If sibling’s inner child is red, perform a double rotation.

58 58 Deletion when X is a leaf Recall sentinels are colored black. Therefore, we can consider X to have two black children and use the 3 cases just described.

59 59 X has a red child? In all three cases, X was colored red, so the color flip and rotations inappropriate. Without covering the specifics, we can move to the next level and perform an operation to make X one of the following: –red –a leaf node  use one of the three cases –or X has a single child: red child  delete X, make child black black child  use one of the three cases

60 60 b-trees Log N search seems less convincing when some operations are orders of magnitude slower than others. A fast hard drive in 2006 can find a disk block in about 7.5 ms, or about 133 uncorrelated accesses per s. In contrast, an AMD Sempron 3600+ (top of the value line late 2006) can execute over 3,000 MFLOPS per s. Successful search in a 10 million record balanced binary search tree requires about 25 comparisons. –Trivial in RAM –About.2 s on disk assuming nobody else is using the system.

61 61 M-ary b-trees Data stored in leaves Nonleaf nodes –store up to M-1 keys –i th key is the smallest subkey in i+1 th subtree Root either –a leaf or –has between 2 to M children. All interior nodes (except root) ceil(M/2) to M children. All leaves at the same depth with ceil(L/2) to L data items (L maximum number of items, root leaf may have less)

62 62 Sample b-tree

63 63 Selecting M & L Optimal choice of M and L depends upon the minimum amount of information that can addressed on a disk. While disks typically operate on a small blocks of bytes (512), many operating systems group the blocks into a larger unit called a cluster.

64 64 Selecting M & L We typically choose M & L based upon the cluster size. M = floor(cluster size / key size) L = floor(cluster size / record size) In the worst case, we will access about log M/2 (N) clusters.

65 65 Adding to a b-tree Very easy when there’s room in the leaf node: insert 57

66 66 Adding to a full node Split into two leaves if possible: insert 55

67 67 When the interior node is full insert 40 cannot split easily

68 68 Splitting an interior node interior node covering 40: 8, 18, 26, and 35 leaf node: 35, 36, 37, 38, 39 Split leaf: [35, 36, 37], and [38, 39, 40] Split the interior node to: [8, 18], promote 26 to parent, [35, 38]

69 69 Splitting an interior node

70 70 b-tree insertion If parent is full, the process can be repeated.

71 71 b-tree deletion Just remove it when there are ceil(L/2) items will still remain. When the number of items is too small, merge nodes


Download ppt "1 CS 310 – Data Structures All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley. All rights reserved. photo ©Oregon Scenics used."

Similar presentations


Ads by Google