0% found this document useful (0 votes)
20 views13 pages

Data Structure 23 + 24

The document discusses heaps, a complete binary tree data structure used primarily for implementing priority queues and sorting algorithms. It covers various types of heaps, their applications in fields like graph algorithms and medical applications, as well as the advantages and disadvantages of using heaps and binary heaps. Additionally, it details the operations for inserting and deleting elements in min-heaps and max-heaps.

Uploaded by

Faisal Sheikh
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)
20 views13 pages

Data Structure 23 + 24

The document discusses heaps, a complete binary tree data structure used primarily for implementing priority queues and sorting algorithms. It covers various types of heaps, their applications in fields like graph algorithms and medical applications, as well as the advantages and disadvantages of using heaps and binary heaps. Additionally, it details the operations for inserting and deleting elements in min-heaps and max-heaps.

Uploaded by

Faisal Sheikh
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/ 13

DATA STRUCTURE

LECTURE 23 + 24
INSTRUCTOR: Tayyba Khalid
Content:

■ Heaps
– Types
– Applications
– Advantages / Disadvantages
■ Binary heap
Heaps
■ A Heap is a complete binary tree data structure that satisfies the
heap property: for every node, the value of its children is greater than
or equal to its own value.
■ Heaps are usually used to implement priority queues, where the
smallest (or largest) element is always at the root of the tree.
Types:

Binomial
Heap

Binary
Fibonac
Heap ci Heap

K-ary
Leftist
Heap Heap
Applications:
1. Priority Queues: Heaps are commonly used to implement priority queues, where
elements with higher priority are extracted first. This is useful in many applications
such as scheduling tasks, handling interruptions, and processing events.

2. Sorting Algorithms: Heapsort, a comparison-based sorting algorithm, is


implemented using the Heap data structure. It has a time complexity of O(n log n),
making it efficient for large datasets.

3. Graph algorithms: Heaps are used in graph algorithms such as Prim’s Algorithm
, Dijkstra’s algorithm., and the A* search algorithm.

4. Lossless Compression: Heaps are used in data compression algorithms such as


Huffman coding, which uses a priority queue implemented as a min-heap to build
a Huffman tree.

5. Medical Applications: In medical applications, heaps are used to store and


manage patient information based on priority, such as vital signs, treatments, and
test results.
6. Load balancing: Heaps are used in load balancing algorithms to
distribute tasks or requests to servers, by processing elements with the
lowest load first.

7. Order statistics: The Heap data structure can be used to efficiently find
the kth smallest (or largest) element in an array

8. Resource allocation: Heaps can be used to efficiently allocate resources


in a system, such as memory blocks or CPU time, by assigning a priority to
each resource and processing requests in order of priority.

9. Job scheduling: The heap data structure is used in job scheduling


algorithms, where tasks are scheduled based on their priority or deadline.
The heap data structure allows efficient access to the highest-priority task,
making it a useful data structure for job scheduling applications.
Advantages / Disadvantages of
Heap Data Structure:
Advantages Disadvantages
■ Time Efficient: Heaps have an ■ Complexity: it has a worst-
average time complexity of case time complexity of O(n
O(log n) for inserting and log n), which may not be
deleting elements, making optimal for some applications
them efficient for large that require faster algorithms.
datasets.
■ Lack of flexibility: The heap
■ Space Efficient : A Heap tree data structure is not very
is a complete binary tree, flexible, as it is designed to
therefore can be stored in an maintain a specific order of
array without wastage of elements.
space.
■ Priority-based: Heaps allow
elements to be processed
based on priority
Binary Heap:

■ A Binary Heap is a complete Binary Tree which is used to store


data efficiently to get the max or min element based on its type.
■ A Binary Heap is either Min Heap or Max Heap.
■ In a Min Binary Heap, the key at the root must be minimum among all
keys present in Binary Heap.
■ The same property must be recursively true for all nodes in Binary
Tree.
Min-heap:

■ Min-heap:
• Heap Property: Each node is smaller than its children
• Root is the minimum element in the tree
• Smallest key always at the front of a queue
• Able to extract min with O(1) time complexity
Max-heap:

• Heap Property: Each node is larger than its children


• Root is the maximum element in the tree
• Largest key always at the front of a queue
• Able to extract max with O(1) time complexity
Min-heap Operations

Insert: Delete Min:


1. Swap the root with the bottom
1. Insert the element at the node
bottom rightmost spot 2. Delete the bottom node
2. Swap the new element with 3. Swap the root with
its parent until it is larger its smallest child
than its parent 4. Repeat until both children
3. This swapping operation is are larger than the value being
swapped, or we create a new child
known as ‘heapify up’ or
node
‘percolate up’
5. This swapping operation is known
as ‘heapify down
Max-heap Operations

Insert: Delete Max:


1. Swap the root with the bottom
right node
1. Insert the element at the 2. Delete the bottom right node
bottom rightmost spot
3. Swap the root with its largest child
2. Swap the new element with 4. Repeat until both children
its parent until it is smaller are smaller than the value being
than its parent, or becomes swapped, or we create a new child
the new root node
5. This swapping operation is known
as ‘heapify down’
Advantages / Disadvantages of
Binary Heap

Advantages disadvantages
• Flexible size ■ Slow lookup (slower than
searching a BST)
• Fast insert

You might also like