0% found this document useful (0 votes)
33 views42 pages

Heap Trees Final

A priority queue is a data structure that processes elements based on their assigned priority, with higher priority elements being processed first. It can be implemented using various structures such as linked lists, multidimensional arrays, or heaps, with heaps being a special type of priority queue that maintains a complete binary tree structure. Key operations on heaps include insertion, deletion, and finding maximum or minimum values, and they are widely used in applications like heap sort and graph algorithms.

Uploaded by

ksrao532
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)
33 views42 pages

Heap Trees Final

A priority queue is a data structure that processes elements based on their assigned priority, with higher priority elements being processed first. It can be implemented using various structures such as linked lists, multidimensional arrays, or heaps, with heaps being a special type of priority queue that maintains a complete binary tree structure. Key operations on heaps include insertion, deletion, and finding maximum or minimum values, and they are widely used in applications like heap sort and graph algorithms.

Uploaded by

ksrao532
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/ 42

Priority Queue (Heap Tree)

A priority queue is a data structure in which each element is assigned a priority. The
priority of the element will be used to determine the order in which the elements will be
processed. The general rules of processing the elements of a priority queue are

• An element with higher priority is processed before an element with a lower priority.
• Two elements with the same priority are processed on a first-come-first-served (FCFS)
basis.
Basic Model of a Priority Queue

H: priority Queue

Deletion Insertion

Deletion is performed according to the priority and the list is maintained internally as per
the property of the queue
Types of Priority Queues
• General Priority queue or simple priority
queue
a) Single Linked List
b) Multidimensional Array

• Special Priority Queues


a) Binary Heap
b) d-heap
c) Leftist Heap
d) Skew Heap
e) Binomial Queues
• General Priority queue or simple priority
queue
a) Single Linked List
In the computer memory, a priority queue can be represented using linked lists. When a
priority queue is implemented using a linked list, then every node of the list will have three
parts:
(a)The information or data part, (b) the priority number of the element, and (c)the address
of the next element.
Consider the priority queue shown in the following Fig.

The highest priority element is in the front of the list so that


every deletion operation takes same amount of time, But
insertion may take different times. In the worst case it is ‘n’.
• General Priority queue or simple priority queue
b) Multidimensional Array:
When arrays are used to implement a priority queue, then a separate queue for each priority number Is
maintained. Each of these queues will be implemented using circular arrays or circular queues. Every
individual queue will have its own FRONT and REAR pointers.
We use a two-dimensional array for this purpose where each queue will be allocated the same
amount of space.
Look at the two-dimensional representation of a priority queue given below.
Given the FRONT and REAR values of each queue, the two-dimensional matrix can be formed
as shown in the following fig.

FRONT[K] and REAR[K] contain the front and rear values of row K, where K is the priority number.
• A priority queue is implemented as a heap.

•A heap is a complete binary tree that facilitates insertion of new data


elements or items and retrieval of minimum or maximum of element.
Binary Heap
(Special Priority Queue)
• Structural Property – A heap follows the structural property
ie a heap is a complete binary tree.
• Heap order Property – the key of each node is less than the
keys of the children of that node.
•A Heap is a special Tree-based data structure in which the tree is a
complete binary tree.

• A heap with N nodes has log N height.

• It is useful to remove the highest or lowest priority element. It is


typically represented as an array.

• There are two types of Heaps.


 Min heap
 Max heap
Min-Heap

• In a Min-Heap the key present at the root node must be less than or equal
among the keys present at all of its children.

• In a Min-Heap the minimum key element present at the root.


Max Heap

• In a Max-Heap the key present at the root node must be greater than or
equal among the keys present at all of its children.

• In a Max-Heap the maximum key element present at the root.


Operations on Max Heap

The following operations are performed on a Max heap data structure...

• 1. Finding Maximum

• 2. Insertion

• 3. Deletion

1. Finding Maximum Value Operation in Max Heap :

• Finding the node which has maximum value in a max heap is very simple. In a max heap,
the root node has the maximum value than all other nodes. So, directly we can display
root node value as the maximum value in max heap.
2. Insertion Operation in Max Heap
• Step 1 - Insert the newNode as last leaf from left to right.

• Step 2 - Compare newNode value with its Parent node.

• Step 3 - If newNode value is greater than its parent, then swap both of them.

• Step 4 - Repeat step 2 and step 3


Example
Consider the below max heap. Insert a new node with value 85.

