Upcoming SlideShare
×

# Ch16

932 views
839 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
932
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
24
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Ch16

1. 1. Chapter 16 Multi-way Search Trees
2. 2. Chapter Objectives <ul><li>Examine 2-3 and 2-4 trees </li></ul><ul><li>Introduce the generic concept of a B-tree </li></ul><ul><li>Examine some specialized implementations of B-trees </li></ul>
3. 3. Multi-way Search Trees <ul><li>In a multi-way search tree, </li></ul><ul><ul><li>each node may have more than two child nodes </li></ul></ul><ul><ul><li>there is a specific ordering relationship among the nodes </li></ul></ul><ul><li>In this chapter, we examine three forms of multi-way search trees </li></ul><ul><ul><li>2-3 trees </li></ul></ul><ul><ul><li>2-4 trees </li></ul></ul><ul><ul><li>B-trees </li></ul></ul>
4. 4. 2-3 Trees <ul><li>A 2-3 tree is a multi-way search tree in which each node has zero, two, or three children </li></ul><ul><li>A node with zero or two children is called a 2-node </li></ul><ul><li>A node with zero or three children is called a 3-node </li></ul><ul><li>A 2-node contains one element and either has no children or two children </li></ul><ul><ul><li>Elements of the left sub-tree less than the element </li></ul></ul><ul><ul><li>Elements of the right sub-tree greater than or equal to the element </li></ul></ul>
5. 5. 2-3 Trees <ul><li>A 3-node contains two elements, one designated as the smaller and one as the larger </li></ul><ul><li>A 3-node has either no children or three children </li></ul><ul><li>If a 3-node has children then </li></ul><ul><ul><li>Elements of the left sub-tree are less than the smaller element </li></ul></ul><ul><ul><li>The smaller element is less than or equal to the elements of the middle sub-tree </li></ul></ul><ul><ul><li>Elements of the middle sub-tree are less then the larger element </li></ul></ul><ul><ul><li>The larger element is less than or equal to the elements of the right sub-tree </li></ul></ul>
6. 6. 2-3 Trees <ul><li>All of the leaves of a 2-3 tree are on the same level </li></ul><ul><li>Thus a 2-3 tree maintains balance </li></ul>
7. 7. FIGURE 16.1 A 2-3 tree
8. 8. Inserting Elements into a 2-3 Tree <ul><li>All insertions into a 2-3 tree occur at the leaves </li></ul><ul><ul><li>The tree is searched to find the proper leaf for the new element </li></ul></ul><ul><li>Insertion has three cases </li></ul><ul><ul><li>Tree is empty (in which case the new element becomes the root of the tree) </li></ul></ul><ul><ul><li>Insertion point is a 2-node </li></ul></ul><ul><ul><li>Insertion point is a 3-node </li></ul></ul>
9. 9. Inserting Elements into a 2-3 Tree <ul><li>The first of these cases is trivial with the element inserted into a new 2-node that becomes the root of the tree </li></ul><ul><li>The second case occurs when the new element is to be inserted into a 2-node </li></ul><ul><li>In this case, we simply add the element to the leaf and make it a 3-node </li></ul>
10. 10. FIGURE 16.2 Inserting 27
11. 11. Insertion into a 2-3 Tree <ul><li>The third case occurs when the insertion point is a 3-node </li></ul><ul><li>In this case </li></ul><ul><ul><li>the 3 elements (the two old ones and the new one) are ordered </li></ul></ul><ul><ul><li>the 3-node is split into two 2-nodes, one for the smaller element and one for the larger element </li></ul></ul><ul><ul><li>the middle element is promoted (or propagated) up a level </li></ul></ul>
12. 12. Insertion into a 2-3 Tree <ul><li>The promotion of the middle element creates two additional cases: </li></ul><ul><ul><li>The parent of the 3-node is a 2-node </li></ul></ul><ul><ul><li>The parent of the 3-node is a 3-node </li></ul></ul><ul><li>If the parent of the 3-node being split is a 2-node then it becomes a 3-node by adding the promoted element and references to the two resulting two nodes </li></ul>
13. 13. FIGURE 16.3 Inserting 32
14. 14. Insertion into a 2-3 Tree <ul><li>If the parent of the 3-node is itself a 3-node then it also splits into two 2-nodes and promotes the middle element again </li></ul>
15. 15. FIGURE 16.4 Inserting 57
16. 16. FIGURE 16.5 Inserting 25
17. 17. Removing Elements from a 2-3 Tree <ul><li>Removal of elements is also made up of three cases: </li></ul><ul><ul><li>The element to be removed is in a leaf that is a 3-node </li></ul></ul><ul><ul><li>The element to be removed is in a leaf that is a 2-node </li></ul></ul><ul><ul><li>The element to be removed is in an internal node </li></ul></ul>
18. 18. Removing Elements from a 2-3 Tree <ul><li>The simplest case is that the element to be removed is in a leaf that is a 3-node </li></ul><ul><li>In this case the element is simply removed and the node is converted to a 2-node </li></ul>
19. 19. FIGURE 16.6 Removal from a 2-3 tree (case 1)
20. 20. Removing Elements from a 2-3 Tree <ul><li>The second case is that the element to be removed is in a leaf that is a 2-node </li></ul><ul><li>This creates a situation called underflow </li></ul><ul><li>We must rotate the tree and/or reduce the tree’s height in order to maintain the properties of the 2-3 tree </li></ul>
21. 21. Removing Elements from a 2-3 Tree <ul><li>This case can be broken down into four subordinate cases </li></ul><ul><li>The first of these subordinate cases (2.1) is that the parent of the 2-node has a right child that is a 3-node </li></ul><ul><li>In this case, we rotate the smaller element of the 3-node around the parent </li></ul>
22. 22. FIGURE 16.7 Removal from a 2-3 tree (case 2.1)
23. 23. Removing Elements from a 2-3 Tree <ul><li>The second of these subordinate cases (2.2) occurs when the underflow cannot be fixed through a local rotation but there are 3-node leaves in the tree </li></ul><ul><li>In this case, we rotate prior to removal of the element until the right child of the parent is a 3-node </li></ul><ul><li>Then we follow the steps for our previous case (2.1) </li></ul>
24. 24. FIGURE 16.8 Removal from a 2-3 tree (case 2.2)
25. 25. Removal of Elements from a 2-3 Tree <ul><li>The third of these subordinate cases (2.3) occurs when none of the leaves are 3-nodes but there are 3-node internal nodes </li></ul><ul><li>In this case, we can convert an internal 3-node to a 2-node and rotate the appropriate element from that node to rebalance the tree </li></ul>
26. 26. FIGURE 16.9 Removal from a 2-3 tree (case 2.3)
27. 27. Removing Elements from a 2-3 Tree <ul><li>The fourth subordinate case (2.4) occurs when there not any 3-nodes in the tree </li></ul><ul><li>This case forces us to reduce the height of the tree </li></ul><ul><li>To accomplish this, we combine each the leaves with their parent and siblings in order </li></ul><ul><li>If any of these combinations produce more than two elements, we split into two 2-nodes and promote the middle element </li></ul>
28. 28. FIGURE 16.10 Removal from a 2-3 tree (case 2.4)
29. 29. Removing Elements from a 2-3 Tree <ul><li>The third of our original cases is that the element to be removed is in an internal node </li></ul><ul><li>As we did with binary search trees, we can simply replace the element to be removed with its inorder successor </li></ul>
30. 30. FIGURE 16.11 Removal from a 2-3 tree (case 3)
31. 31. 2-4 Trees <ul><li>2-4 Trees are very similar to 2-3 Trees adding the characteristic that a node can contain three elements </li></ul><ul><li>A 4-node contains three elements and has either no children or 4 children </li></ul><ul><li>The same ordering property applies as 2-3 trees </li></ul><ul><li>The same cases apply to both insertion and removal of elements as illustrated on the following slides </li></ul>
32. 32. FIGURE 16.12 Insertions into a 2-4 tree
33. 33. FIGURE 16.13 Removals from a 2-4 tree
34. 34. B-Trees <ul><li>Both 2-3 trees and 2-4 trees are examples of a larger class of multi-way search trees called B-trees </li></ul><ul><li>We refer to the maximum number of children of each node as the order of a B-Tree </li></ul><ul><li>Thus 2-3 trees are 3 B-trees and 2-4 trees are 4 B-trees </li></ul>
35. 35. B-Trees <ul><li>B-trees of order m have the following properties: </li></ul>
36. 36. FIGURE 16.14 A B-tree of order 6
37. 37. Motivation for B-trees <ul><li>B-trees were created to make the most efficient use possible of the relationship between main memory and secondary storage </li></ul><ul><li>For all of the collections we have studied thus far, our assumption has been that the entire collections exists in memory at once </li></ul>
38. 38. Motivation for B-trees <ul><li>Consider the case where the collection is too large to exist in primary memory at one time </li></ul><ul><li>Depending upon the collection, the overhead associated with reading and writing from files and/or swapping large segments of memory in and out could be devastating </li></ul>
39. 39. Motivation for B-trees <ul><li>B-trees were designed to flatten the tree structure and to allow for larger blocks of data that could then be tuned so that the size of a node is the same size as a block on secondary storage </li></ul><ul><li>This reduces the number of nodes and/or blocks that must be accessed, thus improving performance </li></ul>
40. 40. B*-trees <ul><li>A variation of B-trees called B*-trees were created to solve the problem that the B-tree could be half empty at any given time </li></ul><ul><li>B*-trees have all of the same properties as B-trees except that, instead of each node having k children where m/2 ≤ k ≤ m, in a B*-tree, each node has k children where (2m–1)/3 ≤ k ≤ m </li></ul><ul><li>This means that each non-root node is at least two-thirds full </li></ul>
41. 41. B+-trees <ul><li>Another potential problem for B-trees is sequential access </li></ul><ul><li>B+-trees provide a solution to this problem by requiring that each element appear in a leaf regardless of whether it appears in an internal node </li></ul><ul><li>By requiring this and then linking the leaves together, B+-trees provide very efficient sequential access while maintaining many of the benefits of a tree structure </li></ul>
42. 42. FIGURE 16.15 A B+-tree of order 6
43. 43. Implementation Strategies for B-trees <ul><li>A good implementation strategy for B-trees is to think of each node as a pair of arrays </li></ul><ul><ul><li>An array of m-1 elements </li></ul></ul><ul><ul><li>An array of m children </li></ul></ul><ul><li>Then, if we think of the tree itself as one large array of nodes, then the elements stored in the array of children would simply be integer indexes into the array of nodes </li></ul>