





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of heaps, a data structure used in algorithms like heapsort and for implementing priority queues. Topics covered include the heap property, heapify() and buildheap() functions, and their applications in heapsort and priority queues. Real-world examples are also provided to illustrate the concepts.
Typology: Slides
1 / 29
This page cannot be seen from the preview
Don't miss anything!






















Heapsort Priority Queues Quicksort
A heap is a “complete” binary tree, usually represented as an array: 16 4 10 14 7 9 3 2 8 1
A = 16 14 10 8 7 9 3 2 4 1
Heaps also satisfy the heap property :
A[ Parent ( i )] ≥ A[ i ] for all nodes i > 1 In other words, the value of a node is at most the value of its parent The largest value is thus stored at the root (A[1])
Because the heap is a binary tree, the height of any node is at most Θ(lg n )
Heapify() : maintain the heap property
Given: a node i in the heap with children l and r Given: two subtrees rooted at l and r , assumed to be heaps Action: let the value of the parent node “float down” so subtree at i satisfies the heap property If A[i] < A[l] or A[i] < A[r], swap A[i] with the largest of A[l] and A[r] Recurse on that subtree Running time: O( h ), h = height of heap = O(lg n )
// given an unsorted array A, make A a heap BuildHeap(A) { heap_size(A) = length(A); for (i = length[A]/2 downto 1) Heapify(A, i); }
Each call to Heapify() takes O(lg n ) time
There are O( n ) such calls (specifically, n/2)
Thus the running time is O( n lg n )
Is this a correct asymptotic upper bound? Is this an asymptotically tight bound?
A tighter bound is O (n )
How can this be? Is there a flaw in the above reasoning?
Given BuildHeap() , an in-place sorting
algorithm is easily constructed: Maximum element is at A[1] Discard by swapping with element at A[n] Decrement heap_size[A] A[n] now contains correct value Restore heap property at A[1] by calling Heapify() Repeat, always swapping A[1] for A[heap_size(A)]
Heapsort(A) { BuildHeap(A); for (i = length(A) downto 2) { Swap(A[1], A[i]); heap_size(A) -= 1; Heapify(A, 1); } }
Heapsort is a nice algorithm, but in practice Quicksort (coming up) usually wins
But the heap data structure is incredibly useful for implementing priority queues A data structure for maintaining a set S of elements, each with an associated value or key Supports the operations Insert() , Maximum() , and ExtractMax() What might a priority queue be useful for?
Insert(S, x) inserts the element x into set S
Maximum(S) returns the element of S with the maximum key
ExtractMax(S) removes and returns the element of S with the maximum key
How could we implement these operations using a heap?
And now, a real-world example… combat billiards Sort of like pool... Except you’re trying to kill the other players… And the table is the size of a polo field… And the balls are the size of Suburbans... And instead of a cue you drive a vehicle with a ram on it Problem: how do you simulate the physics?
Figure 1: boring traditional pool
Simplifying assumptions:
G-rated version: No players Just n balls bouncing around No spin, no friction Easy to calculate the positions of the balls at time Tn from time Tn-1 if there are no collisions in between Simple elastic collisions
HeapInsert(A, key) // what’s running time? { heap_size[A] ++; i = heap_size[A]; while (i > 1 AND A[Parent(i)] < key) { A[i] = A[Parent(i)]; i = Parent(i); } A[i] = key; }
HeapMaximum(A) { // This one is really tricky:
return A[i]; }