380 likes | 649 Views
Heaps. Chapter 10 has several programming projects, including a project that uses heaps . This presentation shows you what a heap is, and demonstrates two of the important heap algorithms. This chapter also takes a look at B-trees. Data Structures and Other Objects Using Java. Heaps.
E N D
Heaps • Chapter 10 has several programming projects, including a project that uses heaps. • This presentation shows you what a heap is, and demonstrates two of the important heap algorithms. • This chapter also takes a look at B-trees Data Structures and Other Objects Using Java
Heaps A heap is a certain kind of complete binary tree.
Heaps Root A heap is a certain kind of complete binary tree. When a complete binary tree is built, its first node must be the root.
Heaps Left child of the root Complete binary tree. The second node is always the left child of the root.
Heaps Right child of the root Complete binary tree. The third node is always the right child of the root.
Heaps Complete binary tree. The next nodes always fill the next level from left-to-right.
Heaps Complete binary tree. The next nodes always fill the next level from left-to-right.
Heaps Complete binary tree. The next nodes always fill the next level from left-to-right.
Heaps Complete binary tree. The next nodes always fill the next level from left-to-right.
Heaps Complete binary tree.
Heaps 45 A heap is a certain kind of complete binary tree. 35 23 27 21 22 4 19 Each node in a heap contains a key that can be compared to other nodes' keys.
Heaps 45 A heap is a certain kind of complete binary tree. 35 23 27 21 22 4 19 The "heap property" requires that each node's key is >= the keys of its children
Adding a Node to a Heap 45 • Put the new node in the next available spot. • Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 35 23 27 21 22 4 19 42
Adding a Node to a Heap 45 • Put the new node in the next available spot. • Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 35 23 42 21 22 4 19 27
Adding a Node to a Heap 45 • Put the new node in the next available spot. • Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 42 23 35 21 22 4 19 27
Adding a Node to a Heap 45 • The parent has a key that is >= new node, or • The node reaches the root. • The process of pushing the new node upward is called reheapificationupward. 42 23 35 21 22 4 19 27
Removing the Top of a Heap 45 • Move the last node onto the root. 42 23 35 21 22 4 19 27
Removing the Top of a Heap 27 • Move the last node onto the root. 42 23 35 21 22 4 19
Removing the Top of a Heap 27 • Move the last node onto the root. • Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 42 23 35 21 22 4 19
Removing the Top of a Heap 42 • Move the last node onto the root. • Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 27 23 35 21 22 4 19
Removing the Top of a Heap 42 • Move the last node onto the root. • Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 35 23 27 21 22 4 19
Removing the Top of a Heap 42 • The children all have keys <= the out-of-place node, or • The node reaches the leaf. • The process of pushing the new node downward is called reheapificationdownward. 35 23 27 21 22 4 19
We will store the data from the nodes in a partially-filled array. Implementing a Heap 42 35 23 27 21 An array of data
Data from the root goes in the first location of the array. Implementing a Heap 42 35 23 27 21 42 An array of data
Data from the next row goesin the next two array locations. Implementing a Heap 42 35 23 27 21 42 35 23 An array of data
Data from the next row goesin the next two array locations. Implementing a Heap 42 35 23 27 21 42 35 23 27 21 An array of data
Data from the next row goesin the next two array locations. Implementing a Heap 42 35 23 27 21 42 35 23 27 21 An array of data We don't care what's in this part of the array.
The links between the tree's nodes are not actually stored as pointers, or in any other way. The only way we "know" that "the array is a tree" is from the way we manipulate the data. Important Points about the Implementation 42 35 23 27 21 42 35 23 27 21 An array of data
If you know the index of a node, then it is easy to figure out the indexes of that node's parent and children. Formulas are given in the book. Important Points about the Implementation 42 35 23 27 21 42 35 23 27 21 [0] [1] [2] [3] [4]
Summary • A heap is a complete binary tree, where the entry at each node is greater than or equal to the entries in its children. • To add an entry to a heap, place the new entry at the next available spot, and perform a reheapification upward. • To remove the biggest entry, move the last node onto the root, and perform a reheapification downward.
Depth of a binary search tree • The first tree has a large depth that would not have to be if it was like the more balanced second tree Count 1 6 5 Count 1 4 100 3 100 1 6 150 1 4 6 150 2 2 4 6 5 4 Count 2 Count 2 3
B-trees • Problem of Unbalanced Trees • Solutions • All involve trees whose depth remains small • Could balance trees periodically • AVL trees • Red-Black Trees • We’ll look at B-trees
B-Tree Rules • Depends on a positive constant integer called MINIMUM • Rule 1: The root may have as few as one element (or none if no children) ; every other node has at least MINIMUM elements • Rule 2: The maximum number of elements in a node is twice the value of MINIMUM
More rules about B-tree • Rule 3: The elements of each B-tree node are stored in a partially filled array, sorted from the smallest element (at index 0) to the largest element (at the final position of the array) • Rule 4: The number of subtrees below node depends on how many elements are in a node: always one more
Subtrees below a B-tree node • Rule 5: For any non-leaf node • An element at index i is greater than all the elements in subtree number i of the node • An element at index i is less than all the elements in subtree number i+1 of the node A B-tree is balanced • Rule 6: Every leaf in a B-tree has the same depth
Sample B-tree 6 2 and 4 9 7 and 8 5 10 1 3 Every child of the root node is also the root node of a smaller B-tree
Non-leaf node with two elements 93 and 107 Subtree 0 Subtree 1 Subtree 2
Searching for a number dataCount =1 childCount=2 Subset [0] 6 Subset[1] 2 and 4 9 7 and 8 5 10 1 3