SlideShare a Scribd company logo
Placement Preparation 
Heaps 
Varun Raj 
B.Tech, CSE
Heaps 
● Basically Heaps is a binary 
tree satisfying the property 
that the value of the parent 
is less than the value of its 
children. 
● This heap is known as a min 
heap. There is also a max-heap 
in which the parent is 
greater than its children
Property of Heap 
● The minimum element is found at the 
root.Proof is trivial 
● The maximum element is found at the 
leaf. 
● Hence time to find the minimum element 
in the array is O(1)
Implementing a Heap 
There are two most common ways to implement 
a heap- 
• Using an Array .We will use this since it is 
more intuitive and simpler 
• Using Linked List by struct. This is also 
another method but I personally feel this is 
an unnecessary complication
Implementation 
Some Basic Implementation Concepts 
• Generally in an array we start indexing from 0 
in C++ but for heaps it is more convenient to 
start numbering of the root from 1. 
• For a particular node i its left child will be 2*i 
and its right child will be 2*i+1 and its parent 
will be i/2 irrespective of i being odd or even
Inserting 
• We continue swapping with 
parent till heap condition is 
satisfied. 
• Time taken to insert an 
element into the heap is at 
worst case O(logn) -the 
height of the heap
Creating a heap 
Method 1 using insert 
• Starting from the insert method described 
earlier to insert elements 
• Time complexity from this method however 
comes out to be log1+log2+…logn 
=log(n!)=Theta (nlogn). Therefore we need 
something faster
Heapify Procedure 
• Opposite of insert procedure. 
• You come down the heap instead 
of moving up 
• Time complexity is again O(logn). 
• Therefore first create a normal 
array and apply heapify to all the 
elements of the array. Time 
complexity is O(n).
Deleting an element from the heap 
● In a heap ,normally you can 
only delete from the root. 
● So you delete from the root and 
swap the last element of the 
heap with it and then you do a 
heapify on the root. 
● Time complexity is again 
O(logn)
Heapsort 
• Since the element at the root is always the smallest we 
print the element at the top and delete the root. 
• The next element at the root will be the second 
smallest element continuing like this we can sort the 
entire array 
• Since the time taken for deletion is O(logn). Therefore 
time taken for n deletions is O(nlogn) 
• Therefore time taken for heapsort is 
O(n)+O(nlogn)=O(nlogn)
Advantages of Heaps 
• Can sort in O(nlogn) time that is as fast as merge sort 
but without using extra space 
• Can insert elements in O(logn) time compare that with 
sorted array. 
• Can remove the smallest element in O(logn) time faster 
than sorted array.
Applications of Heaps 
• Very useful if you want a data structure that is used for 
lots of insertions by value 
• Is used in the implementation of priority queue 
• Is also used for sorting
Lecture 11.1 :  heaps

More Related Content

PDF
Ch 5: Introduction to heap overflows
PDF
FTM tree
PDF
Analyzing Larger RasterData in a Jupyter Notebook with GeoPySpark on AWS - FO...
PDF
CNIT 127 Ch 5: Introduction to heap overflows
PPTX
1 storm-intro
PPTX
2021 Dask Summit - Using STAC to catalog SpatioTemporal datasets
PDF
Scaling metrics
Ch 5: Introduction to heap overflows
FTM tree
Analyzing Larger RasterData in a Jupyter Notebook with GeoPySpark on AWS - FO...
CNIT 127 Ch 5: Introduction to heap overflows
1 storm-intro
2021 Dask Summit - Using STAC to catalog SpatioTemporal datasets
Scaling metrics

What's hot (20)

PPTX
Deep Learning on Aerial Imagery: What does it look like on a map?
PPT
Heapsort
PPT
Heapsort
ODP
Deep Learning meetup
PDF
Raster Processing with Scipy.ndimage (Dev Meet Up II)
ODP
Hubba Deep Learning
PDF
Climate data in r with the raster package
PDF
HeapSort
PDF
Processing Big Data in Real-Time - Yanai Franchi, Tikal
DOCX
whileloop
PDF
Introduction to Date and Time API 4
PDF
Introduction to Date and Time API 3
PPTX
LocationTech Projects
PDF
Getting date and time from ntp server with esp8266 node mcu
PDF
Working with OpenStreetMap using Apache Spark and Geotrellis
PPTX
Terark Product and Technology
PDF
Brand new Date and Time API
PDF
Apache Storm
PDF
Processing Big Data in Realtime
PDF
Prezo at-mesos con2015-final
Deep Learning on Aerial Imagery: What does it look like on a map?
Heapsort
Heapsort
Deep Learning meetup
Raster Processing with Scipy.ndimage (Dev Meet Up II)
Hubba Deep Learning
Climate data in r with the raster package
HeapSort
Processing Big Data in Real-Time - Yanai Franchi, Tikal
whileloop
Introduction to Date and Time API 4
Introduction to Date and Time API 3
LocationTech Projects
Getting date and time from ntp server with esp8266 node mcu
Working with OpenStreetMap using Apache Spark and Geotrellis
Terark Product and Technology
Brand new Date and Time API
Apache Storm
Processing Big Data in Realtime
Prezo at-mesos con2015-final
Ad

Similar to Lecture 11.1 : heaps (20)

