1

Programming II (CS300) Chapter 12: Heaps and Priority Queues

MOUNA KACEM [email protected] Spring 2019 Binary Trees 2

u Terminology

u A binary is a tree in which no node can have more than two children called left and right

u A full is a binary tree in which each node has exactly zero or two children

u A complete binary tree is a binary tree, which is completely filled, with the possible exception of the bottom level, which is filled from left to right Examples of Binary Trees 3

Full binary tree Complete binary tree

The height h of a complete binary tree with N nodes is at most O(log N). Heaps 4

u Binary is a binary tree that should satisfy these two constraints

u Structural constraint

u A Heap is a complete binary tree All levels of a complete binary tree are completely filled except possibly the last level and the last level has all keys as left as possible Heaps 5

u Structural Constraint (complete binary tree)

Non-Heap Heap Non-Heap

Heap Heap Non-Heap Heaps 6

u Binary Heap is a binary tree that should satisfy these two constraints

u Structural constraint

u A Heap is a complete binary tree

u Value-relationship constraint

u A Heap must satisfy the heap ordering property. There are two ordering properties:

u min-heap property: the value of each node is greater than or equal to the value of its parent, with the minimum-value element at the root.

u max-heap property: the value of each node is less than or equal to the value of its parent, with the maximum-value element at the root. Heaps 7

u Value-Relationship Constraint

Max-Heap Min-Heap

Max-Heap

Max-Heap Min-Heap Non-Heaps 8 Binary Algorithms 9 u Binary tree traversals:

u breadth-first traversal or level-order traversal

u Visit nodes by levels from top to bottom and from left to right

u depth-first traversal

u PreOrder traversal visit the parent first and then left and right children

u InOrder traversal visit the left child, then the parent and the right child

u PostOrder traversal visit left child, then the right child and then the parent Binary-Tree – Level-order Traversal 10

Level 0

Level 1

Level 2

Level 3

Level-order traversal: F, B, G, A, D, I, C, E, H Array-Representation of a Heap 11 Array-Representation of a Heap 12

root at index 0

i (j-1)/ 2 (j-1)/ 2

p p p

2*i + 1 2*i + 2 j j l r l r

p (parent); l (left child); r (right child) Heaps and Priority Queues 13

u Heaps

u Priority Queues 14 u QueueADT

u Objects are added and removed according to the First-In, First-Out (FIFO) principle u PriorityQueueADT

u A “collection” of prioritized elements that allows

u Arbitrary element insertion of a new element, and

u Removal of the element having highest priority

u The priority of an element added to a priority-queue is designated by a key

u The element with minimal value of key will have the highest priority Priority-Queue Examples 15 Priority Queue 16

