Data Structures and and Algorithm Xiaoqing Zheng

Size: px
Start display at page:

Download "Data Structures and and Algorithm Xiaoqing Zheng"

Transcription

1 Data Structures and Algorithm Xiaoqing Zheng

2 Trees (max heap) PARENT(i) return i /2 LEFT(i) return 2i RIGHT(i) return 2i

3 Binary trees root[t] / / / / / / / / / / / / / Not array!

4 Binary Search Tree Each node x has: key[x] Pointers: left[x] right[x] p[x]

5 Binary Search Tree Property: for any node x: For all nodes y in the left subtree of x: key[y] key[x] For all nodes y in the right subtree of x: key[y] key[x] Given a set of keys, is BST for those keys unique? 8

6 No uniqueness

7 What can we do given BST? Sort! INORDER-TREE-WALK(x) ) 1. if x NIL 2. then INORDER-TREE-WALK(left[x]) 3. print key[x] 4. INORDER-TREE-WALK(right[x]) i A preorder tree walk prints the root tbefore the values in either subtree, and a postorder tree walk prints the root after the values in its subtrees.

8 Sort?

9 Sort?

10 Sort?

11 Sort?

12 Sort?

13 Sort?

14 Sort?

15 Sort?

16 Sort?

17 Sort?

18 Sort?

19 Sort?

20 Analysis of inorder-walk Theorem. If x is the root of an n-node subtree, then the call INORDER-TREE-WALK(x) takes Θ(n) times. Substitution method T(n) ) = (c + d)n + c Base case: n = 0, T(0) = (c + d) 0 + c = c For n > 0, T(n) = T(k) + T(n k 1) + d = ((c + d)k + c) + ((c + d) (n k 1) + c) + d = (c + d)n + c (c + d) + c + d =(c + d)n + c

21 Sorting Does it mean that we can sort n keys in O(n) time? No. It just means that building a binary search tree takes Ω(nlgn) time (in the comparison model)

22 BST as a data structure Operations: 9 Insert(x) Delete(x) ( ) Search(k)

23 Search TREE-SEARCH(x, k) 1. if x = NIL or k = key[x] ] 2. then return x 3. if k < key[x] ] 4. then return TREE-SEARCH(left[x], k) 5. else return TREE-SEARCH(right[x], g k) )

24 Search ITERATIVE-TREE-SEARCH(x, k) 1. while x NIL and k key[x] ] 2. do if k < key[x] 3. then x left[x] [ ] 4. else x right[x] 5. return x On most computers, this version is more efficient.

25 Minimum and maximum TREE-MINIMUM(x) 1. while left[x] [ ] NIL 2. do x left[x] 3. return x TREE-MAXIMUM(x) 1. while right[x] NIL 2. do x right[x] 7 3. return x 8

26 Successor and predecessor 15 Which is the node 15's successor

27 Successor and predecessor Which h is the node 13's successor

28 Successor and predecessor TREE-SUCCESSOR(x) 1. if right[x] [ ] NIL 2. then return TREE-MINIMUN(right[x]) 3. y p[x] ] 4. while y NIL and x = right[y] 5. do x y 6. y p[x] 7. return y Running time O(h)

29 Constructing BST TREE-INSERT(T, z) ) Running time 9 1. y NIL O(h) 2. x root[t] 3. while x NIL 4. do y x 5. if key[z] < key[x] 6. then x left[x] 7. else x right[x] iht[ ] 8. p[z] y 9. if y = NIL 10. then root[t] z 11. else if key[z] < key[y] 12. then left[x] z 13. else right[x] z TREE-INSERT(T, 2) 8

30 Analysis After we insert n elements, 1 what is the worst possible BST height? 2 Pretty bad: n 1 Average: O(nlgn) 3 4 5

31 Deletion (case 1) z 6 7 z has no child. 7

32 Deletion (case 2) z z has one child. 7

33 Deletion (case 3) z y y 7 7 z has two children.

34 Deletion TREE-DELETE(T, z) ) 1. if left[z] = NIL or right[z] = NIL Running time: 2. then y z O(h) 3. else y TREE-SUCCESSOR(z) 4. if left[y] NIL 5. then x left[y] 6. else x right[y] 9. if p[y] = NIL 7. if x NIL 10. then root[t] x 8. then p[x] p[y] 11. else if y = left[p[y]] 12. then left[p[y]] x Note: z's successor 13. else right[p[y]] x just has one child or 14. if y z z has one child. 15. then key[z] [ ] key[y] [ ] 16. return y

35 Balanced search trees Balanced search trees, or how to avoid this even in the worst case 1 2 AVL (Adelson-Veskii and Landis) tree is identical to a binary search tree, except that for every node in the tree, the height of the left and right subtrees can differ by at most

36 AVL trees Which one is AVL tree?

37 AVL trees A violation i might occur in four case when we insert new node to the AVL tree. Case 1: an insertion into the left subtree of the left child of R. Case2 : an insertion into the right subtree of the left child of R. Case 3: an insertion into the left subtree of the right child of R. Case 4: an insertion into the right subtree of the right child of R.

38 Single rotation B A A γ B α β α β γ Right rotation ti to fix case 1

39 Single rotation A B α β B γ α A β γ Lft Left rotation tti to fix case 4

40 Double rotation C A δ α B β γ Single rotation ti fails to fix case 2

41 Double rotation (first step) C C A δ B δ α B A γ β γ α β Lft Left rotation tti

42 Double rotation (second step) C B B δ A C A γ α β γ δ α β Right rotation ti

43 Double rotation C B A δ A C α B α β γ δ β γ Lft Left-right htdouble rotation ti to fix case 2

44 Double rotation A B α C A C B δ α β γ δ β γ Right-left lftdouble rotation tti to fix case 3

45 AVL tree rotation Four types Rotation Case 1: Left-left f Right rotation Case 4: Right-right Left rotation Case 2: Left-right Left-right double rotation Case 3: Right-left Right-left double rotation

46 AVL tree example Insert 2

47 AVL tree example (cont.) Insert 1 Right rotation

48 AVL tree example (cont.) Insert 4 and 5 Left rotation

49 AVL tree example (cont.) Insert 6 Left rotation

50 AVL tree example (cont.) Insert 7 Left rotation

51 AVL tree example (cont.) Insert 16 and 15 Right-left rotation

52 AVL tree example (cont.) Insert 14 Right-left rotation

53 AVL tree example (cont.) Insert 13 Left rotation

54 AVL tree example (cont.) Insert 12 Right rotation

55 AVL tree example (cont.) Insert 11 Right rotation

56 AVL tree example (cont.) Insert 10 Right rotation

57 AVL tree example (cont.) Insert 8 and 9 Left-right rotation

58 Red-black trees BSTs with an extra one-bit color field in each node. Red-black properties: 1. Every node is either red or black. 2. The root is black. 3. Every leaf (NIL) is black. 4. If a node is red, then both its children are black. 5. All simple paths from any node x to a descendant leaf have the same number of black nodes.

59 Example of a red-black tree NIL NIL NIL 26 NIL NIL NIL NIL NIL NIL

60 Example of a red-black tree nil[t]

61 Height of a red-black tree

