690 likes | 843 Views
Heaps and Heapsort. Lecture 20. Prof. Sin-Min Lee Department of Computer Science San Jose State University. Heaps. Heap Definition. Adding a Node. Removing a Node. Array Implementation. Analysis Source Code. What is Heap?.
E N D
Heaps and Heapsort Lecture 20 • Prof. Sin-Min Lee • Department of Computer Science • San Jose State University
Heaps • Heap Definition. • Adding a Node. • Removing a Node. • Array Implementation. • Analysis • Source Code.
What is Heap? A heap is like a binary search tree. It is a binary tree where the six comparison operators form a total ordersemantics that can be used to compare the node’s entries. But the arrangement of the elements in a heap follows some new rules that are different from a binary search tree.
Heaps A heap is a certain kind of complete binary tree. It is defined by the following properties.
Heaps (Priority Queues) A heap is a data structure which supports efficient implementation of three operations: insert, findMin, and deleteMin. (Such a heap is a min-heap, to be more precise.) Two obvious implementations are based on an array (assuming the size n is known) and on a linked list. The comparisons of their relative time complexity are as follows: insertfindMindeleteMin Sorted array O(n) O(1) O(1) (from large (to move (find at end) (delete the end) to small) elements) Linked list O(1) O(1) O(n) (append) (a pointer to Min) (reset Min pointer)
Heaps Root 1.All leaf nodes occur at adjacent levels. When a complete binary tree is built, its first node must be the root.
Heaps Left child of the root 2.All levels of the tree, except for the bottom level are completely filled. All nodes on the bottom level occur as far left as possible. The second node is always the left child of the root.
Heaps 3.The key of the root node is greater than or equal to the key of each child. Each subtree is also a heap. Right child of the root The third node is always the right child of the root.
Heaps The next nodes always fill the next level from left-to-right.
Heaps The next nodes always fill the next level from left-to-right.
Heaps Complete binary tree.
Heap Storage Rules • A heap is a binary tree where the entries of the nodes can be compared with a total order semantics. In addition, these two rules are followed: • The entry contained by a node is greater than or equal to the entries of the node’s children. • The tree is a complete binary tree, so that every level except the deepest must contain as many nodes as possible; and at the deepest level, all the nodes are as far left as possible.
Heaps 45 A heap is a certain kind of complete binary tree. 35 23 27 21 22 4 The "heap property" requires that each node's key is >= the keys of its children 19
Heaps 45 This tree is not a heap. It breaks rule number 1. 35 23 27 19
Heaps 45 This tree is not a heap. It breaks rule number 2. 35 23 21
Heaps 45 This tree is not a heap. It breaks rule number 3. 27 23 35 21
Heap implementation of priority queue • Each node of the heap contains one entry along with the entry’s priority, and the tree is maintained so that it follows the heap storage rules using the entry’s priorities to compare nodes. Therefore: • The entry contained by a node has a priority that is greater than or equal to the priority of the entries of the nodes children. • The tree is a complete binary tree.
Adding a Node to a Heap 45 • Put the new node in the next available spot. 35 23 27 21 22 4 19 42
Adding a Node to a Heap 45 • 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 • 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 • 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 • 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 • 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
Adding an Entry to a Heap 45 35 23 27 21 22 4 19 5 As an example, suppose that we already have nine entries that are arranged in a heap with the above priorities.
Suppose that we are adding a new entry with a priority 42.The first step is to add this entry in a way that keeps the binary tree complete. In this case the new entry will be the left child of the entry with priority 21. 45 35 23 21 22 4 27 19 5 42
The structure is no longer a heap, since the node with priority 21 has a child with a higher priority. The new entry (with priority 42) rises upward until it reaches an acceptable location. This is accomplished by swapping the new entry with its parent. 45 35 23 42 22 4 27 19 5 21
The new entry is still bigger than its parent, so a second swap is done. 45 42 23 35 22 4 27 19 5 21
Adding an entry to a priority queue Pseudocode for Addingan entry The priority queue has been implemented as a heap. 1.Place the new entry in the heap in the first available location.This keeps the structure as a complete binary tree, but it might no longer be a heap since the new entry might have a higher priority than its parent. 2. while (the new entry has a priority that is higher than its parent) swap the new entry with its parent.
Reheapification upward Nowthe new entry stops rising, because its parent (with priority 45) has a higher priority than the new entry. In general, the “new entry rising” stops when the new entry reaches the root. The rising process is called reheapification upward.
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 goes in 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 goes in the next two array locations. Any node’s two children reside at indexes (2n) and (2n + 1) in the array. Implementing a Heap 42 35 23 27 21 42 35 23 27 21 An array of data
Data from the next row goes in the next two array locations. Implementing a Heap 42 35 23 27 21 We don't care what's in this part of the array. 42 35 23 27 21 An array of data
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. Important Points about the Implementation 42 35 23 27 21 42 35 23 27 21 [1] [2] [3] [4] [5]
Removing an entry from a Heap • When an entry is removed from a priority, we must always remove the • entry with the highest priority - the entry that stands “on top of heap”. • For example, the entry at the root, with priority 45 will be removed.
The highest priority item, at the root, will be removed from the heap 45 45 42 23 35 22 4 27 19 5 21
Removing an entry from a heap • During the removal, we must ensure that the resulting structure remains a heap. If the root entry is the only entry in the heap, then there is really no more work to do except to decrement the member variable that is keeping track of the size of the heap. But if there are other entries in the tree, then the tree must be rearranged because a heap is not allowed to run around without a root. The rearrangement begins by moving the last entry in the last level upto the root, like this:
Removing an entry from a Heap Before the move • After the move 45 21 42 23 23 42 27 35 22 4 27 35 22 4 19 5 21 19 5 The last entry in the last level has been moved to the root.
Heapsort • Heapsort combines the time efficiency of mergesort and the storage efficiency of quicksort. Like mergesort, heapsort has a worst case running time that is O(nlogn), and like quicksort it does not require an additional array. Heapsort works by first transforming the array to be sorted into a heap.
Heap representation A heap is a complete binary tree, and an efficient method of representing a complete binary tree in an array follows these rules: Data from the root of the complete binary tree goes in the [0] component of the array. The data from depth one nodes is placed in the next two components of the array. We continue in this way, placing the four nodes of depth two in the next four components of the array, and so forth. For example, a heap with ten nodes can be stored in a ten element array as shown below:
45 45 42 27 21 23 22 35 19 4 5 45 27 42 21 23 22 35 19 4 5
Rules for location of data in the array • The data from the root always appears in the [0] component of the array. • Suppose that the data for a root node appears in the component [i] of the array. Then the data for its parent is always at location [(i - 1) / 2] (using integer division).
Suppose that the data for a node appears in component [i] of the array. Then its children (if they exist)always have their data at these locations: Left child at component [2i + 1] Right child at component [2i + 2]
The largest value in a heap is stored in the root and that in our array representation of a complete binary tree, the root node is stored in the first array position. Thus, since the array represents a heap, we know that the largest array element is in the first array position. To get the largest value in the correct array position, we simply interchange the first and the final array elements. After interchanging these two array elements, we know that the largest array element is in the final array position, as shown below: