chapter 12: multi-way search trees
DESCRIPTION
CHAPTER 12: Multi-way Search Trees. Java Software Structures: Designing and Using Data Structures Third Edition John Lewis & Joseph Chase. Chapter Objectives. Examine 2-3 and 2-4 trees Introduce the generic concept of a B-tree Examine some specialized implementations of B-trees. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/1.jpg)
© 2010 Pearson Addison-Wesley. All rights reserved.
Addison Wesley is an imprint of
CHAPTER 12: Multi-way Search Trees
Java Software Structures:Designing and Using Data Structures
Third Edition
John Lewis & Joseph Chase
![Page 2: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/2.jpg)
1-2
© 2010 Pearson Addison-Wesley. All rights reserved. 1-2
Chapter Objectives
• Examine 2-3 and 2-4 trees• Introduce the generic concept of a B-tree• Examine some specialized
implementations of B-trees
![Page 3: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/3.jpg)
1-3
© 2010 Pearson Addison-Wesley. All rights reserved. 1-3
Multi-way Search Trees
• In a multi-way search tree, – each node may have more than two child nodes– there is a specific ordering relationship among the
nodes
• In this chapter, we examine three forms of multi-way search trees– 2-3 trees– 2-4 trees– B-trees
![Page 4: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/4.jpg)
1-4
© 2010 Pearson Addison-Wesley. All rights reserved. 1-4
2-3 Trees
• A 2-3 tree is a multi-way search tree in which each node has zero, two, or three children
• A node with zero or two children is called a 2-node• A node with zero or three children is called a 3-node• A 2-node contains one element and either has no
children or two children– Elements of the left sub-tree less than the element– Elements of the right sub-tree greater than or equal to the
element
![Page 5: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/5.jpg)
1-5
© 2010 Pearson Addison-Wesley. All rights reserved. 1-5
2-3 Trees
• A 3-node contains two elements, one designated as the smaller and one as the larger
• A 3-node has either no children or three children• If a 3-node has children then
– Elements of the left sub-tree are less than the smaller element– The smaller element is less than or equal to the elements of the
middle sub-tree– Elements of the middle sub-tree are less then the larger element– The larger element is less than or equal to the elements of the
right sub-tree
![Page 6: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/6.jpg)
1-6
© 2010 Pearson Addison-Wesley. All rights reserved. 1-6
2-3 Trees
• All of the leaves of a 2-3 tree are on the same level
• Thus a 2-3 tree maintains balance
![Page 7: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/7.jpg)
1-7
© 2010 Pearson Addison-Wesley. All rights reserved. 1-7
A 2-3 tree
![Page 8: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/8.jpg)
1-8
© 2010 Pearson Addison-Wesley. All rights reserved. 1-8
Inserting Elements into a 2-3 Tree
• All insertions into a 2-3 tree occur at the leaves– The tree is searched to find the proper leaf for the new
element
• Insertion has three cases– Tree is empty (in which case the new element
becomes the root of the tree)– Insertion point is a 2-node– Insertion point is a 3-node
![Page 9: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/9.jpg)
1-9
© 2010 Pearson Addison-Wesley. All rights reserved. 1-9
Inserting Elements into a 2-3 Tree
• The first of these cases is trivial with the element inserted into a new 2-node that becomes the root of the tree
• The second case occurs when the new element is to be inserted into a 2-node
• In this case, we simply add the element to the leaf and make it a 3-node
![Page 10: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/10.jpg)
1-10
© 2010 Pearson Addison-Wesley. All rights reserved. 1-10
Inserting 27
![Page 11: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/11.jpg)
1-11
© 2010 Pearson Addison-Wesley. All rights reserved. 1-11
Insertion into a 2-3 Tree
• The third case occurs when the insertion point is a 3-node
• In this case – the 3 elements (the two old ones and the new one) are
ordered– the 3-node is split into two 2-nodes, one for the
smaller element and one for the larger element– the middle element is promoted (or propagated) up a
level
![Page 12: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/12.jpg)
1-12
© 2010 Pearson Addison-Wesley. All rights reserved. 1-12
Insertion into a 2-3 Tree
• The promotion of the middle element creates two additional cases:– The parent of the 3-node is a 2-node– The parent of the 3-node is a 3-node
• 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
![Page 13: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/13.jpg)
1-13
© 2010 Pearson Addison-Wesley. All rights reserved. 1-13
Inserting 32
![Page 14: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/14.jpg)
1-14
© 2010 Pearson Addison-Wesley. All rights reserved. 1-14
Insertion into a 2-3 Tree
• 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
![Page 15: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/15.jpg)
1-15
© 2010 Pearson Addison-Wesley. All rights reserved. 1-15
Inserting 57
![Page 16: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/16.jpg)
1-16
© 2010 Pearson Addison-Wesley. All rights reserved. 1-16
Inserting 25
![Page 17: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/17.jpg)
1-17
© 2010 Pearson Addison-Wesley. All rights reserved. 1-17
Removing Elements from a 2-3 Tree
• Removal of elements is also made up of three cases:– The element to be removed is in a leaf that is
a 3-node– The element to be removed is in a leaf that is
a 2-node– The element to be removed is in an internal
node
![Page 18: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/18.jpg)
1-18
© 2010 Pearson Addison-Wesley. All rights reserved. 1-18
Removing Elements from a 2-3 Tree
• The simplest case is that the element to be removed is in a leaf that is a 3-node
• In this case the element is simply removed and the node is converted to a 2-node
![Page 19: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/19.jpg)
1-19
© 2010 Pearson Addison-Wesley. All rights reserved. 1-19
Removal from a 2-3 tree (case 1)
![Page 20: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/20.jpg)
1-20
© 2010 Pearson Addison-Wesley. All rights reserved. 1-20
Removing Elements from a 2-3 Tree
• The second case is that the element to be removed is in a leaf that is a 2-node
• This creates a situation called underflow• We must rotate the tree and/or reduce the
tree’s height in order to maintain the properties of the 2-3 tree
![Page 21: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/21.jpg)
1-21
© 2010 Pearson Addison-Wesley. All rights reserved. 1-21
Removing Elements from a 2-3 Tree
• This case can be broken down into four subordinate cases
• 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
• In this case, we rotate the smaller element of the 3-node around the parent
![Page 22: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/22.jpg)
1-22
© 2010 Pearson Addison-Wesley. All rights reserved. 1-22
Removal from a 2-3 tree (case 2.1)
![Page 23: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/23.jpg)
1-23
© 2010 Pearson Addison-Wesley. All rights reserved. 1-23
Removing Elements from a 2-3 Tree
• 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
• In this case, we rotate prior to removal of the element until the right child of the parent is a 3-node
• Then we follow the steps for our previous case (2.1)
![Page 24: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/24.jpg)
1-24
© 2010 Pearson Addison-Wesley. All rights reserved. 1-24
Removal from a 2-3 tree (case 2.2)
![Page 25: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/25.jpg)
1-25
© 2010 Pearson Addison-Wesley. All rights reserved. 1-25
Removal of Elements from a 2-3 Tree
• The third of these subordinate cases (2.3) occurs when none of the leaves are 3-nodes but there are 3-node internal nodes
• 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
![Page 26: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/26.jpg)
1-26
© 2010 Pearson Addison-Wesley. All rights reserved. 1-26
Removal from a 2-3 tree (case 2.3)
![Page 27: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/27.jpg)
1-27
© 2010 Pearson Addison-Wesley. All rights reserved. 1-27
Removing Elements from a 2-3 Tree
• The fourth subordinate case (2.4) occurs when there not any 3-nodes in the tree
• This case forces us to reduce the height of the tree
• To accomplish this, we combine each the leaves with their parent and siblings in order
• If any of these combinations produce more than two elements, we split into two 2-nodes and promote the middle element
![Page 28: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/28.jpg)
1-28
© 2010 Pearson Addison-Wesley. All rights reserved. 1-28
Removal from a 2-3 tree (case 2.4)
![Page 29: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/29.jpg)
1-29
© 2010 Pearson Addison-Wesley. All rights reserved. 1-29
Removing Elements from a 2-3 Tree
• The third of our original cases is that the element to be removed is in an internal node
• As we did with binary search trees, we can simply replace the element to be removed with its inorder successor
![Page 30: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/30.jpg)
1-30
© 2010 Pearson Addison-Wesley. All rights reserved. 1-30
Removal from a 2-3 tree (case 3)
![Page 31: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/31.jpg)
1-31
© 2010 Pearson Addison-Wesley. All rights reserved. 1-31
2-4 Trees
• 2-4 Trees are very similar to 2-3 Trees adding the characteristic that a node can contain three elements
• A 4-node contains three elements and has either no children or 4 children
• The same ordering property applies as 2-3 trees• The same cases apply to both insertion and
removal of elements as illustrated on the following slides
![Page 32: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/32.jpg)
1-32
© 2010 Pearson Addison-Wesley. All rights reserved. 1-32
Insertions into a 2-4 tree
![Page 33: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/33.jpg)
1-33
© 2010 Pearson Addison-Wesley. All rights reserved. 1-33
Removals from a 2-4 tree
![Page 34: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/34.jpg)
1-34
© 2010 Pearson Addison-Wesley. All rights reserved. 1-34
B-Trees
• Both 2-3 trees and 2-4 trees are examples of a larger class of multi-way search trees called B-trees
• We refer to the maximum number of children of each node as the order of a B-Tree
• Thus 2-3 trees are 3 B-trees and 2-4 trees are 4 B-trees
![Page 35: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/35.jpg)
1-35
© 2010 Pearson Addison-Wesley. All rights reserved. 1-35
B-Trees
• B-trees of order m have the following properties:
![Page 36: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/36.jpg)
1-36
© 2010 Pearson Addison-Wesley. All rights reserved. 1-36
A B-tree of order 6
![Page 37: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/37.jpg)
1-37
© 2010 Pearson Addison-Wesley. All rights reserved. 1-37
Motivation for B-trees
• B-trees were created to make the most efficient use possible of the relationship between main memory and secondary storage
• For all of the collections we have studied thus far, our assumption has been that the entire collections exists in memory at once
![Page 38: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/38.jpg)
1-38
© 2010 Pearson Addison-Wesley. All rights reserved. 1-38
Motivation for B-trees
• Consider the case where the collection is too large to exist in primary memory at one time
• 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
![Page 39: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/39.jpg)
1-39
© 2010 Pearson Addison-Wesley. All rights reserved. 1-39
Motivation for B-trees
• 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
• This reduces the number of nodes and/or blocks that must be accessed, thus improving performance
![Page 40: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/40.jpg)
1-40
© 2010 Pearson Addison-Wesley. All rights reserved. 1-40
B*-trees
• 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
• 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
• This means that each non-root node is at least two-thirds full
![Page 41: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/41.jpg)
1-41
© 2010 Pearson Addison-Wesley. All rights reserved. 1-41
B+-trees
• Another potential problem for B-trees is sequential access
• 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
• 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
![Page 42: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/42.jpg)
1-42
© 2010 Pearson Addison-Wesley. All rights reserved. 1-42
A B+-tree of order 6
![Page 43: CHAPTER 12: Multi-way Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062304/568145ef550346895db2f381/html5/thumbnails/43.jpg)
1-43
© 2010 Pearson Addison-Wesley. All rights reserved. 1-43
Implementation Strategies for B-trees
• A good implementation strategy for B-trees is to think of each node as a pair of arrays– An array of m-1 elements– An array of m children
• 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