Data Structures and Algorithm Xiaoqing Zheng zhengxq@fudan.edu.cn
Trees (max heap) 1 16 2 3 14 10 4 5 6 7 8 7 9 3 8 9 10 2 4 1 PARENT(i) return i /2 LEFT(i) return 2i RIGHT(i) return 2i +1 16 14 10 8 7 9 3 2 4 1
Binary trees root[t] / / / / / / / / / / / / / Not array!
Binary Search Tree Each node x has: key[x] Pointers: left[x] right[x] p[x] 9 5 12 1 6 7 8
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 9 5 12 1 6 7 for those keys unique? 8
No uniqueness 7 5 9 9 5 12 1 6 8 12 1 6 7 8
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.
Sort? 9 5 12 1 6 7 8
Sort? 9 5 12 1 6 7 8
Sort? 9 5 12 1 6 7 8
Sort? 9 5 12 1 6 7 8
Sort? 9 5 12 6 1 7 8
Sort? 9 5 12 6 1 5 7 8
Sort? 9 5 12 6 1 5 7 8
Sort? 9 5 12 6 1 5 6 7 8
Sort? 9 5 12 6 1 5 6 7 8 7 8
Sort? 9 12 1 5 6 7 8 9
Sort? 9 12 1 5 6 7 8 9
Sort? 1 5 6 7 8 9 12
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
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)
BST as a data structure Operations: 9 Insert(x) Delete(x) ( ) Search(k) 5 12 1 6 7 8
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) )
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.
Minimum and maximum TREE-MINIMUM(x) 1. while left[x] [ ] NIL 2. do x left[x] 3. return x 9 5 12 1 6 TREE-MAXIMUM(x) 1. while right[x] NIL 2. do x right[x] 7 3. return x 8
Successor and predecessor 15 Which is the node 15's successor 6 18 3 7 17 20 2 4 13 9
Successor and predecessor 15 6 18 3 7 17 20 2 4 9 13 Which h is the node 13's successor
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)
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 5 12 1 6 2 7 TREE-INSERT(T, 2) 8
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
Deletion (case 1) 15 15 5 16 5 16 3 12 20 3 12 20 10 13 18 23 10 18 23 6 z 6 7 z has no child. 7
Deletion (case 2) 15 15 5 16 z 5 20 3 12 20 3 12 18 23 10 13 18 23 10 6 6 7 z has one child. 7
Deletion (case 3) 15 15 z 5 16 6 16 y 3 12 20 3 12 20 10 13 18 23 10 13 18 23 6 y 7 7 z has two children.
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
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 1. 3 4 5
AVL trees 5 7 2 8 2 8 1 4 7 1 4 3 3 5 Which one is AVL tree?
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.
Single rotation B A A γ B α β α β γ Right rotation ti to fix case 1
Single rotation A B α β B γ α A β γ Lft Left rotation tti to fix case 4
Double rotation C A δ α B β γ Single rotation ti fails to fix case 2
Double rotation (first step) C C A δ B δ α B A γ β γ α β Lft Left rotation tti
Double rotation (second step) C B B δ A C A γ α β γ δ α β Right rotation ti
Double rotation C B A δ A C α B α β γ δ β γ Lft Left-right htdouble rotation ti to fix case 2
Double rotation A B α C A C B δ α β γ δ β γ Right-left lftdouble rotation tti to fix case 3
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
AVL tree example 3 3 2 Insert 2
AVL tree example (cont.) 3 2 2 1 3 1 Insert 1 Right rotation
AVL tree example (cont.) 2 2 1 3 1 4 4 3 5 5 Insert 4 and 5 Left rotation
AVL tree example (cont.) 2 4 1 4 2 5 3 5 1 3 6 6 Insert 6 Left rotation
AVL tree example (cont.) 4 4 2 5 2 6 1 3 6 1 3 5 7 7 Insert 7 Left rotation
AVL tree example (cont.) 4 4 2 6 2 6 1 3 5 7 1 3 5 15 16 7 16 15 Insert 16 and 15 Right-left rotation
AVL tree example (cont.) 4 4 2 6 2 7 1 3 5 15 1 3 6 15 7 16 14 16 5 14 Insert 14 Right-left rotation
AVL tree example (cont.) 4 7 2 7 4 15 1 3 6 15 2 6 14 16 5 14 16 1 3 5 13 13 Insert 13 Left rotation
AVL tree example (cont.) 7 7 4 15 4 15 2 6 14 16 2 6 13 16 1 3 5 13 1 3 5 12 14 12 Insert 12 Right rotation
AVL tree example (cont.) 7 7 4 15 4 13 2 6 13 16 2 6 12 15 1 3 5 12 14 1 3 5 11 14 16 11 Insert 11 Right rotation
AVL tree example (cont.) 7 4 13 7 4 13 2 6 12 15 2 6 11 15 1 3 5 11 14 16 1 3 5 10 12 14 16 10 Insert 10 Right rotation
AVL tree example (cont.) 7 4 13 7 4 13 2 6 11 15 2 6 11 15 1 3 5 10 12 14 16 1 3 5 9 12 14 16 8 8 10 9 Insert 8 and 9 Left-right rotation
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.
Example of a red-black tree 7 3 18 NIL NIL 10 22 8 11 NIL 26 NIL NIL NIL NIL NIL NIL
Example of a red-black tree 7 3 18 10 22 8 11 26 nil[t]
Height of a red-black tree
Height of a red-black tree
Height of a red-black tree
Height of a red-black tree
Height of a red-black tree
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.
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 = 2 0 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.
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/2-1 1. h 2lg(n +1) 1).
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
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)
RB-Insertion Which of the red-black properties can be violated upon the call to RB-INSERT-FIXUP?
RB-Insertion (case 1) Change color 11 11 2 14 2 14 y 1 7 15 1 7 z 15 5 8 y 5 8 4 z 4 Case 1: z's uncle y is red
RB-Insertion (case 2) Left rotation 11 11 2 14 y 7 14 y 1 7 z 15 2 z 8 15 5 8 1 5 4 4 Case 2: z's uncle y is black and z is a right child. Convert case 2 to 3.
RB-Insertion (case 3) Right rotation 11 7 7 14 y 2 z 11 z 2 8 15 1 5 8 14 1 5 4 15 4 Case 3: z's uncle y is black and z is a left child
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)). ( (
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
RB-Insertion 9. else if z = right[p[z]] 10. then z p[z] ] Case 2 11. LEFT-ROTATION(T, z) Case 2 12. color[p[z]] BLACK Case 3 13. color[p[p[z]]] [ [ [ RED Case 3 14. RIGHT-ROTATION(T, p[p[z]]) Case 3 15. else (same as then clause with "right" and "left" exchanged) 16. color[root[t]] BLACK Running time: Running time: O(lgn)
RB-Example INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 z Change color 10 10
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 No change 10 10 2 z 2 Node z s father Node z s father is black, so stop.
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 No change 10 10 2 12 z 2 12 Node z s father Node z s father is black, so stop.
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Change color 10 10 z 2 12 y 2 12 4 z 4 Case 1L: z's uncle y is red and we get new z.
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. 2 12 2 12 4 4
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Left rotation 10 10 2 12 4 12 NIL y 4 2 6 6 z Case 3R: z's uncle y is black and z is a right child.
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Change color 10 10 Node z s father is black, so stop. 4 12 4 z 12 2 y 6 2 6 z 8 8 Case 1R: z's uncle y is red and we get new z.
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 No change 10 10 4 12 4 12 2 6 2 6 z 1 8 1 8 Node z s father is black, so stop.
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Left rotation 10 10 4 12 4 12 2 6 2 8 1 8 1 z 6 9 9 z Case 3R: z's uncle y is black and z is a right child.
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Change color 10 10 4 12 4 12 y 2 8 2 8 z 1 6 9 y 1 6 9 7 z Case 1L: z's uncle y is red and we get new z. 7
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Left rotation 10 10 4 12 y 8 12 y 2 8 z 4 z 9 1 6 9 2 6 7 7 1 Case 2L: z's uncle y is black and z is a right child.
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 Right rotation 10 8 8 12 y 4 10 4 z 9 2 6 9 12 2 6 1 7 1 7 Case 3L: z's uncle y is black and z is a left child.
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 No change 8 8 4 10 4 10 2 6 9 12 2 6 9 12 1 3 7 1 3 7 z Node z s father is black, so stop.
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 No change 8 8 4 10 4 10 2 6 9 12 2 6 9 12 z 1 3 7 11 1 3 7 11 Node z s father is black, so stop.
RB-Example (cont.) INSERT 10, 2, 12, 4, 6, 8, 1, 9, 7, 3, 11, 5 No change 8 8 4 10 4 10 2 6 9 12 2 6 9 12 1 3 5 7 11 1 3 5 7 11 z Node z s father is black, so stop.
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
RB-Deletion Which of the red-black properties can be violated upon the call to RB-DELETE-FIXUP?
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.,,
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.
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.
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.
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).
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 2 11. x p[x] Case 2
RB-Deletion 12. else if color[right[w]] = BLACK 13. then color[left[w]] [ [ BLACK Case 3 14. color[w] RED Case 3 15. RIGHT-ROTATION(T, w) Case 3 16. w right[p[x]] ih[ [ Case 3 17. color[w] color[p[x]] Case 4 18. color[p[x]] BLACK Case 4 19. color[right[w]] BLACK Case 4 20. LEFT-ROTATION(T, p[x]) Case 4 21. x root[t] Case 4 22. else (same as then clause with "right" and "left" exchanged) 23. color[x] BLACK
Analysis of RB-Deletion What is the running time of RB-DELETE? Running time: O(lgn)
RB-Example Dl Delete 3 Delete 8 8 4 10 4 10 2 6 9 12 2 6 9 12 1 3 5 7 11 1 5 7 11 z Node z is red.
RB-Example Dl Delete 2 Delete 8 8 4 10 4 10 2 z 6 9 12 1 x 6 9 12 1 5 7 11 5 7 11
RB-Example Dl Delete 2 Chang color 8 8 4 10 4 10 1 x 6 9 12 1 x 6 9 12 5 7 11 5 7 11 Node x is red.
RB-Example Dl Delete 9 Delete 8 8 4 10 4 10 1 6 z 9 12 1 6 x NIL 12 w 5 7 11 5 7 11
RB-Example Dl Delete 9 Right rotation 8 8 4 10 4 10 x w 1 6 NIL 12 1 6 NIL 11 x w 5 7 11 NIL 5 7 12 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.
RB-Example Dl Delete 9 Left rotation 8 8 4 10 4 11 x w 1 6 NIL 11 1 6 10 12 x w 5 7 12 5 7 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.
RB-Example Dl Delete 4 Exchange 8 8 4 z 11 5 11 1 6 10 12 1 6 10 12 5 7 z 7 Which is 4's successor?
RB-Example Dl Delete 4 Delete 8 8 5 11 5 11 1 6 10 12 1 6 10 12 z 7 7 Node z is red.
RB-Example Dl Delete 10 Delete 8 8 5 11 5 11 1 6 z 10 12 1 6 NIL 12 x w 7 7
RB-Example Dl Delete 10 Change color 8 8 5 11 5 11 x x w 1 6 NIL 12 1 6 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.
RB-Example Dl Delete 10 Change color 8 8 5 11 x 5 11 x 1 6 12 1 6 12 7 Node x is red. 7
RB-Example Dl Delete 11 Delete 8 8 5 11 z 5 12 x 1 6 12 1 6 7 7
RB-Example Dl Delete 11 Change color 8 8 5 12 x 5 12 x 1 6 1 6 7 Node x is red. 7
RB-Example Dl Delete 8 Exchange 8 z 12 5 12 5 z 1 6 1 6 7 7 Which is 8's successor?
RB-Example Dl Delete 8 Delete 12 12 5 z 5 w NIL x 1 6 1 6 7 7
RB-Example Dl Delete 8 Right rotation 12 5 5 w NIL x 1 12 1 6 w 6 NIL x 7 7 Case 1R: x's sibling w is red.
RB-Example Dl Delete 8 Left rotation 5 5 1 12 1 12 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.
RB-Example Dl Delete 8 Right rotation 5 5 1 12 1 7 w 7 NIL x 6 12 6 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.
Typical disk drive
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)
B-tree (1000 keys) root[t] 1000 10001... 1000 1000 1000 10001 10001 10001... 1000 1000 1000 Each internal node and leaf contains 1000 keys.
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.
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] + 1 4. All leaves have the same depth, which is the tree's height h.
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.
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
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
Splitting (B-tree) Try to INSERT T R X P Q S U V Y Z Minimum degree t = 2
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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
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
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.
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.
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.
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.
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.
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 }
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.
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.
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.
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.
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.
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.
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
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.
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?
Any yquestion? Xiaoqing i Zheng Fundan University