Heaps and Queues
Heaps and Queues
Heaps and Queues
Group 11
rear ← rear + 1
queue[rear] ← data
return true
end procedure
Dequeue Operation
• Accessing data from the queue is a process of two
tasks − access the data where front is pointing and
remove the data after access.
• Steps:
− Check if the queue is empty.
− If the queue is empty, produce underflow error and
exit.
− If the queue is not empty, access the data
where front is pointing.
− Increment front pointer to point to the next
available data element.
− Return success.
Algorithm
procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
Menu Based Program
Output:
Application of Queue in Data Structure
• Queue is used when things don’t have to be processed
immediately, but have to be processed
in First In First Out order like Breadth First Search.
Root index is 0
i is the parent value
Max heap Construction Algorithm
• Step 1 − Create a new node at the end of heap.
Step 2 − Assign new value to the node.
• Step 3 − Compare the value of this child node
with its parent.
• Step 4 − If value of parent is less than child,
then swap them.
• Step 5 − Repeat step 3 & 4 until Heap property
holds.
• The time complexity is O(n)
Example
Max Heap Deletion Algorithm