Heaps and Queues

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 48

Queues and Heaps

Group 11

-Simran Singh (6851)


-Amutha Yadav(6852)
-Omkar Zade(6853)
QUEUE
• Definition:
• A queue is an ordered list in which insertions are
done at one end (rear) and deletions are done at
the other end (front).
• The first element to be inserted is the first one to
be deleted. Hence, it is called First in First out
(PIPO) or Last in Last out (LILO) list.
Queue ADT
• The following operations make a queue an
ADT(Abstract Data Type).
• Insertions and deletions in the queue must
follow the FIFO scheme.
• (For simplicity we assume the elements are integers.)
Main Queue Operations

• EnQueue(int data): Inserts an element at the


end of the queue.
• int DeQueue(): Removes and returns the
element at the front of the queue.
Auxiliary Queue Functions

• peek() − Gets the element at the front of the


queue without removing it.

• isfull() − Checks if the queue is full.

• isempty() − Checks if the queue is empty.


Peek()
• Algorithm:

begin procedure peek


return queue[front]
end procedure
isfull()
• Algorithm:

begin procedure isfull


if rear equals to MAXSIZE
return true
else
return false
endif
end procedure
isempty()
• Algorithm:

begin procedure isempty


if front is less than MIN OR front is greater
than rear
return true
else
return false
endif
end procedure
Enqueue Operations
• Queues maintain two data pointers-front and rear.
• Steps:
− Check if the queue is full.
− If the queue is full, produce overflow error and exit.
− If the queue is not full, increment rear pointer to
point the next empty space.
− Add data element to the queue location, where the
rear is pointing.
− return success.
Algorithm
procedure enqueue(data)
if queue is full
return overflow
endif

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.

• This property of Queue makes it also useful in following


kind of scenarios:
1) When a resource is shared among multiple consumers.
Examples :CPU scheduling, Disk Scheduling.

2) When data is transferred asynchronously (data not


necessarily received at same rate as sent) between two
processes. Examples IO Buffers, pipes, file IO, etc.
Real Life Applications

• In real life, Call Centre phone systems will use


Queues, to hold people calling them in an order,
until a service representative is free.
HEAPS
• Heap is a special case of balanced binary tree
data structure where the root-node key is
compared with its children and arranged
accordingly.

• A heap can be of two types −


-Max Heap
-Min Heap
Min Heaps

• The value of the root node is greater than or


equal to either of its children.
key(α) < key(β)
Max Heaps
•The value of the root node is greater than or
equal to either of its children.

• If α has child node β then −


key(α) ≥ key(β)
Representation
• A Heap is a Complete Binary Tree.
• A binary heap is typically represented as an
array.
• The root element will be at Arr[0].
• Arr[(i-1)/2] - Returns the parent node
• Arr[(2*i)+1] - Returns the left child node
• Arr[(2*i)+2] - Returns the right child node
Example

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

• Step 1 − Remove root node.


• Step 2 − Move the last element of last level to
root.
• 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.
Implementation of heap
Output
Heap Queue
• Heap data structure is mainly used to represent
a priority queue.
• In Python, it is available using “heapq” module.
• The property of this data structure in python is
that each time the smallest of heap element is
popped(min heap).
• Whenever elements are pushed or popped, heap
structure in maintained.
• The heap[0] element also returns the smallest
element each time.
Operations of heapq
• 1. heapify(iterable) :- This function is used
to convert the iterable into a heap data structure. i.e.
in heap order.
• 2. heappush(heap, ele) :- This function is used
to insert the element mentioned in its arguments
into heap. The order is adjusted, so as heap
structure is maintained.
• 3. heappop(heap) :- This function is used to remove
and return the smallest element from heap.
The order is adjusted, so as heap structure is
maintained.
Example
• 4. heappushpop(heap, ele) :- This
function combines the functioning of both push
and pop operations in one statement, increasing
efficiency. Heap order is maintained after this
operation.
• 5. heapreplace(heap, ele) :- This function also
inserts and pops element in one statement, but it
is different from above function. In this, element
is first popped, then element is pushed.i.e, the
value larger than the pushed value can be
returned.
Example
6. nlargest(k, iterable, key = fun) :- This
function is used to return the k largest
elements from the iterable specified and
satisfying the key if mentioned.
7. nsmallest(k, iterable, key = fun) :- This
function is used to return the k smallest
elements from the iterable specified and
satisfying the key if mentioned.
Example
Heap Sort
• Heap sort is a comparison based sorting
technique based on Binary Heap data structure.
• It is similar to selection sort where we first find
the maximum element and place the maximum
element at the end.
• We repeat the same process for remaining
element.
• Time complexity of Heap Sort is O(nLogn).
Heap Sort Algorithm for sorting in
increasing 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 tree.
3. Repeat above steps while size of heap is greater
than 1.
Implementation
Output
Application
• To quickly find the smallest and largest element
from a collection of items or array.
• In the implementation of Priority queue in graph
algorithms like Dijkstra's algorithm , Prim's
algorithm and Huffman encoding.
• In order to overcome the Worst Case Complexity of
Quick Sort algorithm from O(n^2) to O( nlog(n) ) in
Heap Sort.
• Systems concerned with security and embedded
system such as Linux Kernel uses Heap Sort
because of the O( nlog(n) ) .
THANK YOU!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy