500 likes | 509 Views
I-APG1, 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
E N D
I-APG1, 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 k1, k2, …, kp: k1 < k2 < … < kp • For the p+1 children c1, c2, …, cp: • c0 subtree: Keys of elements are smaller than k1 • cp subtree: Keys of elements are larger than kp • cisubtree (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 … p k1 k2 … kp Internal node External nodes 1 2 … p+1 1 2 … p k1 k2 ki ki+1 kp Internal node ci ci subtree: ki < keys of elements < ki+1 … …
m-Way Search Tree 1 2 pi … k1 k2 ki … … 1 2 pj … k'1 k '2 k'j … …
m-Way Search Tree • Search • Insert • Delete • Height • IsEmpty • … …examples to follow …
m-Way Search Tree 10 80 5 20 30 40 50 60 70 82 84 86 88 2 3 4 32 36 90 92 94 96 98
m-Way Search Tree: Search search(31) 10 80 5 20 30 40 50 60 70 82 84 86 88 2 3 4 32 36 90 92 94 96 98
m-Way Search Tree: Search search(92) 10 80 5 20 30 40 50 60 70 82 84 86 88 2 3 4 32 36 90 92 94 96 98
m-Way Search Tree: Insert insert(31) 10 80 5 20 30 40 50 60 70 82 84 86 88 2 3 4 31 32 36 90 92 94 96 98
m-Way Search Tree: Insert insert(65) 10 80 5 20 30 40 50 60 70 82 84 86 88 2 3 4 31 32 36 65 90 92 94 96 98
m-Way Search Tree: Delete delete(20) 10 80 5 20 30 40 50 60 70 82 84 86 88 2 3 4 32 36 90 92 94 96 98
82 m-Way Search Tree: Delete delete(84) 10 80 5 20 30 40 50 60 70 82 84 86 88 2 3 4 32 36 90 92 94 96 98
4 m-Way Search Tree: Delete delete(5) 10 80 5 20 30 40 50 60 70 82 84 86 88 2 3 4 32 36 90 92 94 96 98
20 m-Way Search Tree: Delete delete(10) 10 80 5 20 30 40 50 60 70 82 84 86 88 2 3 4 32 36 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 • 2dh-1 - 1 ≤ n ≤ mh - 1 • 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 • IsEmpty • …
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)
2 3 B-Tree of Order 7: Insert insert(3) 10 80 2 4 6 20 30 40 50 60 70 82 84 86 88
10 40 80 20 25 30 B-Tree of Order 7: Insert insert(25) 10 80 2 3 4 6 20 30 40 50 60 70 40 82 84 86 88
B-Tree of Order 3: Insert insert(44) 30 80 20 50 60 90 10 25 35 40 55 70 82 85 95
B-Tree of Order 3: Insert insert(44) 30 80 20 40 50 60 90 10 25 35 44 55 70 82 85 95
B-Tree of Order 3: Insert insert(44) 30 80 50 20 40 60 90 10 25 35 44 55 70 82 85 95
B-Tree of Order 3: Insert 50 insert(44) 30 80 20 40 60 90 10 25 35 44 55 70 82 85 95
B-Tree of Order m: Insert • Overfull node with m elements and m+1 children • P = m, c0, (e1, c1), …, (em, cm) • eis are the elements and cis are the children pointers • Split node around element ed, where d = ┌m/2┐ • P = d-1, c0, (e1, c1), …, (ed-1, cd-1) • Q = m-d, cd, (ed+1, cd+1), …, (em, cm) • and insert (ed,Q) in the parent node
ed ed B-Tree of Order m: Insert ├m ┤ P ed ├m+1 ┤ P Q d = ┌m/2┐ ├≥ d ┤ ├≥ d ┤
B-Tree of Order m: Delete • Delete an element: • The element to be deleted is a leaf • 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
2 3 B-Tree of Order 7: Delete Nonleaf delete(80) 10 80 2 4 6 20 30 40 50 60 70 82 84 86 88 • Replace 80 by either 70 or 82 • Delete either 70 or 82 (leaf)
B-Tree of Order 3: Delete Nonleaf 50 • Replace 80 by either 70 or 82 • Delete either 70 or 82 (leaf) delete(80) 30 80 20 40 60 90 10 25 35 44 55 70 82 85 95
20 30 40 2 3 B-Tree of Order 7: Delete Leaf delete(50) 10 80 2 4 6 20 30 40 50 60 70 82 84 86 88
B-Tree of Order 3: Delete Leaf 50 delete(85) 80 30 20 40 60 90 10 25 35 44 55 70 82 85 95
6 10 20 B-Tree of Order 7: Delete delete(25) 10 40 80 2 3 4 6 20 25 30 50 60 70 82 84 86 88
10 80 20 30 40 50 60 70 B-Tree of Order 7: Delete delete(25) 10 40 80 2 3 4 6 20 25 30 50 60 70 82 84 86 88
20 20 B-Tree of Order 3: Delete delete(10) 30 80 20 50 60 90 10 25 35 40 55 70 82 85 95
30 20 30 50 50 B-Tree of Order 3: Delete delete(10) 80 60 90 25 35 40 55 70 82 85 95
40 40 B-Tree of Order 3: Delete 50 delete(44) 30 80 20 40 60 90 10 25 35 44 55 70 82 85 95
30 40 30 B-Tree of Order 3: Delete 50 delete(44) 80 20 60 90 10 25 35 55 70 82 85 95
50 40 30 B-Tree of Order 3: Delete delete(44) 50 80 20 60 90 10 25 35 55 70 82 85 95
B-Tree of Order m: Delete d = ┌m/2┐ ├d-1 ┤ ├d-2 ┤ • (d-1)+(d-2)+1 = 2d-2 • m odd: 2d-2 = m-1 • m even: 2d-2 = m-2 ├? ┤
ei ci ki ci pi ei ki ci ki pi ei Node Structure: B´-Tree So far … • s, c0, (e1, c1), (e2, c2), …, (es, cs) • ei’s elements in ascending order of key • ci’s children pointers But when element size is large relative to key size … • s, c0, (k1, c1, p1), (k2, c2, p2), …, (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 • Leaf nodes replace null children pointers by pi pointers • Relate each kito some pj s, c0, (k1, c1), (k2, c2), …, (ks, cs) s, p0, (k1, p1), (k2, p2), …, (ks, ps)
44 B´-Tree of Order 3 50 30 80 20 40 60 90 10 25 35 55 70 82 85 95