• Step 1 - Insert the newNode with value 85 as last leaf from left to right.
That means newNode is added as a right child of node with value 75. After
adding max heap is as follows...
• Step 2 - Compare newNode value (85) with its Parent node value (75).
That means 85 > 75
• Step 3 - Here newNode value (85) is greater than its parent value (75),
then swap both of them. After swapping, max heap is as follows...
• Step 4 - Now, again compare newNode value (85) with its parent node value
(89).
• Here, newNode value (85) is smaller than its parent node value (89). So, we
stop insertion process. Finally, max heap after insertion of a new node with
value 85 is as follows...
Algorithm for construction/insertion of Binary Heap Tree
Algorithm Insheap( H, N, item)
{ // A heap H with N elements are stored in the array
N:=N+1, ptr:=N
While( ptr>1) do
{
par:=ptr/2
if (item <=H[par]) then { H[ptr]:=item, return }
H[ptr]:=H[par]
ptr:=par
}
H[1]:=item
}
3. Deletion Operation in Max Heap
• In a max heap, deleting the last node is very simple as it does not disturb
max heap properties.

• Deleting root node from a max heap is little difficult as it disturbs the max
heap properties.

• We use the following steps to delete the root node from a max heap...
• Step 1 - Swap the root node with last node in max heap
• Step 2 - Delete last node.
• Step 3 - Now, compare root value with its left child value.
• Step 4 - If root value is smaller than its left child, then compare left child
with its right sibling. Else goto Step 6
• Step 5 - If left child value is larger than its right sibling, then swap root
with left child, otherwise swap root with its right child.
• Step 6 - If root value is larger than its left child, then compare root value
with its right child value.
• Step 7 - If root value is smaller than its right child, then swap root with
right child, otherwise stop the process.
• Step 8 - Repeat the same until root node fixes at its exact position.
Example:

Consider the above max heap. Delete root node (90) from the max heap.

• Step 1 - Swap the root node (90) with last node 75 in max heap. After
swapping max heap is as follows...
• Step 2 - Delete last node. Here the last node is 90. After deleting node with
value 90 from heap, max heap is as follows...
• Step 3 - Compare root node (75) with its left child (89).
• Here, root value (75) is smaller than its left child value (89). So, compare
left child (89) with its right sibling (70).
• Step 4 - Here, left child value (89) is larger than its right sibling (70), So,
swap root (75) with left child (89).
• Step 5 - Now, again compare 75 with its left child (36).
• Here, node with value 75 is larger than its left child. So, we compare node
75 with its right child 85.
• Step 6 - Here, node with value 75 is smaller than its right child (85). So, we
swap both of them. After swapping max heap is as follows...
• Step 7 - Now, compare node with value 75 with its left child (15).
• Here, node with value 75 is larger than its left child (15) and it does not have
right child. So we stop the process. Finally, max heap after deleting root
node (90) is as follows...
Algorithm for Deletion of an item from a Binary Heap Tree
Algorithm Delheap( H, N, item)
{ item:=H[1], last:=H[N], N:=N-1
ptr:=1, left:=2, right:=3
while(right<=N) do
{ if (last>=H[left] && last>=H[right]) then
{ H[ptr]:=last, return }
if ( H[right]<=H[left] ) then { H[ptr]:=H[left], ptr:=left }
else { H[ptr]:=H[right], ptr:=right}
left:=2*ptr, right:=2*ptr+1
}
if( left=N) then { if (last<=H[left]) then { H[ptr]:=H[left], ptr:=left }
}
H[ptr]:=last
}
Heap Sort

• 1. Insert all the given elements in the form of a heap tree.

• 2. Delete the root node and place it in a array and rearrange the
remaining elements in the form of heap tree.

• 3. Repeat steps 1 and 2 until all the elements are sorted in an


ascending order.
Example:
• Consider the elements : 4,3,7,1,8,5
Construct max heap:

After building max-heap, the elements in the array are:


8,4,7,1,3,5
Step 1: 8 is swapped with 5.
Step 2: 8 is disconnected from heap as 8 is in correct position now and.
Step 3: Max-heap is created and 7 is swapped with 3.
Step 4: 7 is disconnected from heap.
Step 5: Max heap is created and 5 is swapped with 1.
Step 6: 5 is disconnected from heap.
Step 7: Max heap is created and 4 is swapped with 3.
Step 8: 4 is disconnected from heap.
Step 9: Max heap is created and 3 is swapped with 1.
Step 10: 3 is disconnected.
Finally, the sorted array is: 1,3,4,5,7,8
Heap Sort Algorithm
Algorithm Heapsort(A,N)

for j:=1 to N-1 do

call Insheap(A, j, A[j+1])

while(N>1) do

call Delheap(A, N, item)

A[N+1]:=item

}
Complexity Analysis of Heap Sort
Time Complexity: O(n log n)
Space Complexity: O(log n)
Applications of heap tree

• Heap Sort: Heap Sort is one of the best sorting algorithms that use Binary
Heap to sort an array in O(N*log N) time.

• Priority Queue: A priority queue can be implemented by using a heap


because it supports insert(), delete(), extractMax(), decreaseKey()
operations in O(log N) time.

• Graph Algorithms: The heaps are especially used in Graph Algorithms like
Dijkstra’s Shortest Path and Prim’s Minimum Spanning Tree.

You might also like