0% found this document useful (0 votes)
39 views27 pages

Lec-20 Priority Quwu & Heaps

DSA - Priority queue and heaps

Uploaded by

wifisystesttest
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views27 pages

Lec-20 Priority Quwu & Heaps

DSA - Priority queue and heaps

Uploaded by

wifisystesttest
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Priority queue

Abstract data type

Special form of queues

Instead of deleting data from FRONT end, we delete data with


highest priority.

Each data is represented along with a priority

Can be implemented using


Unordered / ordered / 2-DimensionalArrays
Linked list
Heaps
Priority queue
1. Operations
1. Is Empty()
2. Insert_with_priority()
3. Delete_highest_priority()
4. Peek_highest_priority()

Ascending order priority queue: In ascending order priority


queue, a lower priority number is given as a higher priority
Priority queue

In descending order priority queue, a higher priority number is


given as a higher priority in a priority.
Priority queue
1. Array Implementation

2. Priority_queue
{
int item;
int priority;
}

Insert data at appropriate position


based on priority

Delete from the FRONT always.


Highest priority element will be deleted always
Insert at the appropriate location with respect to priority

Delete highest priority

Insert=O(n)
Delete=O(1)
Heaps
• Special Tree-based data structure in which:
Tree is a complete binary tree
Elements are defined in a specific order

Binary Heap makes node data to be stored in an array.


Heaps
1. Types of Binary Heap
Max-Heap
The key at root must be maximum among all keys present in
Binary Heap. The same property must be recursively true for
all nodes in Binary Tree.
Heaps
1. Types of Binary Heap
Min-Heap
The key at root must be minimum among all keys present in
Binary Heap. The same property must be recursively true for
all nodes in Binary Tree.
Heaps
1. Operations on Binary Heap
• Find-max (or Find-min) → find a maximum item of a max-
heap, or a minimum item of a min-heap, respectively.
• Insertion → Add a new item in the heap.
• Deletion → Delete an item from the heap.
• Extract Min-Max → Returning and deleting the maximum or
minimum element in max-heap and min-heap respectively.
• Heapify → Process to rearrange the heap in order to maintain
heap-property.
Heaps
Creating a heap
After each insertion following must satisfy
a) Shape property
b) Heap property
Heaps: min heap

Add 1 Heapify Add 6

Add 5

Heapify
Add 2

Add 4

Heapify
Array: 1, 2, 4,5,3,6
Heap insertion
ALGORITHM
• If heap is empty place element at root.
• Add the element to the bottom level of the heap.
• Compare the added element with its parent; if they are in the
correct order, stop.
• If not, swap the element with its parent and return to the
previous step (heapify)
Heap deletion
ALGORITHM
• Replace the element to be deleted by the last element.
• Delete the last element from the Heap.
• Since, the last element is now placed at the position of the
root node. So, it may not follow the heap property.
Therefore, heapify the last node placed at the position of
root.
Heap deletion
Heap deletion
Heapify
• It is a process to rearrange the elements of the heap in order to maintain the
heap property.
• When a certain node causes an imbalance in the heap due to some
operation

• up_heapify() →
– It follows the bottom-up approach. In this, we check if the nodes are following heap
property by going in the direction of rootNode.
– if nodes are not following the heap property we do certain operations to let the tree
follows the heap property.
• down_heapify() →
– It follows the top-down approach. In this, we check if the nodes are following heap
property by going in the direction of the leaf nodes.
– If nodes are not following the heap property we do certain operations to let the tree
follows the heap property.
Heapify

void down_heapify(int heap[], int parent, int size)


{
largest = parent
leftChild = 2*parent + 1
rightChild = 2*parent + 2
If (leftChild < size && heap[leftChild] > heap[largest])
largest = leftChild
If (rightChild < size && heap[rightChild] > heap[largest])
largest = rightChild
if(parent != largest)
{
swap(heap[parent], heap[largest])
down_heapify(heap,largest,size)
}
}
Heapify

void down_heapify(int heap[], int parent, int size)


{
largest = parent
leftChild = 2*parent + 1
rightChild = 2*parent + 2
If (leftChild < size && heap[leftChild] > heap[largest])
largest = leftChild
If (rightChild < size && heap[rightChild] > heap[largest])
largest = rightChild
if(parent != largest)
{
swap(heap[parent], heap[largest])
down_heapify(heap,largest,size)
}
}
Heapsort
• Heap Sort Algorithm :
1. Build a max heap from the input data.
2. Largest item is stored at the root of the heap. Replace it with
the last item of the heap followed by reducing the size of heap
by 1. Finally, heapify the root of the tree.
3. Repeat step 2 while size of heap is greater than 1.
Heap:Practice
• A={1, 12, 9, 5, 6, 10}
Heap: Heapsort
• Heap Sort Algorithm for sorting in decreasing order:
1. Build a max heap from the input data.
2. At this point, the largest item is stored at the root of the heap. Replace it
with the last item of the heap followed by reducing the size of heap by 1.
Finally, heapify the root of the tree.
3. Repeat step 2 while size of heap is greater than 1.
• Comment on Complexity???
Heap:Practice
• Check if given array is min-heap??

• Recursively???
• Iteratively
Heap:Practice
• Check if given array is min-heap??
• Recursively:

• Iteratively:
Iterate over the array
Simply use index relations between parent and its children
Heap:Practice
• Convert max-heap to min-heap??
Heap:Practice
• Extracting kth largest element from an array of ‘n’ elements
– Can be done using both MAXHEAP & MINHEAP

• MAX-HEAP
– Create a max-heap of ‘n’ elements.
– Delete top k-1 elements.
– Now, root of max-heap will represent the kth largest element

• MIN-HEAP
– Create a min-heap of initial ‘k’ elements.
– Now for remaining (n-k) elements, compare them with ROOT of MIN-
HEAP.
– If ROOT is smaller, replace it with current element.
– When array exhausts, MIN-HEAP will contain KTH largest element at
ROOT
– 1, 2 3 4 5 6 7 8 9 .......................
Heap:Practice
• Merge K sorted Lists into a single sorted list.
– Build a min-heap of K elements
– Insert 1st element of each list into it.
– Pop the root (minimum value) and print
– Replace the popped root node with next element in the same list.
– Continue repeating it.

You might also like