62 Height of a red-black tree

63 Height of a red-black tree

64 Height of a red-black tree

65 Height of a red-black tree

66 Lemma of red-black tree We call the number of black nodes on any path from, but not including, a node x down to a leaf the blackheight of the node, denoted bh(x). Lemma. A red-black tree with n internal nodes has height at most 2lg(n + 1) Dynamic-set operations search, minimum, i maximum, successor, and predecessor can be implemented in O(lgn) ) time on red-black trees.

67 Proof Subtree rooted at any node x contains at least 2 bh(x) ) 1 internal nodes. Base case: Height of x is 0, then x must be a leaf (nil[t]), subtree rooted at x contains at least 2 bh(x) 1 = = 0 internal nodes. Inductive: Height of a child of x is less than the height of x itself, subtree rooted at x contains at least (2 bh(x) - 1 1) + (2 bh(x) - 1 1) + 1 = 2 bh(x) 1 internal nodes.

68 Proof (cont.) According to property 4, at least the nodes on any simple path from the root to a leaf, not including the root, must be black. Consequently, the black-height of the root must be at least h/2; thus, n 2 h/ h 2lg(n +1) 1).

69 Left rotation LEFT-ROTATE(T, x) ) x 1. y right[x] y 2. right[x] left[y] α 3. p[left[y]] x β 4. p[y] p[x] γ 5. if p[x]= nil[t] 6. then root[t] y 7. else if x = left[p[x]] y 8. then left[p[x]] y x 9. else right[p[x]] y 10. left[y] x α β γ 11. p[x] y

70 RB-Insertion RB-INSERT(T, z) 1. y nil[t] 2. x root[t] [ ] 3. while x nil[t] 4. do y x 9. if y = nil[t] 5. if key[z] < key[x] 10. then root[t] z 6. then x left[x] 11. else if key[z] < key[y] 7. else x right[x] 8. p[z] y 12. then left[y] z 13. else right[y] z 14. left[z] nil[t] 15. right[z] nil[t] 16. color[z] RED 17. RB-INSERT-FIXUP(T, z)

71 RB-Insertion Which of the red-black properties can be violated upon the call to RB-INSERT-FIXUP?

72 RB-Insertion (case 1) Change color y z y z 4 Case 1: z's uncle y is red

73 RB-Insertion (case 2) Left rotation y 7 14 y 1 7 z 15 2 z Case 2: z's uncle y is black and z is a right child. Convert case 2 to 3.

74 RB-Insertion (case 3) Right rotation y 2 z 11 z Case 3: z's uncle y is black and z is a left child