PPT
lecture 5
PPTX
Lecture 3 - Data Structure File Organization
PPT
thisisheapsortpptfilewhichyoucanuseanywhereanytim
PPTX
Heap_data_structures_in_data_steruc.pptx
PPT
Heapsort
PPT
Heapsortokkay
PPTX
05 heap 20161110_jintaeks
PPT
PPT
Ch15 Heap
PPTX
Heaps and tries power point this is an educational material
PDF
Heap Hand note
PPT
Analysis of Algorithms-Heapsort
PPT
3.7 heap sort
PDF
Heap and heapsort
PPTX
Lecture 07 - HeapSort.pptx
PPT
Heapsort 1
PPT
Heap Sort (project).ppt
PPT
CSE680-06HeapSort.ppt
PPT
Cis435 week05
lecture 5
Lecture 3 - Data Structure File Organization
thisisheapsortpptfilewhichyoucanuseanywhereanytim
Heap_data_structures_in_data_steruc.pptx
Heapsort
Heapsortokkay
05 heap 20161110_jintaeks
Ch15 Heap
Heaps and tries power point this is an educational material
Heap Hand note
Analysis of Algorithms-Heapsort
3.7 heap sort
Heap and heapsort
Lecture 07 - HeapSort.pptx
Heapsort 1
Heap Sort (project).ppt
CSE680-06HeapSort.ppt
Cis435 week05
Ad

More from Vivek Bhargav (10)

PPTX
Lecture 11.2 : sorting
PPTX
Lecture 10 : trees - 2
PPTX
Lecture 9: Binary tree basics
PPTX
Lecture 7 & 8: Stack & queue
PPTX
Lecture 6: linked list
PPTX
Lecture 5: Asymptotic analysis of algorithms
PPTX
Lecture 4: Functions
PPTX
Lecture 3: Strings and Dynamic Memory Allocation
PPTX
Lecture 2: arrays and pointers
PPTX
Lecture 1: basic syntax
Lecture 11.2 : sorting
Lecture 10 : trees - 2
Lecture 9: Binary tree basics
Lecture 7 & 8: Stack & queue
Lecture 6: linked list
Lecture 5: Asymptotic analysis of algorithms
Lecture 4: Functions
Lecture 3: Strings and Dynamic Memory Allocation
Lecture 2: arrays and pointers
Lecture 1: basic syntax

Recently uploaded (20)

PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
86236642-Electric-Loco-Shed.pdf jfkduklg
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPT
Total quality management ppt for engineering students
PPTX
UNIT 4 Total Quality Management .pptx
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPTX
UNIT - 3 Total quality Management .pptx
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
communication and presentation skills 01
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
Fundamentals of Mechanical Engineering.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
86236642-Electric-Loco-Shed.pdf jfkduklg
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Total quality management ppt for engineering students
UNIT 4 Total Quality Management .pptx
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
UNIT - 3 Total quality Management .pptx
Nature of X-rays, X- Ray Equipment, Fluoroscopy
Fundamentals of safety and accident prevention -final (1).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
communication and presentation skills 01

Lecture 11.1 : heaps

  • 1. Placement Preparation Heaps Varun Raj B.Tech, CSE
  • 2. Heaps ● Basically Heaps is a binary tree satisfying the property that the value of the parent is less than the value of its children. ● This heap is known as a min heap. There is also a max-heap in which the parent is greater than its children
  • 3. Property of Heap ● The minimum element is found at the root.Proof is trivial ● The maximum element is found at the leaf. ● Hence time to find the minimum element in the array is O(1)
  • 4. Implementing a Heap There are two most common ways to implement a heap- • Using an Array .We will use this since it is more intuitive and simpler • Using Linked List by struct. This is also another method but I personally feel this is an unnecessary complication
  • 5. Implementation Some Basic Implementation Concepts • Generally in an array we start indexing from 0 in C++ but for heaps it is more convenient to start numbering of the root from 1. • For a particular node i its left child will be 2*i and its right child will be 2*i+1 and its parent will be i/2 irrespective of i being odd or even
  • 6. Inserting • We continue swapping with parent till heap condition is satisfied. • Time taken to insert an element into the heap is at worst case O(logn) -the height of the heap
  • 7. Creating a heap Method 1 using insert • Starting from the insert method described earlier to insert elements • Time complexity from this method however comes out to be log1+log2+…logn =log(n!)=Theta (nlogn). Therefore we need something faster
  • 8. Heapify Procedure • Opposite of insert procedure. • You come down the heap instead of moving up • Time complexity is again O(logn). • Therefore first create a normal array and apply heapify to all the elements of the array. Time complexity is O(n).
  • 9. Deleting an element from the heap ● In a heap ,normally you can only delete from the root. ● So you delete from the root and swap the last element of the heap with it and then you do a heapify on the root. ● Time complexity is again O(logn)
  • 10. Heapsort • Since the element at the root is always the smallest we print the element at the top and delete the root. • The next element at the root will be the second smallest element continuing like this we can sort the entire array • Since the time taken for deletion is O(logn). Therefore time taken for n deletions is O(nlogn) • Therefore time taken for heapsort is O(n)+O(nlogn)=O(nlogn)
  • 11. Advantages of Heaps • Can sort in O(nlogn) time that is as fast as merge sort but without using extra space • Can insert elements in O(logn) time compare that with sorted array. • Can remove the smallest element in O(logn) time faster than sorted array.
  • 12. Applications of Heaps • Very useful if you want a data structure that is used for lots of insertions by value • Is used in the implementation of priority queue • Is also used for sorting