Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC-310 Database Systems

Similar presentations


Presentation on theme: "CPSC-310 Database Systems"— Presentation transcript:

1 CPSC-310 Database Systems
Professor Jianer Chen Room 315C HRBB Lecture #20

2 B+Trees Support fast search Support range search
Support dynamic changes B+tree node of order n: p1 k1 p2 k2 …… pn kn pn+1 Notes #7

3 Delete in B+tree Basic idea:
Find the leaf L where the record r should be deleted; If L remains at least half-full after deleting r, then delete r, and return; Else consider the sibling L’ of L; If L’ is more than half-full, then move a record from L’ to L, and return; Else combine L and L’ into a single leaf; But now you need to consider the case of deleting a child from L’s parent … (recursively) Notes #7

4 Delete in B+tree Simple case: the node remains at least half-full after deletion. Re-distribute keys among siblings Coalesce with a sibling and delete a pointer from its father Notes #7

5 Delete in B+tree Simple case: the node remains at least half-full after deletion. Re-distribute keys among siblings Coalesce with a sibling and delete a pointer from its father Notes #7

6 Key re-distribution at leaves
p’ k’ p k p” k” i = (n+1)/2  1 t > (n+1)/2 p1 k1 … pi ki --- p* q1 h1 q2 h2 … qt ht --- q* Notes #7

7 Key re-distribution at leaves
p’ k’ p k p” k” i = (n+1)/2  1 t > (n+1)/2 p1 k1 … pi ki --- p* q1 h1 q2 h2 … qt ht --- q* p’ k’ p k p” k” p1 k1 … pi ki q1 h1 --- p* q2 h2 … qt ht --- q* Notes #7

8 Key re-distribution at leaves
p’ k’ p k p” k” i = (n+1)/2  1 t > (n+1)/2 p1 k1 … pi ki --- p* q1 h1 q2 h2 … qt ht --- q* p’ k’ p k p” k” h2 p1 k1 … pi ki q1 h1 --- p* q2 h2 … qt ht --- q* Notes #7

9 Key re-distribution at leaves: Delete 5
order n=3 Notes #7

10 Key re-distribution at leaves: Delete 5
order n=3 Delete(prt, 5) 105 10 40 3 5 10 20 35 40 50 Notes #7

11 Key re-distribution at leaves: Delete 5
order n=3 Delete(prt, 5) 105 10 40 3 5 10 20 35 40 50 Delete 5 Notes #7

12 Key re-distribution at leaves: Delete 5
order n=3 Delete(prt, 5) 105 10 40 3 5 10 20 35 40 50 Notes #7

13 Key re-distribution at leaves: Delete 5
order n=3 Delete(prt, 5) 105 10 40 3 5 10 20 35 40 50 Less than half-full !! Notes #7

14 Key re-distribution at leaves: Delete 5
order n=3 Delete(prt, 5) 105 10 40 3 5 10 20 35 40 50 Look at the sibling, which is more than half-full, so we can redistribute the keysa Notes #7

15 Key re-distribution at leaves: Delete 5
order n=3 Delete(prt, 5) 105 10 40 3 5 10 20 35 40 50 Look at the sibling, which is more than half-full, so we can redistribute the keysa Notes #7

16 Key re-distribution at leaves: Delete 5
order n=3 Delete(prt, 5) 105 10 40 3 5 10 20 35 40 50 Look at the sibling, which is more than half-full, so we can redistribute the keys Notes #7

17 Key re-distribution at leaves: Delete 5
order n=3 Delete(prt, 5) 105 10 40 redistribution 3 10 20 35 40 50 3 10 20 35 Notes #7

18 Key re-distribution at leaves: Delete 5
order n=3 Delete(prt, 5) 105 10 40 redistribution 20 3 10 20 35 40 50 3 10 20 35 Notes #7

19 Key re-distribution at leaves: Delete 5
order n=3 Delete(prt, 5) 105 20 10 40 redistribution 20 3 10 20 35 40 50 3 10 20 35 Notes #7

20 Key re-distribution at leaves: Delete 5
order n=3 105 20 40 3 10 20 35 40 50 Notes #7

21 Key re-distribution at nonleaves
Notes #7

