Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 18: B-Trees Example: M Note: Each leaf has the same depth D H

Similar presentations


Presentation on theme: "Chapter 18: B-Trees Example: M Note: Each leaf has the same depth D H"— Presentation transcript:

1 Chapter 18: B-Trees Example: M Note: Each leaf has the same depth D H
Q T X B C F G J K L N P R S V W Y Z Node x has the following fields: n[x] -- number of keys stored in x. key1[x]  key2[x]  …  keyn[x][x] -- the n[x] keys. leaf[x] -- Boolean. if internal: n[x] + 1 pointers to children, c1[x], c2[x], …, cn[x]+1[x]. Let ki = any key in subtree rooted at ci[x]. Then, k1  key1[x]  k2  key2[x]  …  keyn[x][x]  kn[x]+1.  t  2 called the minimum degree. x  root  t –1  n[x]  2t–1 x = root  n[x]  2t–1 Comp 750, Fall 2009

2 Application: Disk Accesses
Each node is stored as a page. Page size determines t. t is usually large Implies branching factor is large, so height is small. Example: Holds over one billion keys. Height is only 2, so can find any key with only two disk accesses (compare to red-black trees, where the branching factor is 2). Note: Disk accesses dominate performance in this application. 1001 1000 1000 1000 1000 1000 1000 1000 Comp 750, Fall 2009

3 Height of a B-Tree Theorem 18.1: Let n = the number of keys in T, n  1, t  2, h = height of T. Then, Proof: Let T be of height h. The number of nodes is minimized when root has 1 key and all other nodes have t–1 keys. This gives us 2ti-1 nodes at depth i, 1  i  h, and 1 node at depth 0. Hence, Comp 750, Fall 2009

4 B-Tree Operations Search: Create: Insert and Delete:
(logtn) disk accesses. O(t logtn) CPU time. Create: O(1) disk accesses. O(1) CPU time. Insert and Delete: O(logtn) disk accesses. In the code that follows, we use: Disk-Read: To move node from disk to memory. Disk-Write: To move node from memory to disk. We assume root is in memory. Comp 750, Fall 2009

5 Search Search(x, k) i := 1; while i  n[x] and k > keyi[x] do
i := i + 1 od; if i  n[x] and k = keyi[x] then return(x,i) fi; if leaf[x] then return NIL else DiskRead(ci[x]); Search(ci[x]) fi Search(root[T], k) returns (y,i) s.t. keyi[y] = k or NIL if no such key. Worst-case: (logtn) disk reads. (t logtn) CPU time. Comp 750, Fall 2009

6 Creating an Empty Tree Create(T) x := Allocate-Node();
leaf[x] := true; n[x] := 0; Disk-Write(x); root[T] := x To create a nonempty tree, first create an empty tree, then insert nodes. Splitting is fundamental to insert. Comp 750, Fall 2009

7 Splitting Applied to a “full” child of a “nonfull” parent. “full”  2t–1 keys. Example: (t=4) keyi [x] keyi [x] keyi-1[x] keyi+1 [x] keyi-1[x] x x Split … N W … … N S W … y = ci[x] y = ci[x] z = ci+1[x] P Q R S T U V P Q R T U V T1 T2 T3 T4 T5 T6 T7 T8 T1 T2 T3 T4 T5 T6 T7 T8 Comp 750, Fall 2009

8 Split-Child Split-Child(x, i, y) z := Allocate-Node();
leaf[z] := leaf[y]; n[z] := t–1; for j := 1 to t–1 do keyj[z] := keyj+t[y] od; if not leaf[y] then for j := 1 to t do cj[z] := cj+t[y] od fi; n[y] := t–1; for j := n[x] + 1 downto i+1 do cj+1[x] := cj[x] ci+1[x] := z; /* Continued */ for j := n[x] downto i do keyj+1[x] := keyj[x] od; keyi[x] := keyt[y]; n[x] := n[x] + 1; Disk-Write(y); Disk-Write(z); Disk-Write(x) (t) CPU time. O(1) disk writes. Comp 750, Fall 2009

9 Insert First, modify tree (if necessary) to create room for new key.
Insert(T, k) r := root[T]; if n[r] = 2t–1 then s := Allocate-Node(); root[T] := s; leaf[s] := false; n[s] := 0; c1[s] := r; Split-Child(s, 1, r); Insert-Nonfull(s, k) else Insert-Nonfull(r, k) fi First, modify tree (if necessary) to create room for new key. Then, call Insert-Nonfull(). Example: root[T] s H root[T] r r A D F H L N P A D F L N P T1 T2 T3 T4 T5 T6 T7 T8 T1 T2 T3 T4 T5 T6 T7 T8 Comp 750, Fall 2009

