heaps and priority queuespeople.cs.pitt.edu/~aus/cs1501/priorityqueue.pdf · binary heap binary...
TRANSCRIPT
Heaps and Priority Queues
Binary HeapBinary heap is a binary tree that is
● Complete:
● Satisfies the heap property:
○ Every level except possibly the bottom one is completely filled and the leaves in the
bottom level are as far left as possible
○ The key stored in every node is greater than or equal to the keys stored in
its children (max-heap).
Are these binary heaps?
No. Heap property not satisfied. No. It is not complete.
Observe the IndexSince the structure of a heap is so regular (i.e., always a complete binary tree) they can easily be stored in arrays:
Given index i of a node,● the index of its parent is:
○ ?● the indices of its children are:
○ ?
Unused
i / 2
2i and 2i+1
Observe the Index
Benefits of using an array vs references?
● Each node would have 3 references, so:○ [Memory Overhead] Each reference occupies 64 bits of memory (on 64 bit system).○ [Time overhead] All references would need to be updated on every swap.
● Cache locality using an array is better since all values are stored close to each other, and not scattered across memory like in reference case.
Binary HeapsIs every array a heap?
No. Array elements might be completely out of order and not satisfy heap properties.
Is every sorted array a heap?
Yes. Max-heap if descending, Min-heap if ascending.
Where in a Max-heap is the smallest element?
It is always a leaf but we cannot know exactly where it is located.
Is every heap a sorted array?
No. Array elements might still satisfy the heap property while not being sorted.
Inconsistent Heap Order
Can be caused by insertions or deletions.
Need to restore the order with reheapify operations (Swim and Sink).
Swim operationIf in a max-heap one of the nodes is greater than its parent, it switches places with the parent, and repeats this until it is not greater than its parent.
Sink operationIf in a max-heap one of the nodes is smaller than one or both of its children, it switches places with the larger one of the two children, and repeats this until all its children are smaller than itself.
Priority QueuePriority queue is an abstract data type where each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority.
Example scenarios?
Heap can be used as a priority queue.
Various applications with scheduling (scheduling of processes on the CPU, patients at the hospital scheduling, etc.)
Main operations of priority queue
Insert
Remove the maximum (minimum)
Insertion to a heap
Remove the maximum
Examplehttps://www.cs.usfca.edu/~galles/visualization/Heap.html
Insert: 50, 60, 70, 30, 90, 20, 25, 55, 10, 35, 15
Remove Smallest 11 times
Complexities
What if we want to update/delete nodes in the middle?
We need additional index structures.
Index Priority Queue
Textbook code example:
http://algs4.cs.princeton.edu/24pq/IndexMinPQ.java.html
Index Priority Queue Produced by the Textbook Code
Keys and their sequential numbers Array representation of the heap Index structure
Index Priority Queue Produced by the Textbook CodeKeys and their sequential
numbersArray representation
of the heap Index structure
● Draw the graphical diagram of this heap● Insert new value: #10 “Mouse”● Replace word #5 “Times” with “Cat”● Replace word #0 “It” with “Zoo”● Remove word #4 “Of”● What is stored in pq and qp after these
operations?
Exercise