IAPG 1 Lektion 8 BTrees Ch 16 Oplg
I-APG 1, Lektion 8 B-Trees • Ch. 16: – Oplæg: 16. 4 – Opgave: 57, 59
B-Trees • AVL “Internal dictionary”: – Dictionary small enough to reside in internal memory • B-Tree “External dictionary”: – Dictionary resides on disc • ISAM “Indexed Sequential Access Method” – Sequential access – Random access (Index: array-like)
m-Way Search Tree • Each internal node (in the extended search tree) has up to m children and between 1 and m-1 elements • A node with p elements has exactly p+1 children – For the p keys k 1, k 2, …, kp: k 1 < k 2 < … < kp – For the p+1 children c 1, c 2, …, cp: • c 0 subtree: Keys of elements are smaller than k 1 • cp subtree: Keys of elements are larger than kp • ci subtree (1≤i<p): Keys of elements are larger than ki and smaller than ki+1 • (May be empty) • (Extended search tree: Replace null pointers with external nodes)
m-Way Search Tree 1 2 k 1 k 2 … p … kp Internal node External nodes 1 2 1 … 2 p+1 … k 1 k 2 ki p ki+1 kp ci … … Internal node ci subtree: ki < keys of elements < ki+1
m-Way Search Tree 1 2 k 1 k 2 2 k'1 k '2 … pj … … ki … … 1 pi k'j …
m-Way Search Tree • • • Search Insert Delete Height Is. Empty … …examples to follow …
m-Way Search Tree 10 80 5 2 3 4 20 30 40 50 60 70 32 36 82 84 86 88 90 92 94 96 98
m-Way Search Tree: Search search(31) 5 2 3 4 10 80 20 30 40 50 60 70 32 36 82 84 86 88 90 92 94 96 98
m-Way Search Tree: Search search(92) 5 2 3 4 10 80 20 30 40 50 60 70 32 36 82 84 86 88 90 92 94 96 98
m-Way Search Tree: Insert insert(31) 5 2 3 4 10 80 20 30 40 50 60 70 31 32 36 82 84 86 88 90 92 94 96 98
m-Way Search Tree: Insert insert(65) 5 2 3 4 10 80 20 30 40 50 60 70 31 32 36 65 82 84 86 88 90 92 94 96 98
m-Way Search Tree: Delete delete(20) 5 2 3 4 10 80 20 30 40 50 60 70 32 36 82 84 86 88 90 92 94 96 98
m-Way Search Tree: Delete delete(84) 5 2 3 4 10 80 20 30 40 50 60 70 32 36 82 82 84 86 88 90 92 94 96 98
m-Way Search Tree: Delete delete(5) 54 2 3 4 10 80 20 30 40 50 60 70 32 36 82 84 86 88 90 92 94 96 98
m-Way Search Tree: Delete delete(10) 5 2 3 4 20 10 80 20 30 40 50 60 70 32 36 82 84 86 88 90 92 94 96 98
Height of an m-Way Search Tree • An m-way search tree of height h may have – As few as h elements – As may as mh-1 elements • A 200 -way search tree of height 5 might hold – As few as 5 elements – As many as 32 * 1010 – 1 elements
B-Tree of Order m • Is an m-way search tree • If the B-Tree is not empty then – The root has at least two children – All internal nodes other than the root have at least ┌m/2┐children – All external nodes are at the same level
B-Tree of Order 7 10 80 2 4 6 20 30 40 50 60 70 82 84 86 88
Height of a B-Tree of Order m Let • Order m and height h • d = ┌m/2┐and n number of elements Then 1. 2 dh-1 - 1 ≤ n ≤ mh - 1 2. logm(n + 1) ≤ h ≤ logd((n+1)/2) + 1 B-Tree of order 200 and height 3 has at least 19999 elements B-Tree of order 200 and height 5 has at least 2 * 1018 - 1 elements B-Tree order is determined by disk block size and size of elements
B-Tree of Order m • • • Search Insert Delete Height Is. Empty …
B-Tree of Order 7: Search search(4) 10 80 search(35) 2 4 6 20 30 40 50 60 70 82 84 86 88
B-Tree of Order m: Insert • The element to be inserted can be placed in the node • The element to be inserted causes overflow in the node • Split overfull node into two • Insert middle element in parent node • (Iterate towards the root)
B-Tree of Order 7: Insert insert(3) 2 23 4 10 80 6 20 30 40 50 60 70 82 84 86 88
B-Tree of Order 7: Insert insert(25) 2 3 4 6 1010408080 20 20 25 30 40 50 60 70 82 84 86 88
B-Tree of Order 3: Insert insert(44) 30 80 20 10 50 60 25 35 40 55 90 70 82 85 95
B-Tree of Order 3: Insert insert(44) 30 80 20 10 40 50 60 25 35 44 55 90 70 82 85 95
B-Tree of Order 3: Insert insert(44) 30 50 80 20 10 25 35 40 60 44 55 90 70 82 85 95
B-Tree of Order 3: Insert 50 insert(44) 30 20 10 25 35 80 40 60 44 55 90 70 82 85 95
B-Tree of Order m: Insert Overfull node with m elements and m+1 children • P = m, c 0, (e 1, c 1), …, (em, cm) eis are the elements and cis are the children pointers Split node around element ed, where d = ┌m/2┐ • • P = d-1, c 0, (e 1, c 1), …, (ed-1, cd-1) Q = m-d, cd, (ed+1, cd+1), …, (em, cm) and insert (ed, Q) in the parent node
B-Tree of Order m: Insert ├ m P ┤ ed ├ m+1 ┤ ed P d = ┌m/2┐ ed ├ ≥d ┤ Q ├ ≥d ┤
B-Tree of Order m: Delete an element: 1. The element to be deleted is a leaf 2. The element to be deleted is a nonleaf: Transform into 1) by replacing the element to be deleted with the largest element in its left-neighboring subtree or the smallest element in its right-neighboring subtree. The replacing element is a leaf. Delete a leaf element: • The node contains more than the minimum number of elements • Else if nearest left or right sibling is not minimal too: Move that sibling to parent and parent element to the deleted element • Else if nearest left or right sibling is minimal too: Merge these nodes with the parent element (Iterate towards the root) If the root becomes an element short, it is empty, and therefore deleted
B-Tree of Order 7: Delete Nonleaf delete(80) 2 23 4 6 10 80 20 30 40 50 60 70 • Replace 80 by either 70 or 82 • Delete either 70 or 82 (leaf) 82 84 86 88
B-Tree of Order 3: Delete Nonleaf • Replace 80 by either 70 or 82 • Delete either 70 or 82 (leaf) 50 delete(80) 30 20 10 25 35 80 40 60 44 55 90 70 82 85 95
B-Tree of Order 7: Delete Leaf delete(50) 2 23 4 6 10 80 40 60 70 20 20 30 30 40 50 82 84 86 88
B-Tree of Order 3: Delete Leaf 50 delete(85) 80 30 20 10 25 35 40 60 44 55 90 70 82 85 95
B-Tree of Order 7: Delete delete(25) 2 3 4 6 10 6 40 80 10 20 20 25 30 50 60 70 82 84 86 88
B-Tree of Order 7: Delete delete(25) 2 3 4 6 1010408080 20 20 25 30 40 50 50 60 60 70 70 82 84 86 88
B-Tree of Order 3: Delete delete(10) 30 80 20 10 20 25 50 60 35 40 55 90 70 82 85 95
B-Tree of Order 3: Delete delete(10) 50 30 80 30 20 25 50 60 35 40 55 90 70 82 85 95
B-Tree of Order 3: Delete 50 delete(44) 30 20 10 25 80 40 60 35 40 44 55 90 70 82 85 95
B-Tree of Order 3: Delete 50 delete(44) 30 20 30 10 25 80 60 35 40 55 90 70 82 85 95
B-Tree of Order 3: Delete 50 delete(44) 50 80 20 30 10 25 60 35 40 55 90 70 82 85 95
B-Tree of Order m: Delete d = ┌m/2┐ ├ ├ d-1 ┤ ├ ? d-2 ┤ ┤ • (d-1)+(d-2)+1 = 2 d-2 • m odd: 2 d-2 = m-1 • m even: 2 d-2 = m-2
Node Structure: B´-Tree So far … • s, c 0, (e 1, c 1), (e 2, c 2), …, (es, cs) • ei’s elements in ascending order of key • ci’s children pointers ki ci pi But when element size is large relative to key size … • s, c 0, (k 1, c 1, p 1), (k 2, c 2, p 2), …, (ks, cs, ps) • ki’s elements key • ci’s children pointers • pi’s disc location of ei element • % In a B´-Tree … • nonleaf nodes contain no pi pointers ki ci s, c 0, (k 1, c 1), (k 2, c 2), …, (ks, cs) • Leaf nodes replace null children pointers by pi pointers s, p 0, (k 1, p 1), (k 2, p 2), …, (ks, ps) • Relate each ki to some pj ei ci ei ki pi ei
B´-Tree of Order 3 50 30 20 10 25 35 80 40 60 44 55 90 70 82 85 95
- Slides: 50