Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.

Similar presentations


Presentation on theme: "The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right."— Presentation transcript:

1 The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right subtrees. Example 1 17 The relational ordering between data in parent / children nodes can be different than >=, for example can be a “less than” ordering.

2 Operations on heaps All heap operations are based on the following idea: Make a simple structural modification of the heap (different for different operations), which may result in a violation of the heap condition. Traverse the heap from the root to the bottom, or from the bottom to the root (depending on the operation) to recover the heap condition everywhere. The most important operations on heaps are: insertItem (item) Inserts item removeItem () Removes the root and returns the datum stored

3 Linear representation of a heap
The easiest way to represent a complete binary tree is by means of a one- dimensional array of size 2h+1 - 1, where h is the height of the tree. The most attractive property of this representation is that given a position of a node, j, positions of its parent and children can be easily defined (j / 2 is the position of the parent, j * 2 is the position of the left child, and j *2 + 1 is the position of the right child. Our example 1 heap can be represented as follows: k A[k]

4 Linear representation of a heap (contd.)
The insertItem operation in this representation requires the size of the array to be increased by one, and A[size] := newNode 17 k A[k] Now 13 must be walked upheap to fix the heap condition. Its parent is in 15/2, i.e. the 7th position; then, the parent’s parent is in 7/2 position, etc.

5 The upheap method Algorithm upheap (A[n], j, precedes)
Input: array A[n] representing the heap, index of the child’s node j, precedes, a comparator object defining the ordering relationship Output: array A[n] with data originally in j walked up until accordingly ordered with its parent (according to the precedes function) int l := j int key := A[l] int k := l/2 while (k >= 1) and precedes(A[k], key) { A[l] := A[k] l := k k := l/2 } A[l] := key

6 The insertItem method Algorithm insertItem (H, Item, precedes)
Input: heap H[size], item to be inserted precedes, a comparator object defining the ordering relationship Output: boolean variable success set to true if insertItem is successful if H.full() success := false else { success := true size := size + 1 H[size] := Item upheap(H[size], size, precedes) } return success The efficiency if insertItem operation is O(log n), where n is the number of tree nodes.

7 Linear representation of a heap (contd.)
The removeItem operation decreases the size of the array by one and makes A[1] := A[size] 1 k A[k] Now 1 must be walked downheap to fix the heap condition. Its children are in positions 1*2 and 1* Compare to the larger of the two children and if this child is greater, exchange the two. Continue until the heap condition is fixed.

8 The downheap method Algorithm downheap (A[n], j, precedes)
Input: array A[n] representing the heap, index of the parent’s node j, precedes, a comparator object defining the ordering relationship Output: array A[n] with data originally in j walked down until accordingly ordered w.r.t both of its children (according to the precedes function) boolean foundSpot := false int l := j int key := A[l] int k := 2 * l // get the left child first while (k <= n) and (! foundSpot) { if (k < n) and (! precedes (A[k+1], A[k]]) k := k + 1 if (! precedes (A[k], key)) A[l] := A[k], l := k, k := 2 * l else foundSpot := true } // end while A[l] := key

9 The removeItem method Algorithm removeItem (H, precedes)
Input: heap H[size], precedes, a comparator object defining the ordering relationship Output: boolean variable success set to true if removeItem is successful if H.empty() success := false else { success := true item := H[1] H[1] := H[size] size := size - 1 downheap(H[size], 1, precedes)} return success The efficiency of removeItem operation is O(log n), where n is the number of tree nodes.

10 Heap Sort The idea: build a heap containing the elements to be sorted, then remove them in order. Let n be the size of the heap, and m be the number of elements to be sorted. The following two steps implement heap sort: Step1: Construct the heap by doing m insertItem operations. Step2: Do m removeItem operations, putting the elements removed from the heap into the place just vacated by the shrinking heap. Advantages of heap sort: All heap methods, including removeItem and insertItem are O(log n) methods, i.e. each of the two steps takes O(n log n) time. Ignoring the constant of proportionality, runtime efficiency of heap sort is O(n log n) -- much better compared to the quadratic runtime for elementary sorting methods. Heap sort is in-place sorting method, i.e. uses no extra memory

11 Constructing a heap Consider the following list of integers which must be sorted: There are two ways to construct the heap: 1. Top-down, i.e. starting with the empty heap insert one element at a time, fix the heap condition and insert the next element

12 Constructing a heap (contd.)
2. Bottom-up, i.e. arrange the unsorted list in a binary tree Each subtree, starting from the rightmost subtree at the lowest level, is viewed as a heap and the heap condition is fixed. When the left end of the current level is reached, move up to the next level and process it again from right to left until the root is processed. The resulting heap in this example is the following:

13 The sorting step When the heap is constructed, start removing the largest element (or the smallest, depending on the ordering relationship) until only one item is left on the heap, and put the item removed into the place vacated by the shrinking heap. After the first four steps, we have

14 The heapSort method (assuming bottom-up construction)
Algorithm heapSort (A[n], precedes) Input: array A[n] representing the heap, precedes, a comparator object defining the ordering relationship Output: array A[n] represented a sorted (according to the precedes function) list. int y := n / 2 while (y > 0) { downheap (A[n], y, precedes) O(n/2 log n) y := y - 1 } y := n while (y > 1) { temp := A[1], A[1] := A[y], A[y] := temp, y := y - 1 O(n log n) downheap(A[y], 1, precedes) } Overall efficiency: O(1.5 n log n)


Download ppt "The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right."

Similar presentations


Ads by Google