10 Insert-Nonfull Insert-Nonfull(x, k) i := n[x]; if leaf[x] then
while i  1 and k < keyi[x] do keyi+1[x] := keyi[x]; i := i–1 od; keyi+1[x] := k; n[x] := n[x] + 1; Disk-Write(x) else /* not leaf[x] */ while i  1 and k < keyi[x] do i := i–1 od; i := i + 1; Disk-Read(ci[x]); if n[ci[x]] = 2t–1 then Split-Child(x, i, ci[x]); if k > keyi[x] then i := i + 1 fi fi; Insert-Nonfull(ci[x], k) Worst Case: (t logtn) CPU time. (logtn) disk writes. Comp 750, Fall 2009

11 Insert Example t = 3 G M P X A C D E J K N O R S T U V Y Z Insert B
A B C D E J K N O R S T U V Y Z Comp 750, Fall 2009

12 Insert Example (Continued)
G M P X A B C D E J K N O R S T U V Y Z Insert Q G M P T X A B C D E J K N O Q R S U V Y Z Comp 750, Fall 2009

13 Insert Example (Continued)
G M P T X A B C D E J K N O Q R S U V Y Z Insert L P G M T X A B C D E J K L N O Q R S U V Y Z Comp 750, Fall 2009

14 Insert Example (Continued)
G M T X A B C D E J K L N O Q R S U V Y Z Insert F P C G M T X A B D E F J K L N O Q R S U V Y Z Comp 750, Fall 2009

15 Deletion Main Idea: Recursively descend tree.
Ensure any non-root node x that is considered has at least t keys. May have to move key down from parent. Comp 750, Fall 2009

16 Deletion Cases Case 0: Empty root -- make root’s only child the new root. x c1[x] Case 1: k in x, x is a leaf -- delete k from x. x x leaf leaf … k … … …  t keys  t–1 keys Comp 750, Fall 2009

17 Deletion Cases (Continued)
Case 2: k in x, x internal. x not a leaf … k … y z Subcase A: y has at least t keys -- find predecessor k´ of k in subtree rooted at y, recursively delete k´, replace k by k´ in x. x x not a leaf … k … … k´… y y  t keys  t keys pred of k Comp 750, Fall 2009

18 Deletion Cases (Continued)
Subcase B: z has at least t keys -- find successor k´ of k in subtree rooted at z, recursively delete k´, replace k by k´ in x. x x not a leaf … k … … k´… z z  t keys  t keys succ of k Subcase C: y and z both have t–1 keys -- merge k and z into y, free z, recursively delete k from y. x x not a leaf not a leaf … k … … … y z y y’s keys, k, z’s keys t–1 keys t–1 keys 2t–1 keys Comp 750, Fall 2009

19 Deletion Cases (Continued)
Case 3: k not in internal node. Let ci[x] be the root of the subtree that must contain k, if k is in the tree. If ci[x] has at least t keys, then recursively descend; otherwise, execute 3.A and 3.B as necessary. Subcase A: ci[x] has t–1 keys, some sibling has at least t keys. recursively descend x x not a leaf … … k1 … … k2 ci[x] ci[x] k2 k1 t–1 keys t keys k k Comp 750, Fall 2009

20 Deletion Cases (Continued)
Subcase B: ci[x] and sibling both have t–1 keys. recursively descend x x not a leaf … … … … k1 ci[x] ci[x] ci+1[x] ci[x]’s keys, , ci+1[x]’s keys k1 t–1 keys t–1 keys 2t–1 keys k k Comp 750, Fall 2009

21 Delete Example P t = 3 C G M T X A B D E F J K L N O Q R S U V Y Z
Delete F (Case 1) P C G M T X A B D E J K L N O Q R S U V Y Z Comp 750, Fall 2009

22 Delete Example (Continued)
C G M T X A B D E J K L N O Q R S U V Y Z Delete M (Case 2.A) P C G L T X A B D E J K N O Q R S U V Y Z Comp 750, Fall 2009

23 Delete Example (Continued)
C G L T X A B D E J K N O Q R S U V Y Z Delete G (Case 2.C) P C L T X A B D E J K N O Q R S U V Y Z Comp 750, Fall 2009

24 Delete Example (Continued)
C L T X A B D E J K N O Q R S U V Y Z Delete D (Case 3.B) C L P T X A B E J K N O Q R S U V Y Z Comp 750, Fall 2009

25 Delete Example (Continued)
C L P T X A B E J K N O Q R S U V Y Z Case 0 C L P T X A B E J K N O Q R S U V Y Z Comp 750, Fall 2009

26 Delete Example (Continued)
C L P T X A B E J K N O Q R S U V Y Z Delete B (Case 3.A) E L P T X A C J K N O Q R S U V Y Z Comp 750, Fall 2009


Download ppt "Chapter 18: B-Trees Example: M Note: Each leaf has the same depth D H"

Similar presentations


Ads by Google