1 / 50

I-APG1, Lektion 8

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

amccray
Download Presentation

I-APG1, Lektion 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. I-APG1, Lektion 8 B-Trees • Ch. 16: • Oplæg: 16.4 • Opgave: 57, 59

  2. 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)

  3. 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)

  4. 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 … …

  5. m-Way Search Tree 1 2 pi … k1 k2 ki … … 1 2 pj … k'1 k '2 k'j … …

  6. m-Way Search Tree • Search • Insert • Delete • Height • IsEmpty • … …examples to follow …

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. B-Tree of Order 7 10 80 2 4 6 20 30 40 50 60 70 82 84 86 88

  19. 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

  20. B-Tree of Order m • Search • Insert • Delete • Height • IsEmpty • …

  21. 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

  22. 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)

  23. 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

  24. 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

  25. B-Tree of Order 3: Insert insert(44) 30 80 20 50 60 90 10 25 35 40 55 70 82 85 95

  26. B-Tree of Order 3: Insert insert(44) 30 80 20 40 50 60 90 10 25 35 44 55 70 82 85 95

  27. B-Tree of Order 3: Insert insert(44) 30 80 50 20 40 60 90 10 25 35 44 55 70 82 85 95

  28. B-Tree of Order 3: Insert 50 insert(44) 30 80 20 40 60 90 10 25 35 44 55 70 82 85 95

  29. 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

  30. ed ed B-Tree of Order m: Insert ├m ┤ P ed ├m+1 ┤ P Q d = ┌m/2┐ ├≥ d ┤ ├≥ d ┤

  31. 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

  32. 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)

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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 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

  41. 30 40 30 B-Tree of Order 3: Delete 50 delete(44) 80 20 60 90 10 25 35 55 70 82 85 95

  42. 50 40 30 B-Tree of Order 3: Delete delete(44) 50 80 20 60 90 10 25 35 55 70 82 85 95

  43. 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 ├? ┤

  44. 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)

  45. 44 B´-Tree of Order 3 50 30 80 20 40 60 90 10 25 35 55 70 82 85 95

More Related