Priority Queues - 1
Priority Queues - 1
Introduction
● Priority Queues are abstract data structures where each data/value in the
queue has a certain priority.
● A priority queue is a special type of queue in which each element is served
according to its priority.
● If elements with the same priority occur, they are served according to their
order in the queue.
● Generally, the value of the element itself is considered for assigning the
priority.
● For example, the element with the highest value is considered as the highest
priority element. However, in some cases, we may assume the element with
the lowest value to be the highest priority element. In other cases, we can set
priorities according to our needs.
1
Auxiliary Priority Queues Operations
● kth - Smallest/kth – Largest: Returns the kth -Smallest/kth –Largest key in
the priority queue.
● Size: Returns the number of elements in the priority queue.
● Heap Sort: Sorts the elements in the priority queue based on priority (key).
2
● Elements are inserted into the array in sorted order based on the key field.
Deletions are performed at only one end.
● Insertions complexity: O(n); DeleteMin complexity: O(1).
Heaps
3
● A heap is a tree with some special properties.
● The basic requirement of a heap is that the value of a node must be ≥ (or ≤)
than the values of its children. This is called the heap property.
● A heap also has the additional property that all leaf nodes should be at h or
h – 1 level (where h is the height of the tree) for some h > 0 (complete binary
trees).
● That means the heap should form a complete binary tree (as shown below).
In the examples below, the left tree is a heap (each element is greater than its
children) and the right tree is not a heap (since 11 is greater than 2).
Types of Heaps
Based on the property of a heap we can classify heaps into two types:
4
● Min heap: The value of a node must be less than or equal to the values of its
children.
● Max heap: The value of a node must be greater than or equal to the values
of its children.
Binary Heaps
● In a binary heap, each node may have up to two children.
5
● In practice, binary heaps are enough and we concentrate on binary min
heaps and binary max heaps for the remaining discussion.
Representing Heaps: Before looking at heap operations, let us see how heaps can
be represented. One possibility is using arrays. Since heaps are forming complete
binary trees, there will not be any wastage of locations. For the discussion below let
us assume that elements are stored in arrays, which starts at index 0. The previous
max heap can be represented as:
Heap Operations
Some of the important operations performed on a heap are described below along
with their algorithms.
Heapify
Heapify is the process of creating a heap data structure from a binary tree. It is
used to create a Min-Heap or a Max-Heap.
6
● Set current element i as largest.
● The index of the left child is given by 2i + 1 and the right child is given by 2i +
2.
● If leftChild is greater than currentElement (i.e. element at the ith index), set
leftChildIndex as largest. #Condition1
Python Code
7
def heapify(arr, n, i):
largest = i
l = 2 * i + 1 #Index of Left Child
r = 2 * i + 2 #Index of Right Child
if largest != i: #Condition3
arr[i],arr[largest] = arr[largest],arr[i]
heapify(arr, n, largest)
8
Python Code
def insert(array, newNum):
size = len(array)
if size == 0:#If empty heap initially
array.append(newNum) #Simply add the newNum
else:
array.append(newNum);#Step1
for i in range((size//2)-1, -1, -1):
heapify(array, size, i) #Step2
9
● Remove the last element .#Step3
10
Python Code
def deleteNode(array, num):
size = len(array)
i = 0
for i in range(0, size):
if num == array[i]: #Step1
break
11