0% found this document useful (0 votes)
3 views

Lecture 05

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 05

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Heap

The heap is a region of memory that is used to store objects.

It is called a “heap” because it is not organized in a particular


order and can be accessed randomly.

Heaps are the data structure you want to use when you want to
be able to access the maximum or minimum element very
quickly

Heap memory is a type of dynamic memory allocation used for


storing objects and data structures that require a longer lifespan
Heap
The heap is basically all of RAM. A section of RAM is allocated
to runtime functions that need temporary storage while running

The heap property says that is the value of Parent is either


greater than or equal to (in a max heap ) or less than or equal to
(in a min heap) the value of the Child.

A heap is described in memory using linear arrays in a


sequential manner

Heaps are usually implemented with an array


Heap
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.
Heap

70 50 40 45 35 39 16 10 9
Data Insertion in Heap

You can not insert data at root directly


Heap is always a complete binary tree
Follow complete binary tree
We adjust tree from bottom to top
Data will always be inserted from leave node
Insert data as left as possible
Take floor function to get parent index
Insert 60
Array will be now n+1 size
Data Deletion in Heap

You only delete root node


Last element from array will be shifted to root node
After deletion check if its still satisfying heap property
In deletion we adjust tree from top to bottom
Left child will be 2*i and right child will be 2*i+1

Array will be now n-1 size


Heap Sort

After deletion data will be sorted in ascending


order
15 20 7 9 30 Array
30 20 7 9 15 Heap
7 9 15 20 30 Sorted Heap
Time taken will be O(nlog n) in heapsort
Heapify Reading Assignment/HW

15 5 20 1 17 10 30
All leaves are root, no need to heapify
Only apply to no leave nodes the method heapify
Apply from right to left
Start from largest index except from leaves
In complete binary tree non leaf nodes will be
from floor(n/2) +1 ---> n
In example index 4-7 will be leave nodes
Heapify Assignment/HW

Heapify method will start from n/2


Apply from right to left
REFERENCES

1. Introduction to Algorithms, secondedition,T.H.Cormen,C.E.Leiserson,


R.L.Rivest,and C.Stein,PHI Pvt. Ltd./ Pearson Education

2. Introduction to Design and Analysis of Algorithms A strategic approach,


R.C.T.Lee, S.S.Tseng, R.C.Chang and T.Tsai, Mc Graw Hill.

3. Data structures and Algorithm Analysis in C++, Allen Weiss, Second


edition, Pearson education.

You might also like