560 likes | 800 Views
Chapter 10. Tree Projects. Chapter Outline. Objectives List the rules for a heap or B-tree and determine whether a tree satisfies these rules Insert a new element into a heap or remove the largest element
E N D
Chapter 10 Tree Projects
Chapter Outline • Objectives • List the rules for a heap or B-tree and determine whether a tree satisfies these rules • Insert a new element into a heap or remove the largest element • Do a simulation by hand of the algorithm for searching, inserting and removing from B-tree • Use the heap and B-tree data structre • Recognize which operations have logarithmic worst-case performance on balanced trees • Contents • Heaps • B-Trees • Tree, Logs, and Time Analysis Data Structure
Heaps • Is a binary tree in which the elements can be compared with each other using total order semantics • All the elements in the class can be placed in a single line, proceeding from smaller to larger Data Structure
Heap Storage Rules • Heap is a binary tree which these two rules are followed • The element contained by each node is greater than or equal to the elements of that node’s children • The tree is complete binary tree so that every level except the deepest must contain as many nodes as possible • Page 503 Data Structure
The Priority Queue ADT with Heaps • Each node of the heap contains one element with the priority • Rule • The element contained by each node has a priority that is greater than or equal to the priorities of the elements of that node’s children • The tree is a complete binary tree Data Structure
The Priority Queue ADT with Heaps • Adding an Element to a Heap 45 35 23 27 21 22 4 19 4 Data Structure
The Priority Queue ADT with Heaps • Adding an Element to a Heap 45 35 23 27 21 22 4 19 4 42 This is no longer a heap ! Data Structure
The Priority Queue ADT with Heaps • Adding an Element to a Heap 45 35 23 27 42 22 4 19 4 21 Data Structure
45 42 23 27 35 22 4 19 4 21 The Priority Queue ADT with Heaps • Adding an Element to a Heap “New element rising” stops when the new element has a parent with a higher or equal priority or when the new element reaches the root Reheapification upward Data Structure
The Priority Queue ADT with Heaps • Pseudocode for adding an Element • Place the new element in the heap in the first available location. This is a complete binary tree but not a heap • While( the new element has a priority that is higher than its parent) • Swap the new element with its parent Data Structure
The Priority Queue ADT with Heaps • Removing an Element from a Heap • When an element is removed from a priority queue, we must always remove the element with the highest priority = “Top of the heap” 45 42 23 27 35 22 4 19 4 21 Data Structure
The Priority Queue ADT with Heaps • Removing an Element from a Heap 45 21 42 23 42 23 27 35 22 4 27 35 22 4 19 4 21 19 4 The last element in the last level Has been moved to the root Data Structure
The Priority Queue ADT with Heaps • Removing an Element from a Heap 42 21 23 27 35 22 4 19 4 Data Structure
42 35 23 27 21 22 4 19 4 The Priority Queue ADT with Heaps • Removing an Element from a Heap The node that has been sinking has reached a leaf, we can stop and the structure is a heap Reheapification downward Data Structure
The Priority Queue ADT with Heaps • Pseudocode for Removing an Element • Copy the element at the root of the heap to the variable used to return a value • Copy the last element in the deepest level to the root and then take this last node out of the tree • While (the out-of-place element has a priority that is lower than one of its children) • Swap the out-of-place element with its highest-priority child • Return the answer that was saved in Step 1 Data Structure
1 1 2 2 3 3 4 4 5 6 B-Trees • The problem of Unbalanced trees • Solution : • Balance the search trees • periodically • 2. Use a particular kind of • B-tree Data Structure
B-Tree • Is a special kind of tree • Is not binary search tree • Has many more than two children • Each node contains more than just a single element Data Structure
B-Tree Rules • The element in a B-Tree Node • MINIMUM : determine how many elements are held in a single node • B-tree Rule 1 • The root can have as few as one element; Every other node has at least MINIMUM elements • B-tree Rule 2 • The maximum number of elements in a node is twice the value of MINIMUM • B-Tree Rule 3 • The element of each B-tree node are stored in a partially filled array, sorted from the smallest element to the largest element Data Structure
B-Tree Rules • The Subtrees below a B-Tree Node • The number of subtrees below a node depends on how many elements are in the node • B-tree Rule 4 • The number of subtrees below a nonleaf node is always one more than the number or elements in the node • B-tree Rule 5 • For any nonleaf node: (a) An element at index i is greater than all the elements in subtree number i of the node and (b) An element at index i is less than all the elements in subtree number i+1 of the node Data Structure
93 and 107 Subtree Number 0 Subtree Number 1 Subtree Number 2 B-Tree Rules • The Subtrees below a B-Tree Node • B-tree 4 & 5 Each element in subtree Number 0 is less than 93 Each element in subtree Number 2 is greater Than 107 Each element in subtree Number 1 is between 93 and 107 Data Structure
6 2 and 4 9 1 3 5 7 and 8 10 B-Tree Rules • A B-Tree Is Balanced • B-tree Rule 6 • Every leaf in a B-tree has the same depth • An example B-Tree Data Structure
6 2 and 4 9 1 3 5 7 and 8 10 The Set ADT with B-Trees • Property that will allow us to use recursive thinking • Every child of the root node is also the root of a smaller B-tree A smaller B-tree on the right A smaller B-tree on the left Data Structure
The Set ADT with B-Trees • Invariant for the Set ADT implemented with a B-Tree • The elements of the set are stored in a B-tree, satisfying the six B-tree rules • The number of elements in the tree’s root is stored in the instance variable dataCount, and the number of subtrees of the root is stored in the instance variable childCount • The root’s elements are stored in data[0] through data[dataCount-1] • If the root has subtrees, then subset[0] through subset[childCount-1] are references to these subtrees Data Structure
The Set ADT with B-Trees public class IntBalancedSet implements Cloneable { private static final int MINIMUM = 200; private static final int MAXIMUM = 2 * MINIMUM; int dataCount; int[] data = new int[MAXIMUM + 1]; int childCount; IntBalancedSet[] subset = new IntBalancedSet[MAXIMUM + 2]; } Data Structure
6 2 and 4 9 6 ? ? 1 3 5 7 and 8 10 The Set ADT with B-Trees • How to store the subsets Each subset[I] is a reference to another Int-BalancedSet object 1 dataCount data [0] [1] [2] 2 null null childCount subset [0] [1] [2] [3] Refers to a Smaller set Object that contains 1, 2, 3, 4 and 5 Refers to a smaller Set object that Contains 7, 8, 9, 10 Data Structure
The Set ADT with B-Trees • Searching for an Element in a B-Tree • Make a local variable, i, equal to the first index such that data[i] >= target. If there is no such index, then set i equal to dataCount, indicating that none of the elements is greater than or equal to the target • If (we found the target at data[i]) return true; else if (the root has no children) return false; else return subset[i].contains(target); Data Structure
6 and 17 6 and 17 4 4 12 12 19 and 22 19 and 22 2 and 3 2 and 3 5 5 10 10 16 16 18 18 20 20 25 25 The Set ADT with B-Trees • Suppose we are searching for 10 Data Structure
The Set ADT with B-Trees • Suppose we are searching for 10 6 and 17 4 12 19 and 22 2 and 3 5 10 16 18 20 25 Data Structure
The Set ADT with B-Trees • Adding an Element to a B-Tree • It is easier to add a new element to a B-tree if we relax one of the B-tree rules • “loose addition” • There is one element too many in the root node of the B-tree Data Structure
The Set ADT with B-Trees • Adding an Element to a B-Tree • Consider this B-tree where MAXIMUM is 2 • Add 18 to the tree 6 and 17 4 12 19 and 22 6, 17, 19 4 12 18 22 B-Tree is illegal because the root node has three elements. Only the root may have an extra element after the “loose addition” Adding plan : loose addition -> fix the loose addtion Data Structure
The Set ADT with B-Trees • The Loose Addition Operation for a B-tree private void looseAdd(int element) // Precondition: The entire B-tree is valid // Postcondition: If the element was already in this set, then this set is // unchanged. Otherwise, the element has been added to this set, and // the entire B-tree is still valid EXCEPT that the number of elements in // the root of this set might be one more than the allowed maximum Data Structure
The Set ADT with B-Trees • The Loose Addition Operation for a B-tree • Make a local variable, i, equal to the first index such that data[i] >= element. If there is no such index, then set i equal to dataCount, indicating that none of the elements is greater than or equal to the target • If (we found the new element at data[i]) 2a. Return with no further work else if (the root has no children) 2b. Add the new element to the root at data[i] else { 2c. subset[i].looseAdd(element); If the root of subset[i] now has an excess element, then fix that problem before returning } Data Structure
The Set ADT with B-Trees • We are making a loose addition of 18 into this B-tree 6 and 17 4 12 19 and 22 6 and 17 4 12 19 and 22 6 and 17 4 12 18, 19, 22 Data Structure
The Set ADT with B-Trees • A Private Method to Fix an Excess in a Child Private void fixExcess(int i) // Precondition: (i<childCount) and the entire B- // tree is valid EXCEPT that subset[i] has // MAXIMUM+1 elements // Postcondition: The tree has been rearranged so // that the entire B-tree is valid EXCEPT that the // number of elements in the root of this set might be // one more than allowed maximum Data Structure
The Set ADT with B-Trees • Fixing a Child with an Excess Element • To fix child with MAXIMUM+1 elements, the child node is split into two nodes that each contain MINIMUM elements. • This leaves one extra element, which is passed up to the parent Data Structure
The Set ADT with B-Trees • Fixing a Child with an Excess Element MINIMUM = 2 Maximum number of elements is 4 9, 28 3, 6 13, 16, 19, 22, 25 33, 40 1, 2 4,5 7,8 11,12 14,15 17,18 20,21 23,24 26,27 31,32 34,35 50,51 The full node has been split into two nodes, and the middle element (19) has been passed upward 9, 19, 28 3, 6 13, 16 22, 25 33, 40 1, 2 4,5 7,8 11,12 14,15 17,18 20,21 23,24 26,27 31,32 34,35 50,51 Data Structure
The Set ADT with B-Trees • Another example of fixExcess MAXIMUM = 2 Subset[2] has an Excess element 6 and 17 4 12 18, 19, 22 6 , 17, 19 4 22 12 18 Data Structure
The Set ADT with B-Trees • Back to the add Method • How do we “fix the root of the entire tree so that it no longer has too many elements”? 6 , 17, 19 4 22 12 18 17 The new root has no elements 6 19 6 , 17, 19 22 18 12 4 22 4 12 18 B-trees gain height only at the root Data Structure
The Set ADT with B-Trees • Removing an Element from a B-Tree • Performs a “loose removal” • It is allowed to leave a root that has one element too few Private boolean looseRemove(int target) // Precondition: The entire B-tree is valid // Postcondition: If target was in this set, then it // has been removed from this set and the method // returns true; otherwise this set is unchanged and // the method returns false. The entire B-tree is // still valid EXCEPT that the number of elements // in the root of this set might be one less than the // allowed minimum Data Structure
The Set ADT with B-Trees • Removing an Element from a B-Tree answer = looseRemove(target); If ((dataCount == 0) && (childCount == 1)) Fix the root of the entire tree so that it no longer has zero elements Return answer; • How do we “fix the root of the entire tree so that it no longer has zero elements”? The root has no Elements and one child 6 and 17 6 and 17 4 12 19, 22 4 12 19, 22 B-trees lose height only at the root Data Structure
The Set ADT with B-Trees • The Loose Removal from B-Tree • Make a local variable, i, equal to the first index such that data[i] >= target. If there is no such index, then set i equal to dataCount, indicating that none of the elements is greater than or equal to the target • Deal with one of these four possibility: 2a. The root has no children, and we did not find the target: In this case, there is no work to do, and the method returns false 2b. The root has no children, and we found the target: In this case, remove the target from the data array and return true 2c. The root has children, and we did not find the target 2d. The root has children, and we found the target Data Structure
The Set ADT with B-Trees • The Loose Removal from B-Tree • Case 2c : we did not find the target in the root but the target still might appear in subset[i] 1. answer = subset[i].looseRemove(target); • The problem that the root of subset[i] might have only MINIMUM – 1 elements exists 2. private void fixShortage(int i) // precondition: (i<childCount) and the entire B-tree is // valid EXCEPT that subset[i] has MINIMUM –1 elements // postcondition: The tree has been rearranged so that the // entire B-tree is valid EXCEPT that the number of elements in // the root of this set might be one less than the allowed // minimum Data Structure
The Set ADT with B-Trees • The Loose Removal from B-Tree • Case 2d: • We will go into subset[i] and remove the largest element in this subset • Take a copy of this largest element and place it into data[i] We have found the target 28 at data[1] in the root of this B-tree 10, 28 2, 5 13, 16, 19, 22 33, 40 0,1 3,4 6,7,8 11,12 14,15 17,18 20,21 23,24,26 31,32 34,35 50,51 Data Structure
10, 26 2, 5 13, 16, 19, 22 33, 40 0,1 3,4 6,7,8 11,12 14,15 17,18 20,21 23,24 31,32 34,35 50,51 The Set ADT with B-Trees • The Loose Removal from B-Tree • Case 2d: Our plan : go into subset[1], remove the largest element(the 26) And place a copy of this 26 on top of the target • Remove the largest element and place a copy of the largest element into data[i] private int removeBiggest() // Precondition: (dataCount>0) and this entire B-tree is valid // Postcondition: The largest element in this set has been remove, and the // return value is this removed element. The entire B-tree is still valid, Except // that the number of elements in the root of this set might be one less than // the allowed minimum Data Structure
The Set ADT with B-Trees • The Loose Removal from B-Tree • Case 2d: • data[i] = subset[i].removeBiggest(); • The work that remains is to fix the possible shortage that may occur in the root of subset[i] • fixshortage(i); data[i] = subset[i].removeBiggest(); if (subset[i].dataCount < MINIMUM) fixshortage(i); return true; Data Structure
The Set ADT with B-Trees • A Private Method to Fix a shortage in a Child • Case 1 of fixShortage: Transfer an Extra Element from subset[i-1] • Suppose subset[i-1] has more than the minimum number of elements • Transfer data[i-1] down to the front of subset[i].data. Shift over the existing elements to make room and andd one to subset[i].dataCount • Transfer the final element of subset[i-1].data up to replace data[i-1] and subtract one from subset[i-1].dataCount • If subset[i-1] has children, transfer the final child of subset[i-1] over to the front of subset[i]. Add one to subset[i].childcount and subtract one from subset[i-1].childCount Data Structure
The Set ADT with B-Trees • Case 1 of fixShortage: Transfer an Extra Element from subset[i-1] 10, 28 2, 5 13, 16, 19, 22 33 0,1 3,4 6,7,8 11,12 14,15 17,18 20,21 23,24,26 31,32 34,35 This 22 has come up from The middle child 10, 22 This 28 has come down from the root 2, 5 13, 16, 19 28, 33 0,1 3,4 6,7,8 11,12 14,15 17,18 20,21 23,24,26 31,32 34,35 This child has been moved over Data Structure
The Set ADT with B-Trees • Case 2 for fixShortage: Transfer an Extra Element from subset[i+1] • Similar to what you have seen for transferring an element from subset[i-1] • Case 3 for fixShortage: Combine subset[i] with subset[i-1] • Suppose subset[i-1] is present but it has only MINIMUM elements • Transfer data[i-1] down to the end of subset[i-1].data. Subtrack one from dataCount and add one to subset[i-1].dataCount • Transfer all the elements and children from subset[i] to the endof subset[i-1]. Update the values of subset[i-1].dataCount and subset[i-1].childCount • Disconnect the node subset[i] from the B-tree. Reduce childCount by one Data Structure
The Set ADT with B-Trees • Case 3 for fixShortage: Combine subset[i] with subset[i-1] Let’s activate fixShortage(2) 10, 28 2, 5 16, 19 33 0,1 3,4 6,7,8 14,15 17,18 20,21 31,32 34,35 10 2, 5 Subset[2] is merged with its sibling to the left 16, 19, 28, 33 0,1 3,4 6,7,8 14,15 17,18 20,21 31,32 34,35 Data Structure
The Set ADT with B-Trees • Case 3 for fixShortage: Combine subset[i] with subset[i+1] • Similar to what you have seen for combining with subset[i-1] Data Structure