SlideShare una empresa de Scribd logo
1 de 13
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

Más contenido relacionado

La actualidad más candente

Climate data in r with the raster package
Climate data in r with the raster packageClimate data in r with the raster package
Climate data in r with the raster package
Alberto Labarga
 
Processing Big Data in Realtime
Processing Big Data in RealtimeProcessing Big Data in Realtime
Processing Big Data in Realtime
Tikal Knowledge
 

La actualidad más candente (20)

Deep Learning on Aerial Imagery: What does it look like on a map?
Deep Learning on Aerial Imagery: What does it look like on a map?Deep Learning on Aerial Imagery: What does it look like on a map?
Deep Learning on Aerial Imagery: What does it look like on a map?
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heapsort
HeapsortHeapsort
Heapsort
 
Deep Learning meetup
Deep Learning meetupDeep Learning meetup
Deep Learning meetup
 
Raster Processing with Scipy.ndimage (Dev Meet Up II)
Raster Processing with Scipy.ndimage (Dev Meet Up II)Raster Processing with Scipy.ndimage (Dev Meet Up II)
Raster Processing with Scipy.ndimage (Dev Meet Up II)
 
Hubba Deep Learning
Hubba Deep LearningHubba Deep Learning
Hubba Deep Learning
 
Climate data in r with the raster package
Climate data in r with the raster packageClimate data in r with the raster package
Climate data in r with the raster package
 
HeapSort
HeapSortHeapSort
HeapSort
 
Processing Big Data in Real-Time - Yanai Franchi, Tikal
Processing Big Data in Real-Time - Yanai Franchi, TikalProcessing Big Data in Real-Time - Yanai Franchi, Tikal
Processing Big Data in Real-Time - Yanai Franchi, Tikal
 
whileloop
whileloopwhileloop
whileloop
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4
 
Introduction to Date and Time API 3
Introduction to Date and Time API 3Introduction to Date and Time API 3
Introduction to Date and Time API 3
 
LocationTech Projects
LocationTech ProjectsLocationTech Projects
LocationTech Projects
 
Getting date and time from ntp server with esp8266 node mcu
Getting date and time from ntp server with esp8266 node mcuGetting date and time from ntp server with esp8266 node mcu
Getting date and time from ntp server with esp8266 node mcu
 
Working with OpenStreetMap using Apache Spark and Geotrellis
Working with OpenStreetMap using Apache Spark and GeotrellisWorking with OpenStreetMap using Apache Spark and Geotrellis
Working with OpenStreetMap using Apache Spark and Geotrellis
 
Terark Product and Technology
Terark Product and TechnologyTerark Product and Technology
Terark Product and Technology
 
Brand new Date and Time API
Brand new Date and Time APIBrand new Date and Time API
Brand new Date and Time API
 
Apache Storm
Apache StormApache Storm
Apache Storm
 
Processing Big Data in Realtime
Processing Big Data in RealtimeProcessing Big Data in Realtime
Processing Big Data in Realtime
 
Prezo at-mesos con2015-final
Prezo at-mesos con2015-finalPrezo at-mesos con2015-final
Prezo at-mesos con2015-final
 

Similar a Lecture 11.1 : heaps

Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
AmitShou
 

Similar a Lecture 11.1 : heaps (20)

Ch2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptxCh2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptx
 
Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sorting
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
Complexity in array
Complexity in arrayComplexity in array
Complexity in array
 
Heap sort
Heap sort Heap sort
Heap sort
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
 
Mastering Python chapter3
Mastering Python chapter3Mastering Python chapter3
Mastering Python chapter3
 
How bubble sort works
How bubble sort worksHow bubble sort works
How bubble sort works
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Smooth Sort
Smooth SortSmooth Sort
Smooth Sort
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
shell and merge sort
shell and merge sortshell and merge sort
shell and merge sort
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 

Más de Vivek Bhargav

Más de Vivek Bhargav (9)

Lecture 10 : trees - 2
Lecture 10 : trees - 2Lecture 10 : trees - 2
Lecture 10 : trees - 2
 
Lecture 9: Binary tree basics
Lecture 9: Binary tree basicsLecture 9: Binary tree basics
Lecture 9: Binary tree basics
 
Lecture 7 & 8: Stack & queue
Lecture 7 & 8: Stack  & queueLecture 7 & 8: Stack  & queue
Lecture 7 & 8: Stack & queue
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked list
 
Lecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsLecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithms
 
Lecture 4: Functions
Lecture 4: FunctionsLecture 4: Functions
Lecture 4: Functions
 
Lecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationLecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory Allocation
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
 
Lecture 1: basic syntax
Lecture 1: basic syntaxLecture 1: basic syntax
Lecture 1: basic syntax
 

Último

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 

Último (20)

chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 

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