<<

Heaps and Priority Queues Binary

Binary heap is a binary that is

● Complete: ○ Every level except possibly the bottom one is completely filled and the leaves in the

bottom level are as far left as possible

● Satisfies the heap property: ○ 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 Index

Since the structure of a heap is so regular (i.e., always a complete ) they can easily be stored in arrays:

Given index i of a node, ● the index of its parent is: ○ ?i / 2 ● the indices of its children are: ○ ?2i and 2i+1

Unused 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 Heaps

Is 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.

Is every heap a sorted array?

No. Array elements might still satisfy the heap property while not being sorted.

Where in a Max-heap is the smallest element?

It is always a leaf but we cannot know exactly where it is located. Inconsistent Heap Order

Can be caused by insertions or deletions.

Need to restore the order with reheapify operations (Swim and Sink). Swim operation

If 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 operation

If 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 queue is an 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?

Various applications with (scheduling of processes on the CPU, patients at the hospital scheduling, etc.)

Heap can be used as a priority queue. Main operations of priority queue

Insert

Remove the maximum (minimum) Insertion to a heap Remove the maximum Example https://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 Code

Keys and their sequential Array representation Index structure numbers of the heap

Exercise

● 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?