330 likes | 605 Views
Heap And Heap Sort. Ethan Coulter 4/22/2004 CS 146 – Dr. Lee. A heap data structure is a data structure that stores a collection of objects (with keys), and has the following properties: – Complete Binary tree – Heap Order
E N D
Heap And Heap Sort Ethan Coulter 4/22/2004 CS 146 – Dr. Lee
A heap data structure is a data structure that stores a collection of objects (with keys), and has the following properties: – Complete Binary tree – Heap Order It is implemented as an array where each node in the tree corresponds to an element of the array. What is a heap?
A Complete Binary Tree A Complete Binary Tree is a binary tree that is completely filled on all levels with a possible exception where the lowest level is filled from left to right.
Heap Order Property For every node v, other than the root, the key stored in v is greater or equal (smaller or equal for max heap) than the key stored in the parent of v. In this case the maximum value is stored in the root
Properties Of The Heap The parent of node v, stored in A[i], is stored in A[i/2] • The left child of node v, stored in A[i], is stored in A[2i] • The right child of node v, stored in A[i], is stored in A[2i+1]
Maintaining The Heap Order In order to maintain the heap order the value of all children of A[i] must be less than the value of A[i]. • A[i].child.value <= A[i].value To keep this property we use a function called Heapify()
Heapify() – Input: an array A and an index i. – Assumption: • the binary trees rooted at left and right children of A[i] are heaps • A[i] may violate the heap order property – Purpose: Push the value at A[i] down the heap until the tree rooted at A[i] is a heap.
The Algorithm Heapify(A, i) l Left_child(i) r Right_child(i) if (l ≤ heap-size[A] and A [l] > A [i] then Largest l else Largest i if (r ≤ heap-size[A] and A[r] > A [largest] then Largestr if (largest = i) then swap(A [i], A [largest]) A [i] A [largest] Heapify(A, largest)
Heap Sort 1. Build_heap(A) 2. For i length[A] down to 2 do 3. Swap(A[1], A[i]) 4. heap-size[A] heap-size[A]-1 5. Heapify(A,1)
Time Cost Analysis Line 1 takes O(n) time There are n-1 calls to Heapify each call requires O(log n) time. Total cost O(n log n).
Example 19 12 16 1 4 7 Array A 4 1 16 19 12 7
Example Take out biggest 19 12 16 Move the last element to the root 1 4 7 Sorted: Array A 4 1 16 12 7 19
Example 7 swap HEAPIFY() 12 16 1 4 Sorted: Array A 7 4 1 16 12 19
Example 16 12 7 1 4 Sorted: Array A 4 1 16 12 7 19
Example Take out biggest 16 Move the last element to the root 12 7 1 4 Sorted: Array A 4 1 12 7 16 19
Example 4 12 7 1 Sorted: Array A 4 1 12 7 16 19
Example 4 swap HEAPIFY() 12 7 1 Sorted: Array A 4 1 12 7 16 19
Example 12 7 4 1 Sorted: Array A 12 7 4 1 16 19
Example Take out biggest 12 Move the last element to the root 7 4 1 Sorted: Array A 7 4 1 16 19 12
Example 1 swap HEAPIFY() 7 4 Sorted: Array A 1 7 4 16 19 12
Example 7 4 1 Sorted: Array A 4 1 7 16 19 12
Example Take out biggest 7 Move the last element to the root 4 1 Sorted: Array A 4 1 16 19 7 12
Example 1 swap HEAPIFY() 4 Sorted: Array A 4 1 16 19 7 12
Example Take out biggest 4 Move the last element to the root 1 Sorted: Array A 1 4 16 19 7 12
Example Take out biggest 1 Sorted: Array A 4 1 16 19 7 12
Example Sorted: 16 7 12 4 1 19