75 RB-tree insertion z's father is lft left child z's father is right child Types Case 1L: z's zs uncle is red. Case 2L: z's uncle is black and z is right child. Case 3L: z's uncle is black and z is left child. Case 1R: z's uncle is red. Case 2R: z's zs uncle is black and z is left child. Case 3R: z's uncle is black and z is right child. Operation Change color. Left rotation, p(z). Right rotation, p(p(z)). Change color. Right rotation, p(z). Lft Left rotation, tti p(p(z)). ( (

76 RB-Insertion RB-INSERT-FIXUP(T, z) 1. while color[p[z]] = RED 2. do if p[z] =left[p[p[z]]] lf 3. then y right[p[p[z]]] 4. if color[y] = RED 5. then color[p[z]] BLACK 6. color[y] BLACK 7. color[p[p[z]]] RED 8. z p[p[z]] Case 1 Case 1 Case 1 Case 1

77 RB-Insertion 9. else if z = right[p[z]] 10. then z p[z] ] Case LEFT-ROTATION(T, z) Case color[p[z]] BLACK Case color[p[p[z]]] [ [ [ RED Case RIGHT-ROTATION(T, p[p[z]]) Case else (same as then clause with "right" and "left" exchanged) 16. color[root[t]] BLACK Running time: Running time: O(lgn)

78 RB-Example INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 z Change color 10 10

79 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 No change z 2 Node z s father Node z s father is black, so stop.

80 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 No change z 2 12 Node z s father Node z s father is black, so stop.

81 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Change color z 2 12 y z 4 Case 1L: z's uncle y is red and we get new z.

82 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Change color 10 z 10 z Node z is root, so stop

83 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Left rotation NIL y z Case 3R: z's uncle y is black and z is a right child.

84 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Change color Node z s father is black, so stop z 12 2 y z 8 8 Case 1R: z's uncle y is red and we get new z.

85 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 No change z Node z s father is black, so stop.

86 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Left rotation z z Case 3R: z's uncle y is black and z is a right child.

87 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Change color y z y z Case 1L: z's uncle y is red and we get new z. 7

88 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Left rotation y 8 12 y 2 8 z 4 z Case 2L: z's uncle y is black and z is a right child.

89 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Right rotation y z Case 3L: z's uncle y is black and z is a left child.

90 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 No change z Node z s father is black, so stop.

91 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 No change z Node z s father is black, so stop.

92 RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 No change z Node z s father is black, so stop.

93 RB-Deletion RB-DELETE(T, z) ) 1. if left[z] = nil[t] or right[z] = nil[t] 2. then y z 3. else y TREE-SUCCESSOR(z) 4. if left[y] nil[t] 5. then x left[y] 6. else x right[y] 10. else if y = left[p[y]] 7. p[x] [ ] p[y] [ ] 11. then left[p[y]] x 8. if p[y] = nil[t] 12. else right[p[y]] x 9. then root[t] x 13. if y z 14. then key[z] key[y] 15. if color[y] = BLACK 16. then RB-DELETE-FIXUP(T, x) ) 17. return y

94 RB-Deletion Which of the red-black properties can be violated upon the call to RB-DELETE-FIXUP?

95 RB-Deletion (case 1) B D D A C E w x B A C w E x C E β α ζ A C w β x ζ ε δ γ ζ ε β α δ γ Case 1: x's sibling w is red. Convert case 1 to 2, 3, or 4.,,

96 RB-Deletion (case 2) B x B D A C E D A C E w x C E β α ζ C E β α ζ δ γ ζ ε δ γ ζ ε Case 2: x's sibling w is black, and both of w's children are black. Get the new node x.

97 RB-Deletion (case 3) B B C A D D A C E w x x w E D β α γ C E β α ζ E δ δ γ ζ ε Case 3: x's sibling w is black, and w's left children is red, and w's right child is black. ζ ε, g Convert case 3 to 4.

98 RB-Deletion (case 4) B D D A C E w x B A C E C E β α ζ A C β ζ ε δ γ ζ ε β α δ γ x = root[t] Case 4: x's sibling w is black, and w's right child is red. Terminate the while loop.

99 RB-tree deletion z is left child z is right child Types Operation Case 1L: x's sibling w is red. Case 2L: x's sibling w is black and both of w's children are black. Case 3L: x's sibling w is black, and w's left children is red, and w's right child is black. Case 4L: x's sibling w is black, and w's right child is red. Case 1R: x's sibling w is red. Case 2R: x's sibling w is black and both of w's children are black. Case 3R: x's sibling w is black, and w's right children is red, and ws w's left child is black. Case 4R: x's sibling w is black, and w's left child is red. Left rotation, p(x). Change color. Right rotation, w. Left rotation, p(x). Right rotation, p(x). Change color. Left rotation, w. Right rotation, p(x).

100 RB-DELETE RB-DELETE-FIXUP(T, z) 1. while x root[t] and color[x]=black 2. do if x = lf left[p[x]] 3. then w right[p[x]] 4. if color[w] = RED 5. then color[w] BLACK Case 1 6. color[p[x]] RED Case 1 7. LEFT-ROTATION(T, p[x]) Case 1 8. w right[p[z]] Case 1 9. if color[left[w]] lf = BLACK and color[right[w]] = BLACK 10. then color[w] RED Case x p[x] Case 2

101 RB-Deletion 12. else if color[right[w]] = BLACK 13. then color[left[w]] [ [ BLACK Case color[w] RED Case RIGHT-ROTATION(T, w) Case w right[p[x]] ih[ [ Case color[w] color[p[x]] Case color[p[x]] BLACK Case color[right[w]] BLACK Case LEFT-ROTATION(T, p[x]) Case x root[t] Case else (same as then clause with "right" and "left" exchanged) 23. color[x] BLACK

102 Analysis of RB-Deletion What is the running time of RB-DELETE? Running time: O(lgn)

103 RB-Example Dl Delete 3 Delete z Node z is red.

104 RB-Example Dl Delete 2 Delete z x

105 RB-Example Dl Delete 2 Chang color x x Node x is red.

106 RB-Example Dl Delete 9 Delete z x NIL 12 w

107 RB-Example Dl Delete 9 Right rotation x w 1 6 NIL NIL 11 x w NIL Case 3L: x's sibling w is black and w's left child Case 3L: x s sibling w is black, and w s left child is red and w's right child is black.

108 RB-Example Dl Delete 9 Left rotation x w 1 6 NIL x w Case 4L: x's sibling w is black and w's right child Case 4L: x s sibling w is black, and w s right child is red.

109 RB-Example Dl Delete 4 Exchange z z 7 Which is 4's successor?

110 RB-Example Dl Delete 4 Delete z 7 7 Node z is red.

111 RB-Example Dl Delete 10 Delete z NIL 12 x w 7 7

112 RB-Example Dl Delete 10 Change color x x w 1 6 NIL NIL 12 7 NIL NIL 7 Case 2L: x's sibling w is black and both of w's Case 2L: x s sibling w is black, and both of w s children are black. Then, we get new x.

113 RB-Example Dl Delete 10 Change color x 5 11 x Node x is red. 7

114 RB-Example Dl Delete 11 Delete z 5 12 x

115 RB-Example Dl Delete 11 Change color x 5 12 x Node x is red. 7

116 RB-Example Dl Delete 8 Exchange 8 z z Which is 8's successor?

117 RB-Example Dl Delete 8 Delete z 5 w NIL x

118 RB-Example Dl Delete 8 Right rotation w NIL x w 6 NIL x 7 7 Case 1R: x's sibling w is red.

119 RB-Example Dl Delete 8 Left rotation w 6 NIL x w 7 NIL x NIL 7 6 Case 3R: x's sibling w is black and w'sright Case 3R: x s sibling w is black, and w s right child is red and w's left child is black.

120 RB-Example Dl Delete 8 Right rotation w 7 NIL x Case 4R: x's sibling w is black and w's left child Case 4R: x s sibling w is black, and w s left child is red.

121 Typical disk drive

122 B-tree root[t] P C G M T X A B D E F J K L N O Q R S U V Y Z The minimum degree for this B-tree is t = 2, every node other than the root must have at least 1 keys and every node can contain at most 3 keys (2-3-4 tree)

123 B-tree (1000 keys) root[t] Each internal node and leaf contains 1000 keys.

124 Definition of B-trees A B-tree T haves the following properties: 1. Every node x has the following fields: n[x], the number of keys currently stored in node x; the n[x] keys themselves, stored in nondecreasing order, so that key 1 [x] key 2 [x] key n[x] [x]; leaf[x], a boolean value that is TRUE if x is a leaf and FALSE if x is an internal node. 2. Each internal node x also contains n[x] [ ] + 1 has the pointers c 1 [x], c 2 [x],, c n[x] + 1 [x] to its children. Leaf nodes have no children, so their c i fields are undefined.

125 Definition of B-trees 3. The keys key i [x] separate ranges of keys stored in each subtree: if k i is any key stored in the subtree with root c i [x], then k 1 key 1 [x] k 2 key 2 [x] key n[x] [x] k n[x] All leaves have the same depth, which is the tree's height h.

126 Definition of B-trees 5. There are lower and upper bounds on the number of key a node can contain (t 2, minimum degree) Every node other than the root must have at least t 1 keys. Every internal node other than the root thus has at least t children; Every node can contain at most 2t 1 keys. An internal node can have at most 2t children. Theorem. If n 1, then for any n-key B-tree T of height h and minimum degree t 2, h log t n.

127 Searching a B-tree SEARCH K root[t] P C G M T X A B D E F J K L N O Q R S U V Y Z

128 Searching a B-tree SEARCH K root[t] P C G M T X A B D E F J K L N O Q R S U V Y Z

129 Splitting (B-tree) Try to INSERT T R X P Q S U V Y Z Minimum degree t = 2

130 Splitting (B-tree) Try to INSERT T R X R U X P Q S U V Y Z P Q S V Y Z Number of keys = 3 Minimum degree t = 2

131 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. Minimum degree t = 2

132 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. F Minimum degree t = 2

133 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. F S Minimum degree t = 2

134 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. F Q S Minimum degree t = 2

135 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. F Q S Q Splitting #keys = 3 F S Case 1: current node is root and has 3 keys. Minimum degree t = 2

136 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. Q Insert K Q F S F K S #keys < 3 Case 2: current node has at most 2 keys and the appropriate subtree has at most 2 keys. Minimum degree t = 2

137 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. Q Insert C Q F K S C F K S #keys < 3 Case 2: current node has at most 2 keys and the appropriate subtree has at most 2 keys. Minimum degree t = 2

138 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. Q Splitting F Q C F K S C K S #keys = 3 Case 3: current node has at most 2 keys and the appropriate subtree has 3 keys. Minimum degree t = 2

139 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. F Q Insert L F Q C K S C K L S #keys < 3 Case 4: the appropriate subtree has at most 2 keys (after case 3). Minimum degree t = 2

140 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. F Q Insert H F Q C K L S C H K L S #keys < 3 Case 2: current node has at most 2 keys and the appropriate subtree has at most 2 keys. Minimum degree t = 2

141 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. F Q Insert T F Q C H K L S C H K L S T #keys < 3 Case 2: current node has at most 2 keys and the appropriate subtree has at most 2 keys. Minimum degree t = 2

142 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. F Q Insert V F Q C H K L S T C H K L S T V #keys < 3 Case 2: current node has at most 2 keys and the appropriate subtree has at most 2 keys. Minimum degree t = 2

143 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. F Q Splitting F Q T C H K L S T V C H K L S V #keys = 3 Case 3: current node has at most 2 keys and the appropriate subtree has 3 keys. Minimum degree t = 2

144 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. F Q T F Q Insert W T C H K L S V C H K L S V W #keys < 3 Case 4: the appropriate subtree has at most 2 keys (after case 3). Minimum degree t = 2

145 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. #keys = 3 F Q T Q Splitting C H K L S V W F T Increase height C H K L S V W Case 1: current node is root and has 3 keys. Minimum degree t = 2

146 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. #keys < 3 Q F T C H K L S V W Minimum degree t = 2

147 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. Q Splitting Q #keys = 3 F T F K T C H K L S V W C H L S V W Case 3: current node has at most 2 keys and the appropriate subtree has 3 keys. Minimum degree t = 2

148 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. Q Insert M Q #keys < 3 F K T F K T C H L S V W C H L M S V W Case 4: the appropriate subtree has at most 2 keys (after case 3). Minimum degree t = 2

149 Insertion (B-tree) INSERT F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E. F Q B K T W A C D E H L N P R S V X Y Z

150 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 #keys > 1 F Q B K T W A C D E H L N P R S V X Y Z

151 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 F Q #keys > 1 B K T W A C D E H L N P R S V X Y Z

152 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 F Q Delete Y F Q B K T W B K T W A C D E H L N P R S V X Y Z A C D E H L N P R S V X Z #keys > 1 Case 1: key k = Y is in a leaf.

153 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 #keys > 1 F Q B K T W A C D E H L N P R S V X Z

154 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 F Q #keys > 1 B K T W A C D E H L N P R S V X Z

155 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 F Q Exchange X and W Delete X F Q B K T W B K T X A C D E H L N P R S V X Z A C D E H L N P R S V Z Case 2-a: key k = W is in a internal node and one of its #keys > 1 children that precedes or follows k has at least 2 keys.

156 Deletion (B-tree) DELETE F other than W Minimum degree t = 2 #keys > 1 #keys = 1 F Q Merge Q B K T W B F K T W A C D E H L N P R S V X Z A C D E H L N P R S V X Z Case 2-b: key k = F is in a internal node and the both of its children that precedes or follows k only has 1 key.

157 Deletion (B-tree) DELETE F other than W Minimum degree t = 2 Q Exchange F and E Delete E Q B F K T W B E K T W A C D E H L N P R S V X Z A C D H L N P R S V X Z Case 2-a: key k = F is in a internal node and one of its #keys > 1 children that precedes or follows k has at least 2 keys.

158 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 F Q #keys > 1 Exchange Q and R Delete R F R B K T X B K T X A C D E H L N P R S V Z A C D E H L N P S V Z Which h is Q's successor?? It is R. Case 2-a: key k = Q is in a internal node and one of its children that precedes or follows k has at least 2 keys.

159 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 F R F R Merge B K T X B K T A C D E H L N P S V Z A C D E H L N P S V X Z Case 2-b: key k = X is in a internal node and the both of its #keys = 1 children that precedes or follows k only has 1 key.

160 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 F R F R Delete X B K T B K T A C D E H L N P S V X Z A C D E H L N P S V Z #keys > 1 Current node is { V, X, Z }

161 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 #keys = 1 F R Merge F B K T B K R T A C D E H L N P S V Z A C D E H L N P S V Z Case 3-b: key k = K is not present in a internal node and the appropriate subtree that must contain k has only 1 key and the subtree's immediate siblings have only 1 key.

162 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 B F Exchange K and L Delete L K R T B L R T F A C D E H L N P S V Z A C D E H N P S V Z Case 2-a: key k = K is in a internal node and one of its #keys > 1 children that precedes or follows k has at least 2 keys.

163 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 #keys = 1 #keys > 1 F Borrow one key from the sibling B L R T B F R T L A C D E H N P S V Z A C D E H N P S V Z Case 3-a: key k = B is not present in a internal node and the appropriate subtree that must contain k has only 1 key and one of the subtree's immediate siblings has at least 2 keys.

164 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 B L Exchange B and C Delete C F R T C F R T L A C D E H N P S V Z A D E H N P S V Z Case 2-a: key k = B is in a internal node and one of its #keys > 1 children that precedes or follows k has at least 2 keys.

165 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 C L Borrow one key from the sibling F R T C E R T L A D E H N P S V Z A D F H N P S V Z Case 3-a: key k = H is not present in a internal node and the #keys = 1 #keys > 1 appropriate subtree that must contain k has only 1 key and one of the subtree's immediate siblings has at least 2 keys.

166 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 L Delete H L C E R T C E R T A D F H N P S V Z A D F N P S V Z #keys > 1 Case 1: key k = H is in a leaf.

167 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 L #keys > 1 C E R T A D F N P S V Z

168 Deletion (B-tree) DELETE Y, W, Q, X, K, B, H, P Minimum degree t = 2 L Delete P L C E R T C E R T A D F N P S V Z A D F N S V Z #keys > 1 Case 1: key k = P is in a leaf.

169 B-tree Thinking and practice. Write code for B-TREE-SEARCH(x, k) Write code for B-TREE-SPLIT-CHILD(x, i, y) Write code for B-TREE-INSERT(T, k) Write code for B-TREE-DELETE(T, k) How about B+ tree?

170 Any yquestion? Xiaoqing i Zheng Fundan University

CISC 320 Introduction to Algorithms Spring 2014

CISC 320 Introduction to Algorithms Spring 2014 CISC 20 Introdution to Algorithms Spring 2014 Leture 9 Red-Blk Trees Courtes of Prof. Lio Li 1 Binr Serh Trees (BST) ke[x]: ke stored t x. left[x]: pointer to left hild of x. right[x]: pointer to right

More information

7 Dictionary. EADS c Ernst Mayr, Harald Räcke 109

7 Dictionary. EADS c Ernst Mayr, Harald Räcke 109 7 Dictionary Dictionary: S.insert(x): Insert an element x. S.delete(x): Delete the element pointed to by x. S.search(k): Return a pointer to an element e with key[e] = k in S if it exists; otherwise return

More information

CS 240 Data Structures and Data Management. Module 4: Dictionaries

CS 240 Data Structures and Data Management. Module 4: Dictionaries CS 24 Data Structures and Data Management Module 4: Dictionaries A. Biniaz A. Jamshidpey É. Schost Based on lecture notes by many previous cs24 instructors David R. Cheriton School of Computer Science,

More information

7 Dictionary. Harald Räcke 125

7 Dictionary. Harald Räcke 125 7 Dictionary Dictionary: S. insert(x): Insert an element x. S. delete(x): Delete the element pointed to by x. S. search(k): Return a pointer to an element e with key[e] = k in S if it exists; otherwise

More information

searching algorithms

searching algorithms searching algorithms learning objectives algorithms your software system software hardware learn what the searching problem is about learn two algorithms for solving this problem learn the importance of

More information

7 Dictionary. Ernst Mayr, Harald Räcke 126

7 Dictionary. Ernst Mayr, Harald Räcke 126 7 Dictionary Dictionary: S. insert(x): Insert an element x. S. delete(x): Delete the element pointed to by x. S. search(k): Return a pointer to an element e with key[e] = k in S if it exists; otherwise

More information

Data Structures and Algorithms " Search Trees!!

Data Structures and Algorithms  Search Trees!! Data Structures and Algorithms " Search Trees!! Outline" Binary Search Trees! AVL Trees! (2,4) Trees! 2 Binary Search Trees! "! < 6 2 > 1 4 = 8 9 Ordered Dictionaries" Keys are assumed to come from a total

More information

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ] 423 16. Binary Search Trees [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap. 12.1-12.3] Dictionary implementation 424 Hashing: implementation of dictionaries with expected very fast access times. Disadvantages

More information

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ] 418 16. Binary Search Trees [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap. 12.1-12.3] 419 Dictionary implementation Hashing: implementation of dictionaries with expected very fast access times. Disadvantages

More information

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk I. Yu, D. Karabeg INF2220: algorithms and data structures Series 1 Topic Function growth & estimation of running time, trees (Exercises with hints for solution)

More information

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM Search Trees Chapter 1 < 6 2 > 1 4 = 8 9-1 - Outline Ø Binary Search Trees Ø AVL Trees Ø Splay Trees - 2 - Outline Ø Binary Search Trees Ø AVL Trees Ø Splay Trees - 3 - Binary Search Trees Ø A binary search

More information

Advanced Implementations of Tables: Balanced Search Trees and Hashing

Advanced Implementations of Tables: Balanced Search Trees and Hashing Advanced Implementations of Tables: Balanced Search Trees and Hashing Balanced Search Trees Binary search tree operations such as insert, delete, retrieve, etc. depend on the length of the path to the

More information

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm 8 Priority Queues 8 Priority Queues A Priority Queue S is a dynamic set data structure that supports the following operations: S. build(x 1,..., x n ): Creates a data-structure that contains just the elements

More information

The null-pointers in a binary search tree are replaced by pointers to special null-vertices, that do not carry any object-data

The null-pointers in a binary search tree are replaced by pointers to special null-vertices, that do not carry any object-data Definition 1 A red black tree is a balanced binary search tree in which each internal node has two children. Each internal node has a color, such that 1. The root is black. 2. All leaf nodes are black.

More information

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn Priority Queue / Heap Stores (key,data) pairs (like dictionary) But, different set of operations: Initialize-Heap: creates new empty heap

More information

Splay Trees. CMSC 420: Lecture 8

Splay Trees. CMSC 420: Lecture 8 Splay Trees CMSC 420: Lecture 8 AVL Trees Nice Features: - Worst case O(log n) performance guarantee - Fairly simple to implement Problem though: - Have to maintain etra balance factor storage at each

More information

B- TREE. Michael Tsai 2017/06/06

B- TREE. Michael Tsai 2017/06/06 B- TREE Michael Tsai 2017/06/06 2 B- Tree Overview Balanced search tree Very large branching factor Height = O(log n), but much less than that of RB tree Usage: Large amount of data to be stored - - Partially

More information

Fixed-Universe Successor : Van Emde Boas. Lecture 18

Fixed-Universe Successor : Van Emde Boas. Lecture 18 Fixed-Universe Successor : Van Emde Boas Lecture 18 Fixed-universe successor problem Goal: Maintain a dynamic subset S of size n of the universe U = {0, 1,, u 1} of size u subject to these operations:

More information

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures Christian Scheideler SS 2018 23.04.2018 Chapter 3 1 Overview Basic data structures Search structures (successor searching) Dictionaries

More information

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques AVL Trees Manolis Koubarakis 1 AVL Trees We will now introduce AVL trees that have the property that they are kept almost balanced but not completely balanced. In this way we have O(log n) search time

More information

Chapter 5 Arrays and Strings 5.1 Arrays as abstract data types 5.2 Contiguous representations of arrays 5.3 Sparse arrays 5.4 Representations of

Chapter 5 Arrays and Strings 5.1 Arrays as abstract data types 5.2 Contiguous representations of arrays 5.3 Sparse arrays 5.4 Representations of Chapter 5 Arrays and Strings 5.1 Arrays as abstract data types 5.2 Contiguous representations of arrays 5.3 Sparse arrays 5.4 Representations of strings 5.5 String searching algorithms 0 5.1 Arrays as

More information

Search Trees. EECS 2011 Prof. J. Elder Last Updated: 24 March 2015

Search Trees. EECS 2011 Prof. J. Elder Last Updated: 24 March 2015 Search Trees < 6 2 > 1 4 = 8 9-1 - Outline Ø Binary Search Trees Ø AVL Trees Ø Splay Trees - 2 - Learning Outcomes Ø From this lecture, you should be able to: q Define the properties of a binary search

More information

Assignment 5: Solutions

Assignment 5: Solutions Comp 21: Algorithms and Data Structures Assignment : Solutions 1. Heaps. (a) First we remove the minimum key 1 (which we know is located at the root of the heap). We then replace it by the key in the position

More information

Each internal node v with d(v) children stores d 1 keys. k i 1 < key in i-th sub-tree k i, where we use k 0 = and k d =.

Each internal node v with d(v) children stores d 1 keys. k i 1 < key in i-th sub-tree k i, where we use k 0 = and k d =. 7.5 (a, b)-trees 7.5 (a, b)-trees Definition For b a an (a, b)-tree is a search tree with the following properties. all leaves have the same distance to the root. every internal non-root vertex v has at

More information

Priority queues implemented via heaps

Priority queues implemented via heaps Priority queues implemented via heaps Comp Sci 1575 Data s Outline 1 2 3 Outline 1 2 3 Priority queue: most important first Recall: queue is FIFO A normal queue data structure will not implement a priority

More information

Data Structures and Algorithm. Xiaoqing Zheng

Data Structures and Algorithm. Xiaoqing Zheng Data Structures and Algorithm Xiaoqing Zheng zhengxq@fudan.edu.cn Dictionary problem Dictionary T holding n records: x records key[x] Other fields containing satellite data Operations on T: INSERT(T, x)

More information

Functional Data Structures

Functional Data Structures Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakultät für Informatik Technische Universität München 2017-2-3 1 Part II Functional Data Structures 2 Chapter 1 Binary Trees 3 1 Binary Trees

More information

CS Data Structures and Algorithm Analysis

CS Data Structures and Algorithm Analysis CS 483 - Data Structures and Algorithm Analysis Lecture VII: Chapter 6, part 2 R. Paul Wiegand George Mason University, Department of Computer Science March 22, 2006 Outline 1 Balanced Trees 2 Heaps &

More information

Verified Analysis of Functional Data Structures

Verified Analysis of Functional Data Structures Verified Analysis of Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakultät für Informatik Technische Universität München 2017-8-6 1 Lecture 1 Introduction Lecture 2 Search Trees Lecture 3

More information

CS 151. Red Black Trees & Structural Induction. Thursday, November 1, 12

CS 151. Red Black Trees & Structural Induction. Thursday, November 1, 12 CS 151 Red Black Trees & Structural Induction 1 Announcements Majors fair tonight 4:30-6:30pm in the Root Room in Carnegie. Come and find out about the CS major, or some other major. Winter Term in CS

More information

Dictionary: an abstract data type

Dictionary: an abstract data type 2-3 Trees 1 Dictionary: an abstract data type A container that maps keys to values Dictionary operations Insert Search Delete Several possible implementations Balanced search trees Hash tables 2 2-3 trees

More information

Part III. Data Structures. Abstract Data Type. Dynamic Set Operations. Dynamic Set Operations

Part III. Data Structures. Abstract Data Type. Dynamic Set Operations. Dynamic Set Operations Abstract Data Type Part III Data Structures An abstract data type (ADT) is defined by an interface of operations or methods that can be performed and that have a defined behavior. The data types in this

More information

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts) Introduction to Algorithms October 13, 2010 Massachusetts Institute of Technology 6.006 Fall 2010 Professors Konstantinos Daskalakis and Patrick Jaillet Quiz 1 Solutions Quiz 1 Solutions Problem 1. We

More information

Fundamental Algorithms

Fundamental Algorithms Fundamental Algorithms Chapter 5: Searching Michael Bader Winter 2014/15 Chapter 5: Searching, Winter 2014/15 1 Searching Definition (Search Problem) Input: a sequence or set A of n elements (objects)

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms Design and Analysis of Algorithms 6.046J/18.401J LECTURE 7 Skip Lists Data structure Randomized insertion With high probability (w.h.p.) bound 7/10/15 Copyright 2001-8 by Leiserson et al L9.1 Skip lists

More information

Advanced Data Structures

Advanced Data Structures Simon Gog gog@kit.edu - Simon Gog: KIT University of the State of Baden-Wuerttemberg and National Research Center of the Helmholtz Association www.kit.edu Predecessor data structures We want to support

More information

7 Dictionary. 7.1 Binary Search Trees. Binary Search Trees: Searching. 7.1 Binary Search Trees

7 Dictionary. 7.1 Binary Search Trees. Binary Search Trees: Searching. 7.1 Binary Search Trees 7 Dictionary Dictionary: S.insert(): Insert an element. S.delete(): Delete the element pointed to by. 7.1 Binary Search Trees An (internal) binary search tree stores the elements in a binary tree. Each

More information

Part I. Efficient Algorithms and Data Structures. Part I. Organizational Matters. WS 2011/12 Organizational Matters. Harald Räcke. Winter Term 2011/12

Part I. Efficient Algorithms and Data Structures. Part I. Organizational Matters. WS 2011/12 Organizational Matters. Harald Räcke. Winter Term 2011/12 Part I WS / Organizational Matters Efficient Algorithms and Data Structures Harald Räcke Fakultät für Informatik TU München http://www4.in.tum.de/lehre/ws/ea/ Winter Term / c Ernst Mayr, Harald Räcke c

More information

Dictionary: an abstract data type

Dictionary: an abstract data type 2-3 Trees 1 Dictionary: an abstract data type A container that maps keys to values Dictionary operations Insert Search Delete Several possible implementations Balanced search trees Hash tables 2 2-3 trees

More information

Advanced Data Structures

Advanced Data Structures Simon Gog gog@kit.edu - Simon Gog: KIT The Research University in the Helmholtz Association www.kit.edu Predecessor data structures We want to support the following operations on a set of integers from

More information

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17 Fibonacci (Min-)Heap A forest of heap-order trees (parent priority child priority). Roots in circular doubly-linked list. Pointer to minimum-priority root. Siblings in circular doubly-linked list; parent

More information

Binary Search Trees. Motivation

Binary Search Trees. Motivation Binary Search Trees Motivation Searching for a particular record in an unordered list takes O(n), too slow for large lists (databases) If the list is ordered, can use an array implementation and use binary

More information

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3 MA008 p.1/37 MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3 Dr. Markus Hagenbuchner markus@uow.edu.au. MA008 p.2/37 Exercise 1 (from LN 2) Asymptotic Notation When constants appear in exponents

More information

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October Contents 1 Cartesian Tree. Definition. 1 2 Cartesian Tree. Construction 1 3 Cartesian Tree. Operations. 2 3.1 Split............................................

More information

Heaps and Priority Queues

Heaps and Priority Queues Heaps and Priority Queues Motivation Situations where one has to choose the next most important from a collection. Examples: patients in an emergency room, scheduling programs in a multi-tasking OS. Need

More information

Graphs and Trees Binary Search Trees AVL-Trees (a,b)-trees Splay-Trees. Search Trees. Tobias Lieber. April 14, 2008

Graphs and Trees Binary Search Trees AVL-Trees (a,b)-trees Splay-Trees. Search Trees. Tobias Lieber. April 14, 2008 Search Trees Tobias Lieber April 14, 2008 Tobias Lieber Search Trees April 14, 2008 1 / 57 Graphs and Trees Binary Search Trees AVL-Trees (a,b)-trees Splay-Trees Tobias Lieber Search Trees April 14, 2008

More information

Solution suggestions for examination of Logic, Algorithms and Data Structures,

Solution suggestions for examination of Logic, Algorithms and Data Structures, Department of VT12 Software Engineering and Managment DIT725 (TIG023) Göteborg University, Chalmers 24/5-12 Solution suggestions for examination of Logic, Algorithms and Data Structures, Date : April 26,

More information

Fundamental Algorithms

Fundamental Algorithms Fundamental Algithms Chapter 4: AVL Trees Jan Křetínský Winter 2016/17 Chapter 4: AVL Trees, Winter 2016/17 1 Part I AVL Trees (Adelson-Velsky and Landis, 1962) Chapter 4: AVL Trees, Winter 2016/17 2 Binary

More information

Inge Li Gørtz. Thank you to Kevin Wayne for inspiration to slides

Inge Li Gørtz. Thank you to Kevin Wayne for inspiration to slides 02110 Inge Li Gørtz Thank you to Kevin Wayne for inspiration to slides Welcome Inge Li Gørtz. Reverse teaching and discussion of exercises: 3 teaching assistants 8.00-9.15 Group work 9.15-9.45 Discussions

More information

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes. Analysis of Algorithms Fibonacci Heaps Andres Mendez-Vazquez October 29, 2015 1 / 119 Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci

More information

Review Of Topics. Review: Induction

Review Of Topics. Review: Induction Review Of Topics Asymptotic notation Solving recurrences Sorting algorithms Insertion sort Merge sort Heap sort Quick sort Counting sort Radix sort Medians/order statistics Randomized algorithm Worst-case

More information

CSE 202: Design and Analysis of Algorithms Lecture 3

CSE 202: Design and Analysis of Algorithms Lecture 3 CSE 202: Design and Analysis of Algorithms Lecture 3 Instructor: Kamalika Chaudhuri Announcement Homework 1 out Due on Tue Jan 24 in class No late homeworks will be accepted Greedy Algorithms Direct argument

More information

Ordered Dictionary & Binary Search Tree

Ordered Dictionary & Binary Search Tree Ordered Dictionary & Binary Search Tree Finite map from keys to values, assuming keys are comparable (). insert(k, v) lookup(k) aka find: the associated value if any delete(k) (Ordered set: No values;

More information

16. Binary Search Trees

16. Binary Search Trees Dictionary imlementation 16. Binary Search Trees [Ottman/Widmayer, Ka..1, Cormen et al, Ka. 1.1-1.] Hashing: imlementation of dictionaries with exected very fast access times. Disadvantages of hashing:

More information

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February) Algorithms and Data Structures 016 Week 5 solutions (Tues 9th - Fri 1th February) 1. Draw the decision tree (under the assumption of all-distinct inputs) Quicksort for n = 3. answer: (of course you should

More information

16. Binary Search Trees

16. Binary Search Trees Dictionary imlementation 16. Binary Search Trees [Ottman/Widmayer, Ka..1, Cormen et al, Ka. 12.1-12.] Hashing: imlementation of dictionaries with exected very fast access times. Disadvantages of hashing:

More information

Problem: Data base too big to fit memory Disk reads are slow. Example: 1,000,000 records on disk Binary search might take 20 disk reads

Problem: Data base too big to fit memory Disk reads are slow. Example: 1,000,000 records on disk Binary search might take 20 disk reads B Trees Problem: Data base too big to fit memory Disk reads are slow Example: 1,000,000 records on disk Binary search might take 20 disk reads Disk reads are done in blocks Example: One block read can

More information

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20. Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20. Princeton University COS 4 Theory of Algorithms Spring 2002 Kevin Wayne Priority Queues Operation mae-heap insert find- delete- union

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 5: Divide and Conquer (Part 2) Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ A Lower Bound on Convex Hull Lecture 4 Task: sort the

More information

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003 University of New Mexico Department of Computer Science Midterm Examination CS 361 Data Structures and Algorithms Spring, 2003 Name: Email: Print your name and email, neatly in the space provided above;

More information

04 - Treaps. Dr. Alexander Souza

04 - Treaps. Dr. Alexander Souza Algorths Theory 04 - Treaps Dr. Alexander Souza The dctonary proble Gven: Unverse (U,

More information

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd 4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd Data Compression Q. Given a text that uses 32 symbols (26 different letters, space, and some punctuation characters), how can we

More information

7.3 AVL-Trees. Definition 15. Lemma 16. AVL-trees are binary search trees that fulfill the following balance condition.

7.3 AVL-Trees. Definition 15. Lemma 16. AVL-trees are binary search trees that fulfill the following balance condition. Definition 15 AVL-trees are binary search trees that fulfill the following balance condition. F eery node height(left sub-tree()) height(right sub-tree()) 1. Lemma 16 An AVL-tree of height h contains at

More information

Analysis of Algorithms - Using Asymptotic Bounds -

Analysis of Algorithms - Using Asymptotic Bounds - Analysis of Algorithms - Using Asymptotic Bounds - Andreas Ermedahl MRTC (Mälardalens Real-Time Research Center) andreas.ermedahl@mdh.se Autumn 004 Rehersal: Asymptotic bounds Gives running time bounds

More information

Amortized analysis. Amortized analysis

Amortized analysis. Amortized analysis In amortized analysis the goal is to bound the worst case time of a sequence of operations on a data-structure. If n operations take T (n) time (worst case), the amortized cost of an operation is T (n)/n.

More information

Tutorial Session 5. Discussion of Exercise 4, Preview on Exercise 5, Preparation for Midterm 1. Running Time Analysis, Asymptotic Complexity

Tutorial Session 5. Discussion of Exercise 4, Preview on Exercise 5, Preparation for Midterm 1. Running Time Analysis, Asymptotic Complexity Tutorial Session 5 Tuesday, 19 th of March 2019 Discussion of Exercise 4, Preview on Exercise 5, Preparation for Midterm 1 Running Time Analysis, Asymptotic Complexity 16.15 17.45 / 18.15 19.45 BIN 0.B.06

More information

Week 5: Quicksort, Lower bound, Greedy

Week 5: Quicksort, Lower bound, Greedy Week 5: Quicksort, Lower bound, Greedy Agenda: Quicksort: Average case Lower bound for sorting Greedy method 1 Week 5: Quicksort Recall Quicksort: The ideas: Pick one key Compare to others: partition into

More information

Slides for CIS 675. Huffman Encoding, 1. Huffman Encoding, 2. Huffman Encoding, 3. Encoding 1. DPV Chapter 5, Part 2. Encoding 2

Slides for CIS 675. Huffman Encoding, 1. Huffman Encoding, 2. Huffman Encoding, 3. Encoding 1. DPV Chapter 5, Part 2. Encoding 2 Huffman Encoding, 1 EECS Slides for CIS 675 DPV Chapter 5, Part 2 Jim Royer October 13, 2009 A toy example: Suppose our alphabet is { A, B, C, D }. Suppose T is a text of 130 million characters. What is

More information

MIDTERM Fundamental Algorithms, Spring 2008, Professor Yap March 10, 2008

MIDTERM Fundamental Algorithms, Spring 2008, Professor Yap March 10, 2008 INSTRUCTIONS: MIDTERM Fundamental Algorithms, Spring 2008, Professor Yap March 10, 2008 0. This is a closed book exam, with one 8 x11 (2-sided) cheat sheet. 1. Please answer ALL questions (there is ONE

More information

Discrete Mathematics

Discrete Mathematics Discrete Mathematics Trees H. Turgut Uyar Ayşegül Gençata Yayımlı Emre Harmancı 2001-2016 License You are free to: c 2001-2016 T. Uyar, A. Yayımlı, E. Harmancı Share copy and redistribute the material

More information

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing 1 Splay Trees Sleator and Tarjan, Self Adjusting Binary Search Trees JACM 32(3) 1985 The claim planning ahead. But matches previous idea of being lazy, letting potential build up, using it to pay for expensive

More information

Lecture 5: Splay Trees

Lecture 5: Splay Trees 15-750: Graduate Algorithms February 3, 2016 Lecture 5: Splay Trees Lecturer: Gary Miller Scribe: Jiayi Li, Tianyi Yang 1 Introduction Recall from previous lecture that balanced binary search trees (BST)

More information

Problem. Maintain the above set so as to support certain

Problem. Maintain the above set so as to support certain Randomized Data Structures Roussos Ioannis Fundamental Data-structuring Problem Collection {S 1,S 2, } of sets of items. Each item i is an arbitrary record, indexed by a key k(i). Each k(i) is drawn from

More information

Data Structures and Algorithm. Xiaoqing Zheng

Data Structures and Algorithm. Xiaoqing Zheng Data Structures and Algorithm Xiaoqing Zheng zhengxq@fudan.edu.cn MULTIPOP top[s] = 6 top[s] = 2 3 2 8 5 6 5 S MULTIPOP(S, x). while not STACK-EMPTY(S) and k 0 2. do POP(S) 3. k k MULTIPOP(S, 4) Analysis

More information

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example Recurrence Relations Chapter 2 Divide and Conquer Equation or an inequality that describes a function by its values on smaller inputs. Recurrence relations arise when we analyze the running time of iterative

More information

Insert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park

Insert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park 1617 Preview Data Structure Review COSC COSC Data Structure Review Linked Lists Stacks Queues Linked Lists Singly Linked List Doubly Linked List Typical Functions s Hash Functions Collision Resolution

More information

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26 Binary Search Introduction Problem Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26 Strategy 1: Random Search Randomly select a page until the page containing

More information

Objec&ves. Review. Data structure: Heaps Data structure: Graphs. What is a priority queue? What is a heap?

Objec&ves. Review. Data structure: Heaps Data structure: Graphs. What is a priority queue? What is a heap? Objec&ves Data structure: Heaps Data structure: Graphs Submit problem set 2 1 Review What is a priority queue? What is a heap? Ø Proper&es Ø Implementa&on What is the process for finding the smallest element

More information

Data selection. Lower complexity bound for sorting

Data selection. Lower complexity bound for sorting Data selection. Lower complexity bound for sorting Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 12 1 Data selection: Quickselect 2 Lower complexity bound for sorting 3 The

More information

10 van Emde Boas Trees

10 van Emde Boas Trees Dynamic Set Data Structure S: S. insert(x) S. delete(x) S. search(x) S. min() S. max() S. succ(x) S. pred(x) Ernst Mayr, Harald Räcke 389 For this chapter we ignore the problem of storing satellite data:

More information

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th CSE 3500 Algorithms and Complexity Fall 2016 Lecture 10: September 29, 2016 Quick sort: Average Run Time In the last lecture we started analyzing the expected run time of quick sort. Let X = k 1, k 2,...,

More information

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem CS 577 Introduction to Algorithms: Jin-Yi Cai University of Wisconsin Madison In the last class, we described InsertionSort and showed that its worst-case running time is Θ(n 2 ). Check Figure 2.2 for

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Spring 2017-2018 Outline 1 Sorting Algorithms (contd.) Outline Sorting Algorithms (contd.) 1 Sorting Algorithms (contd.) Analysis of Quicksort Time to sort array of length

More information

Lecture 4. Quicksort

Lecture 4. Quicksort Lecture 4. Quicksort T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo choo@skku.edu Copyright 2000-2018 Networking

More information

CPSC 320 Sample Final Examination December 2013

CPSC 320 Sample Final Examination December 2013 CPSC 320 Sample Final Examination December 2013 [10] 1. Answer each of the following questions with true or false. Give a short justification for each of your answers. [5] a. 6 n O(5 n ) lim n + This is

More information

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018 CS17 Integrated Introduction to Computer Science Klein Contents Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018 1 Tree definitions 1 2 Analysis of mergesort using a binary tree 1 3 Analysis of

More information

AVL trees. AVL trees

AVL trees. AVL trees Dnamic set DT dnamic set DT is a structure tat stores a set of elements. Eac element as a (unique) ke and satellite data. Te structure supports te following operations. Searc(S, k) Return te element wose

More information

Advanced Algorithms (2IL45)

Advanced Algorithms (2IL45) Technische Universiteit Eindhoven University of Technology Course Notes Fall 2013 Advanced Algorithms (2IL45) Mark de Berg Contents 1 Introduction to randomized algorithms 4 1.1 Some probability-theory

More information

Algorithms Theory. 08 Fibonacci Heaps

Algorithms Theory. 08 Fibonacci Heaps Algorithms Theory 08 Fibonacci Heaps Prof. Dr. S. Albers Priority queues: operations Priority queue Q Operations: Q.initialize(): initializes an empty queue Q Q.isEmpty(): returns true iff Q is empty Q.insert(e):

More information

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case Outline Computer Science Average Case Analysis: Binary Search Trees Mike Jacobson Department of Computer Science University of Calgary Lecture #7 Motivation and Objective Definition 4 Mike Jacobson (University

More information

U ovom dijelu upoznat ćemo strukturu podataka stablo, uvesti osnovnu terminologiju, implementaciju i algoritme nad tom strukturom.

U ovom dijelu upoznat ćemo strukturu podataka stablo, uvesti osnovnu terminologiju, implementaciju i algoritme nad tom strukturom. POGLAVLJE Stabla U ovom dijelu upoznat ćemo strukturu podataka stablo, uvesti osnovnu terminologiju, implementaciju i algoritme nad tom strukturom..1 Stabla Stabla su vrlo fleksibilna nelinearna struktura

More information

CS361 Homework #3 Solutions

CS361 Homework #3 Solutions CS6 Homework # Solutions. Suppose I have a hash table with 5 locations. I would like to know how many items I can store in it before it becomes fairly likely that I have a collision, i.e., that two items

More information

Introduction to Algorithms and Data Structures. Markus Bläser Saarland University Draft Thursday 22, 2015 and forever

Introduction to Algorithms and Data Structures. Markus Bläser Saarland University Draft Thursday 22, 2015 and forever Introduction to Algorithms and Data Structures Markus Bläser Saarland University Draft Thursday 22, 2015 and forever Contents 1 Introduction 1 1.1 Binary search........................... 1 1.2 Machine

More information

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort Sorting Algorithms We have already seen: Selection-sort Insertion-sort Heap-sort We will see: Bubble-sort Merge-sort Quick-sort We will show that: O(n log n) is optimal for comparison based sorting. Bubble-Sort

More information

AVL Trees. Properties Insertion. October 17, 2017 Cinda Heeren / Geoffrey Tien 1

AVL Trees. Properties Insertion. October 17, 2017 Cinda Heeren / Geoffrey Tien 1 AVL Trees Properties Insertion October 17, 2017 Cinda Heeren / Geoffrey Tien 1 BST rotation g g e h b h b f i a e i a c c f d d Rotations preserve the in-order BST property, while altering the tree structure

More information

Binary Search Trees. Dictionary Operations: Additional operations: find(key) insert(key, value) erase(key)

Binary Search Trees. Dictionary Operations: Additional operations: find(key) insert(key, value) erase(key) Binary Search Trees Dictionary Operations: find(key) insert(key, value) erase(key) Additional operations: ascend() get(index) (indexed binary search tree) delete(index) (indexed binary search tree) Complexity

More information

CS60007 Algorithm Design and Analysis 2018 Assignment 1

CS60007 Algorithm Design and Analysis 2018 Assignment 1 CS60007 Algorithm Design and Analysis 2018 Assignment 1 Palash Dey and Swagato Sanyal Indian Institute of Technology, Kharagpur Please submit the solutions of the problems 6, 11, 12 and 13 (written in

More information

1. Introduction Bottom-Up-Heapsort is a variant of the classical Heapsort algorithm due to Williams ([Wi64]) and Floyd ([F64]) and was rst presented i

1. Introduction Bottom-Up-Heapsort is a variant of the classical Heapsort algorithm due to Williams ([Wi64]) and Floyd ([F64]) and was rst presented i A Tight Lower Bound for the Worst Case of Bottom-Up-Heapsort 1 by Rudolf Fleischer 2 Keywords : heapsort, bottom-up-heapsort, tight lower bound ABSTRACT Bottom-Up-Heapsort is a variant of Heapsort. Its

More information

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms Prof. Gregory Provan Department of Computer Science University College Cork 1 Lecture Outline CS 4407, Algorithms Growth Functions

More information

Binary Decision Diagrams. Graphs. Boolean Functions

Binary Decision Diagrams. Graphs. Boolean Functions Binary Decision Diagrams Graphs Binary Decision Diagrams (BDDs) are a class of graphs that can be used as data structure for compactly representing boolean functions. BDDs were introduced by R. Bryant

More information