Eecs 281 Heaps
Eecs 281 Heaps
Eecs 281 Heaps
design: build and use explicit data structures that are concrete realizations of trees analysis: describe the dynamic properties of algorithms
Tree: nonempty collection of vertices/ nodes and edges in which there exists precisely one path connecting any two nodes
Some Examples
M-ary tree: definition above where each internal node has M children.
Concrete Implementation
Node in Binary Tree
struct node {Item item; node *left, *right};
A node contains some information, and points to its left child node and right child node Efficient for moving down a tree from parent to child Modification to move up tree from child to parent?
Efficient insertion of new items Efficient removal of item with largest key
Defn: a priority queue is a data structure of items with keys that supports two basic operations: insert a new item, and remove the item with the largest key
Insert
increment size of array put item at the end of the array constant time: O(1)
Remove maximum
find the max in the array by inspecting each element exchange the maximum with the last item decrement the size of the array linear time: O(N)
We can do better
Heap
Imprecise Defn: Storage of data in an array, such that each key is guaranteed to be the keys in two other specific positions and the key in one other specific position
(tree-like properties)
Defn: A heap is a set of nodes with keys arranged in a complete heap-ordered binary tree, represented as an array
Example
answer to 1) answer to 2)
Bottom Up Heapify
void fixUp(Item heap[], int k) { while (k > 1 && heap[k/2] < heap[k]){ exch(heap[k], heap[k/2]); k = k/2;} }
Pass index (k) of array element w/ increased priority Exchange the key in the given node with the key of the parent until:
we reach the root, or we reach a parent with a larger (or equal) key
Pass index (k) of array element w/ decreased priority Exchange the key in the given node with the largest key among the nodes children, moving down to that child, until:
we reach bottom of heap there are no children with a larger key
Unlike root, last node is not known in advance, must pass it (heapsize)
Heap Implementation
void insert (Item item) { pq[++N] = item; fixUp(pq,N); } Item getmax() { swap(pq[1], pq[N]); fixDown(pq, N-1, 1) return pq[N--]; }
Insert
put item at the end of the priority queue use fixUp to find proper position
Remove maximum
remove root take item from end of array and place at root use fixDown to find proper position
Phase 1
Transform unsorted array into heap
called Heapifying
Phase 2
Remove the largest item from heap and add it to sorted sequence Heapify Repeat
Note order of node visitation in tree What would happen if algorithm started nearer to root?
Remember first array index in heap is 1, not 0 Make call to buildHeap Loop from last item in the heap
swap current root and current last position fix the heap
Heapsort
Remove elements one at a time, filling original array from back to front.
each takes at most O(log N) time, N of them
Summary: Heaps
Unsorted array
O(1) insertion of an item O(N) removal of largest item
Heap
efficient O(log N) insertion of an item efficient O(log N) removal of largest item
Heapsort
O(N log N) sort that takes advantage of heap properties