Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC-608 Database Systems

Similar presentations


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

1 CPSC-608 Database Systems
Fall 2018 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #23 Notes #7

2 B+Trees A B+tree node of order n
where ph are pointers (disk addresses) and kh are search-keys (values of the attributes in the index) pn+1 kn k2 p2 k1 p1 p3 …… root 100 An Example B+ tree of order n=3 30 120 150 180 3 5 11 30 35 100 101 110 120 130 150 156 179 180 200 Notes #7

3 B+tree rules Rule 1. All leaves are at same lowest level (balanced tree) Rule 2. Pointers in leaves point to records except for “sequence pointer” Rule 3. Number of keys/pointers in nodes: Max. # pointers Max. # keys Min. # keys Non-leaf n+1 n (n+1)/2 (n+1)/2 1 Leaf (n+1)/2 + 1 (n+1)/2 Root 2 1 Notes #7

4 Search in a B+tree Start from the root Search in a leaf block
May not have to go to the data file Search(ptr, k); \\ search a record of key value k in the subtree rooted at ptr \\ assume the B+tree is a dense index of order n Case 1. ptr is a leaf \\ pn+1 is the sequence pointer IF (k = ki) for a key ki in *ptr THEN return(pi); ELSE return(Null); Case 2. ptr is not a leaf find a key ki in *ptr such that ki-1 ≤ k < ki; return(Search(pi, k)); Notes #7

5 Range Search in B+tree To research all records whose key values are between k1 and k2: Range-Search(ptr, k1, k2) Call Search(ptr, k1); Follow the sequence pointers until the search key value is larger than k2. Notes #7

6 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). Notes #7

7 Pseudo Code for Insertion in B+tree
insert in a leaf Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). Notes #7

8 Pseudo Code for Insertion in B+tree
insert in a leaf Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). no overflow Notes #7

9 Pseudo Code for Insertion in B+tree
insert in a leaf Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). no overflow with overflow Notes #7

10 Pseudo Code for Insertion in B+tree
insert in a leaf Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). no overflow new child to parent with overflow Notes #7

11 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). insert in a nonleaf Notes #7

12 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). insert in a nonleaf recursion Notes #7

13 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). insert in a nonleaf recursion no overflow Notes #7

14 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). insert in a nonleaf recursion no overflow no overflow Notes #7

15 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). insert in a nonleaf recursion no overflow with overflow no overflow Notes #7

16 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). insert in a nonleaf recursion no overflow with overflow no overflow new child to parent Notes #7

17 Pseudo Code for Insertion in B+tree
Insert(pt, (p, k), (p', k')); \\ technically, the smallest key kmin in pt is also returned \\ (p, k) is a pointer-key pair to be inserted into the subtree rooted at pt; p' is a new sibling \\ of pt, if created, and k' is the smallest key value in p' ; Case 1. pt = (p1, k1, ..., pi, ki, --, pn+1) is a leaf \\ pn+1 is the sequence pointer IF (i < n) THEN insert (p, k) into pt; return(p' = Null, k' = 0); ELSE re-arrange (p1, k1), ..., (pn, kn), and (p, k) into (ρ1, κ1, ..., ρn+1, κn+1); create a new leaf p''; put (ρr+1, κr+1, …, ρn+1, κn+1, --, pn+1) in p''; \\ r = (n+1)/2 put (ρ1, κ1, ..., ρr, κr, --, p'') in pt; \\ pn+1 and p'' are sequence pointers in p'' and pt. IF pt is the root THEN create a new root with children pt and p'' (and key κr+1) ELSE return(p' = p'', k' = κr+1); Case 2. pt is not a leaf find a key ki in pt such that ki-1 ≤ k < ki; Insert(pi , (k, p), (k", p")); IF (p" = Null) THEN return(k' = 0, p' = Null); ELSE IF there is room in pt, THEN insert (k", p") into pt; return(k' = 0, p' = Null); ELSE re-arrange the content in pt and (k", p") into (ρ1, κ1, ..., ρn+1, κn+1, ρn+2); create a new node p''; put (ρr+1, κr+1, …, ρn+1, κn+1, ρn+2, -- ) in p''; \\ r = (n+1)/2 leave (ρ1, κ1, ..., ρr-1, κr-1, ρr , -- ) in pt; IF pt is the root THEN create a new root with children pt and p'' (and key κr) ELSE return(p' = p'', k' = κr ). new root Notes #7

18 Delete in B+tree Notes #7

19 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

20 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

21 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

22 Simple case: Delete key 35
order n=3 Notes #7

23 Simple case: Delete key 35
order n=3 Delete(prt, 35) 105 10 40 3 5 8 10 20 35 40 50 Notes #7

24 Simple case: Delete key 35
order n=3 Delete(prt, 35) 105 10 40 3 5 8 10 20 35 40 50 Delete 35 Notes #7

25 After deletion, the node still keeps at least half-full
Simple case: Delete key 35 order n=3 Delete(prt, 35) 105 10 40 3 5 8 10 20 35 40 50 After deletion, the node still keeps at least half-full Notes #7

26 Simple case: Delete key 35
order n=3 105 10 40 3 5 8 10 20 40 50 Notes #7

27 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

28 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

29 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

30 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

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

32 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

33 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

34 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

35 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

36 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

37 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

38 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

39 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

40 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

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

42 Key re-distribution at nonleaves
Notes #7

43 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

44 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

45 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

46 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

47 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

48 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

49 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

50 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

51 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

52 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

53 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

54 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

55 Node Coalescence Notes #7

56 Leaf Coalescence Notes #7

57 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

58 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

59 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

60 Leaf coalescence : Delete key 5
Notes #7

61 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

62 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

63 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

64 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

65 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

66 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

67 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

68 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

69 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

70 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

71 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

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

73 Nonleaf Coalescence Notes #7

74 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

75 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

76 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

77 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

78 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

79 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

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

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

82 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

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

84 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

85 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

86 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

87 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

88 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

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

90 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *pt 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

91 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *pt 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

92 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *pt 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

93 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *pt 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

94 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *pt 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 half-full condition is satisfied recursion Notes #7

95 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *pt 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 half-full condition is satisfied recursion key re-distribution Notes #7

96 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *pt 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 half-full condition is satisfied recursion key re-distribution node coalescence Notes #7

97 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *pt 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 half-full condition is satisfied recursion key re-distribution node coalescence decide if a new root Notes #7

98 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *pt 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

99 Pseudo Code for Deletion in a B+tree
Delete(pt, (k,p), belowmin); \\ technically, the smallest key kmin in *pt 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

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


Download ppt "CPSC-608 Database Systems"

Similar presentations


Ads by Google