22 Key re-distribution at nonleaves
p’ k’ p k p” k” i+1 = (n+1)/2  1 t+1> (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 q2 h2 … qt ht qt+1 - Notes #7

23 Key re-distribution at nonleaves
p’ k’ p k p” k” i+1 = (n+1)/2  1 t+1> (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 q2 h2 … qt ht qt+1 - p’ k’ p k p” k” p1 k1 … pi ki pi+1 q1 h1 q2 h2 … qt ht qt+1 - Notes #7

24 Key re-distribution at nonleaves
p’ k’ p k p” k” i+1 = (n+1)/2  1 t+1> (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 q2 h2 … qt ht qt+1 - p’ k’ p k p” k” p1 k1 … pi ki pi q1 q1 h1 q2 h2 … qt ht qt+1 - Notes #7

25 Key re-distribution at nonleaves
p’ k’ p k p” k” i+1 = (n+1)/2  1 t+1> (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 q2 h2 … qt ht qt+1 - p’ k’ p k p” k” p1 k1 … pi ki pi q1 ? q1 h1 q2 h2 … qt ht qt+1 - Notes #7

26 Key re-distribution at nonleaves
p’ k’ p k p” k” i+1 = (n+1)/2  1 t+1> (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 q2 h2 … qt ht qt+1 - p’ k’ p k p” k” p1 k1 … pi ki pi q1 ? q1 h1 q2 h2 … qt ht qt+1 - Notes #7

27 Key re-distribution at nonleaves
p’ k’ p k p” k” i+1 = (n+1)/2  1 t+1> (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 q2 h2 … qt ht qt+1 - p’ k’ p k p” k” p1 k1 … pi ki pi q1 k’ q1 h1 q2 h2 … qt ht qt+1 - Notes #7

28 Key re-distribution at nonleaves
p’ k’ p k p” k” i+1 = (n+1)/2  1 t+1> (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 q2 h2 … qt ht qt+1 - p’ k’ p k p” k” ? p1 k1 … pi ki pi q1 k’ q1 h1 q2 h2 … qt ht qt+1 - Notes #7

29 Key re-distribution at nonleaves
p’ k’ p k p” k” i+1 = (n+1)/2  1 t+1> (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 q2 h2 … qt ht qt+1 - p’ k’ p k p” k” ? p1 k1 … pi ki pi q1 k’ q1 h1 q2 h2 … qt ht qt+1 - Notes #7

30 Key re-distribution at nonleaves
p’ k’ p k p” k” i+1 = (n+1)/2  1 t+1> (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 q2 h2 … qt ht qt+1 - p’ k’ p k p” k” h1 p1 k1 … pi ki pi q1 k’ q1 h1 q2 h2 … qt ht qt+1 - Notes #7

31 Key re-distribution at nonleaves
p’ k’ p k p” k” i+1 = (n+1)/2  1 t+1> (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 q2 h2 … qt ht qt+1 - p’ k’ p k p” k” h1 p1 k1 … pi ki pi q1 k’ q2 h2 … qt ht qt+1 Notes #7

32 Key re-distribution at nonleaves
p’ k’ p k p” k” i+1 = (n+1)/2  1 t+1> (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 q2 h2 … qt ht qt+1 - an example of key re-distribution at nonleaves will be given later. p’ k’ p k p” k” h1 p1 k1 … pi ki pi q1 k’ q2 h2 … qt ht qt+1 Notes #7

33 Delete in B+tree Simple case: the node remains at least half-full after deletion. Re-distribute keys among siblings Coalesce with a sibling and delete a pointer from its father Notes #7

34 Delete in B+tree Simple case: the node remains at least half-full after deletion. Re-distribute keys among siblings Coalesce with a sibling and delete a pointer from its father Observation: when two siblings both are no more than half full, coalesce them into a single node (which is nearly full) Notes #7

35 Node Coalescence Notes #7

36 Leaf Coalescence Notes #7

37 Leaf Coalescence p’ k’ p k p” k” p1 k1 … pi ki --- p*
i = (n+1)/2  1 t = (n+1)/2 p1 k1 … pi ki --- p* q1 h1 … qt ht --- q* Notes #7

38 Leaf Coalescence p’ k’ p k p” k” p1 k1 … pi ki --- p*
i = (n+1)/2  1 t = (n+1)/2 p1 k1 … pi ki --- p* q1 h1 … qt ht --- q* p’ k’ p k p” k” p1 k1 … pi ki q1 h1 … qt ht - q* q* Notes #7

39 Leaf Coalescence p’ k’ p k p” k” p1 k1 … pi ki --- p*
i = (n+1)/2  1 t = (n+1)/2 p1 k1 … pi ki --- p* q1 h1 … qt ht --- q* p’ k’ p k p” k” p1 k1 … pi ki q1 h1 … qt ht - q* q* Notes #7

40 Leaf coalescence : Delete key 5
Notes #7

41 Leaf coalescence : Delete key 5
order n=3 Delete(prt, 5) 38 10 60 80 3 5 10 20 40 50 60 75 80 90 Notes #7

42 Leaf coalescence : Delete key 5
order n=3 Delete(prt, 5) 38 10 60 80 3 5 10 20 40 50 60 75 80 90 Delete 5 Notes #7

43 Leaf coalescence : Delete key 5
order n=3 Delete(prt, 5) 38 10 60 80 3 5 10 20 40 50 60 75 80 90 Notes #7

44 The sibling is just half-full, so we should coalesce
Leaf coalescence : Delete key 5 order n=3 Delete(prt, 5) 38 10 60 80 3 5 10 20 40 50 60 75 80 90 The sibling is just half-full, so we should coalesce Notes #7

45 Leaf coalescence : Delete key 5
order n=3 Delete(prt, 5) 38 10 60 80 Leaf coalescence 3 10 20 10 20 40 50 60 75 80 90 3 5 10 20 Notes #7

46 Leaf coalescence : Delete key 5
order n=3 Delete(prt, 5) Less than half-full 38 10 60 80 Leaf coalescence 3 10 20 10 20 40 50 60 75 80 90 3 5 10 20 Notes #7

47 half-full, so we can re-distribute pointers at nonleaves
Leaf coalescence : Delete key 5 order n=3 Delete(prt, 5) Less than half-full more than half-full, so we can re-distribute pointers at nonleaves 38 10 60 80 Leaf coalescence 3 10 20 10 20 40 50 60 75 80 90 3 5 10 20 Notes #7

48 half-full, so we can re-distribute pointers at nonleaves
Leaf coalescence : Delete key 5 order n=3 Delete(prt, 5) Less than half-full more than half-full, so we can re-distribute pointers at nonleaves 38 10 60 80 Leaf coalescence 3 10 20 10 20 40 50 60 75 80 90 3 5 10 20 Notes #7

49 half-full, so we can re-distribute pointers at nonleaves
Key re-distribution at Nonleaves order n=3 Delete(prt, 5) Less than half-full more than half-full, so we can re-distribute pointers at nonleaves 38 10 60 80 Leaf coalescence 3 10 20 10 20 40 50 60 75 80 90 3 5 10 20 Notes #7

50 half-full, so we can re-distribute pointers at nonleaves
Key re-distribution at Nonleaves order n=3 Delete(prt, 5) Less than half-full more than half-full, so we can re-distribute pointers at nonleaves 38 60 38 10 60 80 Leaf coalescence 3 10 20 10 20 40 50 60 75 80 90 3 5 10 20 Notes #7

51 half-full, so we can re-distribute pointers at nonleaves
Key re-distribution at Nonleaves order n=3 Delete(prt, 5) Less than half-full more than half-full, so we can re-distribute pointers at nonleaves 38 60 38 10 80 60 80 Leaf coalescence 3 10 20 10 20 40 50 60 75 80 90 3 5 10 20 Notes #7

52 Key re-distribution at Nonleaves
order n=3 60 38 80 3 10 20 40 50 60 75 80 90 Notes #7

53 Nonleaf Coalescence Notes #7

54 Nonleaf Coalescence p’ k’ p k p” k” p1 k1 … pi ki pi+1
i+1 = (n+1)/2  1 t+1= (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 … qt ht qt+1 Notes #7

55 Nonleaf Coalescence p’ k’ p k p” k” p1 k1 … pi ki pi+1
i+1 = (n+1)/2  1 t+1= (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 … qt ht qt+1 p’ k’ p k p” k” p1 k1 … pi ki pi+1 q1 h1 … qt ht qt+1 q1 h1 … qt ht qt+1 Notes #7

56 Nonleaf Coalescence p’ k’ p k p” k” p1 k1 … pi ki pi+1
i+1 = (n+1)/2  1 t+1= (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 … qt ht qt+1 p’ k’ p k p” k” p1 k1 … pi ki pi+1 ? q1 h1 … qt ht qt+1 q1 h1 … qt ht qt+1 Notes #7

57 Nonleaf Coalescence p’ k’ p k p” k” p1 k1 … pi ki pi+1
i+1 = (n+1)/2  1 t+1= (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 … qt ht qt+1 p’ k’ p k p” k” p1 k1 … pi ki pi+1 ? q1 h1 … qt ht qt+1 q1 h1 … qt ht qt+1 Notes #7

58 Nonleaf Coalescence p’ k’ p k p” k” p1 k1 … pi ki pi+1
i+1 = (n+1)/2  1 t+1= (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 … qt ht qt+1 p’ k’ p k p” k” p1 k1 … pi ki pi+1 k q1 h1 … qt ht qt+1 q1 h1 … qt ht qt+1 Notes #7

59 Nonleaf Coalescence p’ k’ p k p” k” p1 k1 … pi ki pi+1
i+1 = (n+1)/2  1 t+1= (n+1)/2 p1 k1 … pi ki pi+1 q1 h1 … qt ht qt+1 p’ k’ p k p” k” p1 k1 … pi ki pi+1 k q1 h1 … qt ht qt+1 q1 h1 … qt ht qt+1 Notes #7

60 Nonleaf coalescence : Delete key 5
order n=3 Notes #7

61 Nonleaf coalescence : Delete key 5
order n=3 Delete(prt, 5) 55 10 60 3 5 10 20 55 58 61 72 Notes #7

62 Nonleaf coalescence : Delete key 5
order n=3 Delete(prt, 5) 55 10 60 3 5 10 20 55 58 61 72 delete 5 Notes #7

63 Nonleaf coalescence : Delete key 5
order n=3 Delete(prt, 5) 55 10 60 3 5 10 20 55 58 61 72 Notes #7

64 Nonleaf coalescence : Delete key 5
order n=3 Delete(prt, 5) 55 10 60 leaf coalescence 3 10 20 10 20 55 58 61 72 3 5 10 20 Notes #7

65 Nonleaf coalescence : Delete key 5
order n=3 Delete(prt, 5) 55 less than half-full 10 60 leaf coalescence 10 20 55 58 61 72 3 10 20 3 5 10 20 Notes #7

66 Nonleaf coalescence : Delete key 5
order n=3 Delete(prt, 5) 55 less than half-full just half-full, so we need to coalesce 10 60 leaf coalescence 10 20 55 58 61 72 3 10 20 3 5 10 20 Notes #7

67 Nonleaf coalescence : Delete key 5
order n=3 55 60 Delete(prt, 5) 55 nonleaf coalescence 55 60 60 leaf coalescence 61 72 3 10 20 10 20 55 58 3 5 10 20 Notes #7

68 Nonleaf coalescence : Delete key 5
order n=3 55 60 Delete(prt, 5) 55 nonleaf coalescence new root 55 60 60 leaf coalescence 61 72 3 10 20 10 20 55 58 3 5 10 20 Notes #7

69 Nonleaf coalescence : Delete key 5
order n=3 55 60 3 10 20 55 58 61 72 Notes #7

70 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *ptr is also returned \\ (k,p) is the data record to be deleted from the subtree rooted at pt; belowmin = true if \\ after deletion, pt has fewer than the required min # of pointers; Case 1. pt is a leaf delete (k,p) in pt; IF pt has at least (n+1)/2 data pointers OR pt is the root THEN return (belowmin = false) ELSE return (belowmin = true); Case 2. pt is not a leaf find a key ki in pt such that ki ≤ k < ki+1; Delete(pi , (k, p), belowmin'); IF (not belowmin') THEN return(belowmin= false); ELSE IF pi has an adjacent sibling p' that has more than the required min # of pointers THEN move one key-pointer pair from p' to pi; ELSE combine pi with an adjacent sibling of pi into a single node; IF pt is the root with only one pointer pi THEN pt = pi; return(belowmin= false); IF pt has at least (n+1)/2 pointers OR pt is the root THEN return(belowmin= false) ELSE return(belowmin= true); Notes #7

71 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *ptr is also returned \\ (k,p) is the data record to be deleted from the subtree rooted at pt; belowmin = true if \\ after deletion, pt has fewer than the required min # of pointers; Case 1. pt is a leaf delete (k,p) in pt; IF pt has at least (n+1)/2 data pointers OR pt is the root THEN return (belowmin = false) ELSE return (belowmin = true); Case 2. pt is not a leaf find a key ki in pt such that ki ≤ k < ki+1; Delete(pi , (k, p), belowmin'); IF (not belowmin') THEN return(belowmin= false); ELSE IF pi has an adjacent sibling p' that has more than the required min # of pointers THEN move one key-pointer pair from p' to pi; ELSE combine pi with an adjacent sibling of pi into a single node; IF pt is the root with only one pointer pi THEN pt = pi; return(belowmin= false); IF pt has at least (n+1)/2 pointers OR pt is the root THEN return(belowmin= false) ELSE return(belowmin= true); delete at a leaf Notes #7

72 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *ptr is also returned \\ (k,p) is the data record to be deleted from the subtree rooted at pt; belowmin = true if \\ after deletion, pt has fewer than the required min # of pointers; Case 1. pt is a leaf delete (k,p) in pt; IF pt has at least (n+1)/2 data pointers OR pt is the root THEN return (belowmin = false) ELSE return (belowmin = true); Case 2. pt is not a leaf find a key ki in pt such that ki ≤ k < ki+1; Delete(pi , (k, p), belowmin'); IF (not belowmin') THEN return(belowmin= false); ELSE IF pi has an adjacent sibling p' that has more than the required min # of pointers THEN move one key-pointer pair from p' to pi; ELSE combine pi with an adjacent sibling of pi into a single node; IF pt is the root with only one pointer pi THEN pt = pi; return(belowmin= false); IF pt has at least (n+1)/2 pointers OR pt is the root THEN return(belowmin= false) ELSE return(belowmin= true); delete at a nonleaf Notes #7

73 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *ptr is also returned \\ (k,p) is the data record to be deleted from the subtree rooted at pt; belowmin = true if \\ after deletion, pt has fewer than the required min # of pointers; Case 1. pt is a leaf delete (k,p) in pt; IF pt has at least (n+1)/2 data pointers OR pt is the root THEN return (belowmin = false) ELSE return (belowmin = true); Case 2. pt is not a leaf find a key ki in pt such that ki ≤ k < ki+1; Delete(pi , (k, p), belowmin'); IF (not belowmin') THEN return(belowmin= false); ELSE IF pi has an adjacent sibling p' that has more than the required min # of pointers THEN move one key-pointer pair from p' to pi; ELSE combine pi with an adjacent sibling of pi into a single node; IF pt is the root with only one pointer pi THEN pt = pi; return(belowmin= false); IF pt has at least (n+1)/2 pointers OR pt is the root THEN return(belowmin= false) ELSE return(belowmin= true); delete at a nonleaf recursion Notes #7

74 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *ptr is also returned \\ (k,p) is the data record to be deleted from the subtree rooted at pt; belowmin = true if \\ after deletion, pt has fewer than the required min # of pointers; Case 1. pt is a leaf delete (k,p) in pt; IF pt has at least (n+1)/2 data pointers OR pt is the root THEN return (belowmin = false) ELSE return (belowmin = true); Case 2. pt is not a leaf find a key ki in pt such that ki ≤ k < ki+1; Delete(pi , (k, p), belowmin'); IF (not belowmin') THEN return(belowmin= false); ELSE IF pi has an adjacent sibling p' that has more than the required min # of pointers THEN move one key-pointer pair from p' to pi; ELSE combine pi with an adjacent sibling of pi into a single node; IF pt is the root with only one pointer pi THEN pt = pi; return(belowmin= false); IF pt has at least (n+1)/2 pointers OR pt is the root THEN return(belowmin= false) ELSE return(belowmin= true); delete at a nonleaf no new child needs to be added recursion Notes #7

75 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *ptr is also returned \\ (k,p) is the data record to be deleted from the subtree rooted at pt; belowmin = true if \\ after deletion, pt has fewer than the required min # of pointers; Case 1. pt is a leaf delete (k,p) in pt; IF pt has at least (n+1)/2 data pointers OR pt is the root THEN return (belowmin = false) ELSE return (belowmin = true); Case 2. pt is not a leaf find a key ki in pt such that ki ≤ k < ki+1; Delete(pi , (k, p), belowmin'); IF (not belowmin') THEN return(belowmin= false); ELSE IF pi has an adjacent sibling p' that has more than the required min # of pointers THEN move one key-pointer pair from p' to pi; ELSE combine pi with an adjacent sibling of pi into a single node; IF pt is the root with only one pointer pi THEN pt = pi; return(belowmin= false); IF pt has at least (n+1)/2 pointers OR pt is the root THEN return(belowmin= false) ELSE return(belowmin= true); delete at a nonleaf no new child needs to be added recursion key re-distribution Notes #7

76 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *ptr is also returned \\ (k,p) is the data record to be deleted from the subtree rooted at pt; belowmin = true if \\ after deletion, pt has fewer than the required min # of pointers; Case 1. pt is a leaf delete (k,p) in pt; IF pt has at least (n+1)/2 data pointers OR pt is the root THEN return (belowmin = false) ELSE return (belowmin = true); Case 2. pt is not a leaf find a key ki in pt such that ki ≤ k < ki+1; Delete(pi , (k, p), belowmin'); IF (not belowmin') THEN return(belowmin= false); ELSE IF pi has an adjacent sibling p' that has more than the required min # of pointers THEN move one key-pointer pair from p' to pi; ELSE combine pi with an adjacent sibling of pi into a single node; IF pt is the root with only one pointer pi THEN pt = pi; return(belowmin= false); IF pt has at least (n+1)/2 pointers OR pt is the root THEN return(belowmin= false) ELSE return(belowmin= true); delete at a nonleaf no new child needs to be added recursion key re-distribution node coalescence Notes #7

77 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *ptr is also returned \\ (k,p) is the data record to be deleted from the subtree rooted at pt; belowmin = true if \\ after deletion, pt has fewer than the required min # of pointers; Case 1. pt is a leaf delete (k,p) in pt; IF pt has at least (n+1)/2 data pointers OR pt is the root THEN return (belowmin = false) ELSE return (belowmin = true); Case 2. pt is not a leaf find a key ki in pt such that ki ≤ k < ki+1; Delete(pi , (k, p), belowmin'); IF (not belowmin') THEN return(belowmin= false); ELSE IF pi has an adjacent sibling p' that has more than the required min # of pointers THEN move one key-pointer pair from p' to pi; ELSE combine pi with an adjacent sibling of pi into a single node; IF pt is the root with only one pointer pi THEN pt = pi; return(belowmin= false); IF pt has at least (n+1)/2 pointers OR pt is the root THEN return(belowmin= false) ELSE return(belowmin= true); delete at a nonleaf no new child needs to be added recursion key re-distribution node coalescence decide if a new root Notes #7

78 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *ptr is also returned \\ (k,p) is the data record to be deleted from the subtree rooted at pt; belowmin = true if \\ after deletion, pt has fewer than the required min # of pointers; Case 1. pt is a leaf delete (k,p) in pt; IF pt has at least (n+1)/2 data pointers OR pt is the root THEN return (belowmin = false) ELSE return (belowmin = true); Case 2. pt is not a leaf find a key ki in pt such that ki ≤ k < ki+1; Delete(pi , (k, p), belowmin'); IF (not belowmin') THEN return(belowmin= false); ELSE IF pi has an adjacent sibling p' that has more than the required min # of pointers THEN move one key-pointer pair from p' to pi; ELSE combine pi with an adjacent sibling of pi into a single node; IF pt is the root with only one pointer pi THEN pt = pi; return(belowmin= false); IF pt has at least (n+1)/2 pointers OR pt is the root THEN return(belowmin= false) ELSE return(belowmin= true); report if pt is less than half-full Notes #7

79 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *ptr is also returned \\ (k,p) is the data record to be deleted from the subtree rooted at pt; belowmin = true if \\ after deletion, pt has fewer than the required min # of pointers; Case 1. pt is a leaf delete (k,p) in pt; IF pt has at least (n+1)/2 data pointers OR pt is the root THEN return (belowmin = false) ELSE return (belowmin = true); Case 2. pt is not a leaf find a key ki in pt such that ki ≤ k < ki+1; Delete(pi , (k, p), belowmin'); IF (not belowmin') THEN return(belowmin= false); ELSE IF pi has an adjacent sibling p' that has more than the required min # of pointers THEN move one key-pointer pair from p' to pi; ELSE combine pi with an adjacent sibling of pi into a single node; IF pt is the root with only one pointer pi THEN pt = pi; return(belowmin= false); IF pt has at least (n+1)/2 pointers OR pt is the root THEN return(belowmin= false) ELSE return(belowmin= true); Notes #7

80 B+tree deletions in practice
Often, coalescing is not implemented Too hard and not worth it! Notes #7

81 Outline of Course Representing things by tables E-R model (Ch. 4)
Good table structures Functional Dependencies (Ch.3) Basic operations on relations Relational Algebra (Chs. 2+5) Storage management (Chs ) SQL languages in DDL/DML (Ch. 6) Query processing (Chs ) More on SQL (Chs. 7-9) Transition processing (Chs )


Download ppt "CPSC-310 Database Systems"

Similar presentations


Ads by Google