heap sort

heap sort - Heap Sort The binary heap data structures is an...

This preview shows pages 1–4. Sign up to view the full content.

Heap Sort The binary heap data structures is an array that can be viewed as a complete binary tree. Each node of the binary tree corresponds to an element of the array. The array is completely filled on all levels except possibly lowest. We represent heaps in level order, going from left to right. The array corresponding to the heap above is [25, 13, 17, 5, 8, 3]. The root of the tree A[1] and given index i of a node, the indices of its parent, left child and right child can be computed PARENT ( i ) return floor( i /2 29 LEFT ( i ) return 2 i RIGHT ( i ) return 2 i + 1 Let's try these out on a heap to make sure we believe they are correct. Take this heap,

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
which is represented by the array [20, 14, 17, 8, 6, 9, 4, 1]. We'll go from the 20 to the 6 first. The index of the 20 is 1. To find the index of the left child, we calculate 1 * 2 = 2. This takes us (correctly) to the 14. Now, we go right, so we calculate 2 * 2 + 1 = 5. This takes us (again, correctly) to the 6. Now let's try going from the 4 to the 20. 4's index is 7. We want to go to the parent, so we calculate 7 / 2 = 3, which takes us to the 17. Now, to get 17's parent, we calculate 3 / 2 = 1, which takes us to the 20. Heap Property In a heap, for every node i other than the root, the value of a node is greater than or equal (at most) to the value of its parent. A[PARENT ( i )] ≥ A[ i ] Thus, the largest element in a heap is stored at the root. Following is an example of Heap:
By the definition of a heap , all the tree levels are completely filled except possibly for the lowest level, which is filled from the left up to a point. Clearly a heap of height h has the minimum number of elements when it has just one node at the lowest level. The levels above the lowest level form a complete binary tree of height h - 1 and 2 h -1 nodes. Hence the minimum number of nodes possible in a heap of height h is 2 h . Clearly a heap of height h , has the maximum number of elements when its lowest level is completely filled. In this case the heap is a complete binary tree of height h and hence has 2 h+1 -1 nodes. Following is not a heap , because it only has the heap property - it is not a complete binary tree. Recall that to be complete, a binary tree has to fill up all of its levels with the possible exception of the last one, which must be filled in from the left side. Height of a node We define the height of a node in a tree to be a number of edges on the longest simple downward path from a node to a leaf. Height of a tree

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 13

heap sort - Heap Sort The binary heap data structures is an...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online