CPSC-310 Database Systems Professor Jianer Chen Room 315C HRBB Lecture #20
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
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; 3.1 If L’ is more than half-full, then move a record from L’ to L, and return; 3.2 Else combine L and L’ into a single leaf; 3.3 But now you need to consider the case of deleting a child from L’s parent … (recursively) Notes #7
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
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
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
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
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
Key re-distribution at leaves: Delete 5 order n=3 Notes #7
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
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
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
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
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
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
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
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
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
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
Key re-distribution at leaves: Delete 5 order n=3 105 20 40 3 10 20 35 40 50 Notes #7
Key re-distribution at nonleaves Notes #7
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
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
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 q1 h1 q2 h2 … qt ht qt+1 - Notes #7
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 ? q1 h1 q2 h2 … qt ht qt+1 - Notes #7
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 ? q1 h1 q2 h2 … qt ht qt+1 - Notes #7
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 k’ q1 h1 q2 h2 … qt ht qt+1 - Notes #7
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 k’ q1 h1 q2 h2 … qt ht qt+1 - Notes #7
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 k’ q1 h1 q2 h2 … qt ht qt+1 - Notes #7
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+1 q1 k’ q1 h1 q2 h2 … qt ht qt+1 - Notes #7
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+1 q1 k’ q2 h2 … qt ht qt+1 Notes #7
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+1 q1 k’ q2 h2 … qt ht qt+1 Notes #7
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
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
Node Coalescence Notes #7
Leaf Coalescence Notes #7
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
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
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
Leaf coalescence : Delete key 5 Notes #7
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
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
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
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
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
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
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
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
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
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
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
Key re-distribution at Nonleaves order n=3 60 38 80 3 10 20 40 50 60 75 80 90 Notes #7
Nonleaf Coalescence Notes #7
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
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
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
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
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
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
Nonleaf coalescence : Delete key 5 order n=3 Notes #7
Nonleaf coalescence : Delete key 5 order n=3 Delete(prt, 5) 55 10 60 3 5 10 20 55 58 61 72 Notes #7
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
Nonleaf coalescence : Delete key 5 order n=3 Delete(prt, 5) 55 10 60 3 5 10 20 55 58 61 72 Notes #7
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
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
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
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
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
Nonleaf coalescence : Delete key 5 order n=3 55 60 3 10 20 55 58 61 72 Notes #7
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
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
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
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
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
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
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
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
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
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
B+tree deletions in practice Often, coalescing is not implemented Too hard and not worth it! Notes #7
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. 13-14) SQL languages in DDL/DML (Ch. 6) Query processing (Chs. 15-16) More on SQL (Chs. 7-9) Transition processing (Chs. 17-19)