public interface PriorityQueueADT>{ public void insert(T newObject); public T removeBest(); // removes and returns the element with the highest priority

public T peekBest(); // returns without removing the element with the highest priority public boolean isEmpty(); } Java.lang.Comparable Interface 17

u Interface providing a mean for defining comparison between two objects

u Comparable interface Includes a single method: compareTo

u a.compareTo(b) must return an integer i such that

u i < 0 designated that a < b u i = 0 designated that a = b u i > 0 designated that a > b

for more details about the interface Comparable, please visit https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html Implementation Strategies for a 18 Priority-Queue

u Implementing a priorityQueue with an Unsorted LinkedList tail head

(18, 3) (15, 1) (45, 0) (10, 2)

Method Running Time insert O(1) removeBest O(n) peekBest O(n) isEmpty O(1) Implementation Strategies for a 19 Priority-Queue

u Implementing a priorityQueue with a Sorted LinkedList tail head

(45, 0) (15, 1) (10, 2) (18, 3)

Method Running Time insert O(n) removeBest O(1) peekBest O(1) isEmpty O(1) Implementation Strategies for a 20 Priority-Queue

Using Unsorted LinkedList Using Sorted LinkedList Method Running Time Method Running Time insert O(1) insert O(n) removeBest O(n) removeBest O(1) peekBest O(n) peekBest O(1) isEmpty O(1) isEmpty O(1) Implementing a Priority-Queue 21 using a min-Heap

public interface PriorityQueueADT>{ public void insert(T newObject); // insert an element to the priority Queue

public T peekBest(); // returns without removing the element with the highest priority

public boolean isEmpty(); // checks whether the priority Queue is empty or not public T removeBest(); // removes and returns the element with the highest priority } Implementing a Priority-Queue 22 using a min-Heap

u Adding an element to the Heap (insert operation)

Michael T. Goofrich, et al., "Data Structures & Algorithms", Wiley, six edition, 2014, pp. 360 - 384. Implementing a Priority-Queue 23 using a min-Heap

u Adding an element to the Heap (insert operation)

1. First Step: Maintain the complete binary tree property

u If the heap is empty or the bottom level is already full

u Place the new node at the leftmost position of a new level Implementing a Priority-Queue 24 using a min-Heap

u Adding an element to the Heap (insert operation)

1. First Step: Maintain the complete binary tree property

u If the heap is not empty and the bottom level is not already full

u Place the new node just beyond the rightmost node at the bottom level of the heap Implementing a Priority-Queue 25 using a min-Heap

u Adding an element to the Heap (insert operation)

1. First Step: Maintain the complete binary tree property

2. Second Step: Restore the heap-order property (Up-Heap Bubbling)

u After the first step, the tree representing the heap is complete. But, it may violate the heap-order property Implementing a Priority-Queue 26 using a min-Heap

u Adding an element to the Heap (insert operation)

1. Second Step: Restore the heap-order property (Up-Heap Bubbling) After the first step, the tree representing the heap is complete. But, it may violate the heap-order property

u if the priority queue was empty before the insertion à Nothing to do Implementing a Priority-Queue 27 using a min-Heap

u Adding an element to the Heap (insert operation) 1. Second Step: Restore the heap-order property (Up-Heap Bubbling) u if the priority queue was not empty before the insertion Suppose that u The new node is inserted at the position p of the priority queue. (p cannot be the position of the root as the priority queue was not empty before the insertion operation) u The new inserted node’s parent is located at the position q of the priority queue u We compare the key (aka priority) at position p to that of p’s parent at position q Implementing a Priority-Queue 28 using a min-Heap

u Adding an element to the Heap (insert operation)

1. Second Step: Restore the heap-order property (Up-Heap Bubbling)

u if the priority queue was not empty before the insertion

u We compare the key (aka priority) at position p to that of p’s parent at position q

u if key kp ≥ kq, the heap-order property is satisfied and the algorithm terminates

u if key kp < kq, then restore the heap-order property swap the entries stored at positions p and q Again, the heap-order property may be violated, so repeat the process: Going up in the binary tree until no violation of the heap-order property occurs Implementing a Priority-Queue using a 29 min-Heap

u Adding an element to the Heap (insert operation)

(b) (a) Implementing a Priority-Queue using a 30 min-Heap

u Adding an element to the Heap (insert operation)

(c) (d) Implementing a Priority-Queue using a 31 min-Heap

u Adding an element to the Heap (insert operation)

(e) (f) Implementing a Priority-Queue using a 32 min-Heap

u Adding an element to the Heap (insert operation)

(g) Implementing a Priority-Queue using a 33 min-Heap

u Adding an element to the Heap (insert operation)

u The upward movement of the newly inserted entry by means of swaps is conventionally called up-heap bubbling or percolate up u A swap either resolves the violation of the heap-order property or propagates it one level up in the heap. u In the worst case, upheap bubbling causes the new entry to move all the way up to the root of heap. u In the worst case, the number of swaps performed in the execution of method insert is equal to the height of the heap (O(log(n))) u The insert operation using a heap is of : O(log(n)) where n represents the number of elements in the heap (i.e. size of the heap) Implementation Strategies for a 34 Priority-Queue

Using Unsorted LinkedList Using Sorted LinkedList Using a Heap Method Running Time Method Running Time Method Running Time insert O(1) insert O(n) insert O(log(n)) removeBest O(n) removeBest O(1) removeBest peekBest O(n) peekBest O(1) peekBest isEmpty O(1) isEmpty O(1) isEmpty

Recall that the height h of a complete binary tree with n nodes is at most O(log(n)). Implementing a Priority-Queue using a 35 min-Heap

u Removing an element from the Heap (removeBest operation)

u Using a min-heap, the entry with the highest priority (with the smallest key) is stored at the root r of the binary tree representing the heap

u Deleting the root node would leave two disconnected subtrees Implementing a Priority-Queue using a 36 min-Heap

u Removing an element to the Heap (removeBest operation)

u Using a min-heap, the entry with the highest priority (with the smallest key) is stored at the root r of the binary tree representing the heap

u Deleting the root node would leave two disconnected subtrees Implementing a Priority-Queue using a 37 min-Heap

u Removing an element to the Heap (removeBest operation)

u Using a min-heap, the entry with the highest priority (with the smallest key) is stored at the root r of the binary tree representing the heap

u Deleting the root node would leave two disconnected subtrees

u Solution: Delete the leaf at the last position p of the tree and copy its value to the root r

u Then, perform a Heap-Down Bubbling or percolate down process after the removal Implementing a Priority-Queue using a 38 min-Heap

u Removing an element to the Heap (removeBest operation) Implementing a Priority-Queue using a 39 min-Heap

u Removing an element to the Heap (removeBest operation) Implementing a Priority-Queue using a 40 min-Heap

u Removing an element to the Heap (removeBest operation) Implementing a Priority-Queue using a 41 min-Heap

u Removing an element to the Heap (removeBest operation) Implementation Strategies for a 42 Priority-Queue

Using Unsorted LinkedList Using Sorted LinkedList Using a Heap Method Running Time Method Running Time Method Running Time insert O(1) insert O(n) insert O(log(n)) removeBest O(n) removeBest O(1) removeBest O(log(n)) peekBest O(n) peekBest O(1) peekBest isEmpty O(1) isEmpty O(1) isEmpty Implementation Strategies for a 43 Priority-Queue

Using Unsorted LinkedList Using Sorted LinkedList Using a Heap Method Running Time Method Running Time Method Running Time insert O(1) insert O(n) insert O(log(n)) removeBest O(n) removeBest O(1) removeBest O(log(n)) peekBest O(n) peekBest O(1) peekBest O(1) isEmpty O(1) isEmpty O(1) isEmpty O(1) Array-Based implementation of a 44 Heap Array-Based implementation of a 45 Heap

u The array-based representation of a binary tree is suitable for a complete binary tree. u The elements of the tree are stored in an array T such that the element at position p is stored in T with index equal to the level number f (p) of p, defined as follows: u If p is the root, then f (p) = 0. u If p is the left child of position q, then f (p) = 2 f (q)+1. u If p is the right child of position q, then f (p) = 2 f (q)+2. u For a tree with of size n, the elements have contiguous indices in the range [0,n−1] u The last position of is always at index n−1.