Heaps - eecs.wsu.eduslong/cpts223/heaps.pdf · Binary Heap Implementation Further Types of Heaps...
Transcript of Heaps - eecs.wsu.eduslong/cpts223/heaps.pdf · Binary Heap Implementation Further Types of Heaps...
Heaps
Seth Long
March 5, 2010
HeapsPreliminariesHeaps
Binary HeapsBinary HeapsOperationsBuilding a HeapBinary Heap Implementation
Further Types of Heapsd-heapLeftist HeapsSkew HeapsBinomial Queues
Preliminaries
I “Regular” queues do not provide an order of items (firstcome, first served)
I Some tasks may have higher priority, thus the priority queueI Cluster exampleI Task scheduling on operating systems
I Priority Queues use an order based on priority
I A priority queue is usually implemented with a heapI Simple ways to make such a thing:
I Unordered list: O(1) enqueue, O(N) dequeueI Ordered list: O(N) enqueue, O(1) dequeueI Tree: O(log(n)) enqueue, O(log(n)) dequeue
I Note that we do not need a total ordering, just the next item
Heaps
I Main operations:I enqueue (or insert)I dequeue (delete minimum)
I Other useful operations:I Increase PriorityI Decrease PriorityI Remove (cancel job)
I Do not keep all items in order
I Will be able to get O(log(n)) for nearly all operations, andO(1) average case for insert.
Binary Heaps
I This is a binary tree (NOT binary search tree)
I Two additional properties must be satisfiedI Structure Property:
I Each level is completely filledI Except for the last, which is filled left to right
I This is more strict than AVL or red-black balance!
I Height is blog2 NcI Heap-order Property:
I Every parent is of higher priority than it’s childrenI Basically, ∀x : priority(x) < priority(parent(x))
I Operations must maintain these properties!
Enqueue and Dequeue
I EnqueueI Insert into heap at next available slotI Percolate element up the heap
I Wikipedia lists other terms up-heap, bubble up, sift up
I DequeueI Next element is always at the rootI Size decreases by 1I Move last element to root, percolate down
Enqueue Example (insert 14)
Dequeue Example
Dequeue Example
Other Operations
I Decrease PriorityI Lower priority of an itemI Need to percolate changed item up
I Increase PriorityI Raise priority of an itemI Need to percolate changed item down
I RemoveI Can be implemented in terms of other operationsI Raise priority to bring to rootI dequeueI Or, delete, swap with last value, percolate down if needed
Building a Heap
I Suppose you have a set of N items
I The task is to make it into a heap
I Solution 1: Perform N inserts
I O(N) average case, O(N log N) worst case
I Solution 2: Assume initial set is a heap
I Then, perform percolate down for each internal node
I Proportional to sum of heights of nodes (O(N))
Heap Building Demo
Heap Building Demo
Heap Building Demo
Heap Building Demo
Implementation
I Data storage: An ArrayI Given an item at position i:
I i’s left child is at position 2iI i’s right child is at position 2i + 1I i’s parent is at position b i
2cI Will keep track of number of items in heap
I This is an easy data structure to use for percolate up/down
I May need to resize array eventually.
d-heap
I Each node has d children, instead of 2
I Kind of like a B-tree (may even wish to keep children sorted)
I Enqueue with logd N node accesses (average constantnumber, again)
I Dequeue with d logd N
I A binary heap is then a d-heap with d=2
I Reduced tree height, same benefits as B-tree
Leftist Heaps
I Heap Merging: It is good to be able to merge two heaps!I d-heaps (including binary heaps) merge in O(n) timeI Leftist heaps merge much more easily
I Definition: Null Path Length of node X (npl(X) is the lengthof the shortest path from X to a node without two children.
I Leftist heap property:∀X : npl(X− > left) ≥ npl(X− > right)
I Maintain original heap property as well
I All left paths are longer, basically
I Right subtree is shallower, and thus operations in it are faster.
I Unfortunately breaks nice binary heap array implementation
Leftist Heap and not-Leftist heap
Null Path Length shown as node label:
Merging
I Observation: Given right path length r , heap must have atleast 2r − 1 nodes
I So, given N nodes, maximum right path length isblog2(N + 1)c
I Merging:I Assume root(H1) comes before root(H2)I Recursively merge H2 with right subheap of H1I If result is not leftist, swap left and right subheapsI Running time O(log N)
Example: Merge these two
Ends up like this
Skew Heaps
I Self-adjusting version of leftist-heap
I Skew heaps are to leftist heaps as splay trees are to AVL trees
I Always swap left and right subheaps when merging
I Do not need NPL information
I Worst case O(N)
Binomial Queues
I Also called binomial heaps
I The main idea: Keep a collection of heap-ordered trees topostpone merging
I A binomial queue is a forest of binary heaps, which can eachhave different height
I Check roots of all trees to find minimum element (canmaintain pointer to this)