Binary Heaps Notes For GATE _ Introduction to Binary Heaps - Data Structures
Binary Heaps Notes For GATE _ Introduction to Binary Heaps - Data Structures
Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App fo
BYJU'S GATE (/gate/) > GATE (/gate/gate-exam/) > GATE Study Material (/gate/study-material/) > GATE Notes For CSE (/gate/gate-notes-for-cse/) > Introduction to Data structures
(tel:+919241333666)
Binary Heap is an important topic belonging to the famous chapter of Computer Science i.e. Data Structure. And, when it comes to a competitive
examination like GATE, you have to read the whole topic quite deeply. In this article, we have covered all the topics relevant to the binary heap. We
hope the notes for the CSE topics will help you understand this topic in a better way.
Topic of Contents
Advantages of Heap
Disadvantages of Heap
Types of Heaps
There are two types of heaps:
1. Max Heap
2. Min Heap
3. Max Heap: A max-heap is a complete binary tree in which the value in each internal node is greater than or equal to the values in the
children of that node.
https://byjus.com/gate/binary-heaps-notes/ 1/13
2/14/25, 11:46 AM Binary Heaps Notes For GATE | Introduction to Binary Heaps - Data Structures
tech.byjusweb.com/revive/www/delivery/cl.php?
bannerid=82&zoneid=201&sig=9bd29b5e3eed52cfbaa13a2bff86688a551920d65cfe4ac05b71169dbcf79afd&dest=https%3A%2
fta-gate-hts%3Futm_source%3Dbep_crm%26utm_medium%3Dseo_pages%26utm_campaign%3Dgate_fta)
In this picture you can see that the root element has maximum value, which means in the max heap the root element is always greater.
NOTE: This is very important, in the max heap, the parent value should always be greater than a child node at every level.
1. Min Heap: In a Min-Heap the key present at the root node must be less than or equal among the keys present at all of its children.
In this picture, you can see that the root element has minimum value, which means in the min heap the root element is always minimum.
NOTE: This is very important, in the min heap, the parent value should always be smaller than a child node at every level.
https://byjus.com/gate/binary-heaps-notes/ 2/13
2/14/25, 11:46 AM Binary Heaps Notes For GATE | Introduction to Binary Heaps - Data Structures
Figure: Representation of Binary Heap
Start your FREE GATE Preparation Today (https://ad-
With BYJU'S Exam Prep Free Trial
tech.byjusweb.com/revive/www/delivery/cl.php?
bannerid=82&zoneid=201&sig=9bd29b5e3eed52cfbaa13a2bff86688a551920d65cfe4ac05b71169dbcf79afd&dest=https%3A%2
fta-gate-hts%3Futm_source%3Dbep_crm%26utm_medium%3Dseo_pages%26utm_campaign%3Dgate_fta)
1. Heapify Method
When it comes to creating a heap tree (Min Heap or Max Heap), the heapify method is most suitable, because it takes less time as compared to
the other methods.
Explanation of Picture
In this example we have used Heapify method:
https://byjus.com/gate/binary-heaps-notes/ 3/13
2/14/25, 11:46 AM Binary Heaps Notes For GATE | Introduction to Binary Heaps - Data Structures
Heapify → It is a method to rearrange the heap to sustain heap property.
Start your FREE GATE Preparation Today (https://ad-
With BYJU'S Exam Prep Free Trial
In the above picture, we have a set of arrays and we have to create a Max heap from that. Whenever we have to create a tree, we have to start
from the left side. We have created a basic tree that you can see in the picture as “Initial Elements”.
tech.byjusweb.com/revive/www/delivery/cl.php?
Now, to create a max heap, we have to keep one thing in mind that the parent node should be greater than the child node at every level. If you
bannerid=82&zoneid=201&sig=9bd29b5e3eed52cfbaa13a2bff86688a551920d65cfe4ac05b71169dbcf79afd&dest=https%3A%2
noticed, the second level 3 is a parent node and 8 is a child node. Here, the parent node is smaller than the child node, so we have to swap them.
fta-gate-hts%3Futm_source%3Dbep_crm%26utm_medium%3Dseo_pages%26utm_campaign%3Dgate_fta)
Once we swap, 8 will take the position of 3.
Now the main parent node has 4 values which are smaller than the child value, we again play swap games and this way we can get the greater
value in the main parent node.
Insertion: Firstly, we have to insert the new element at the end of the heap and we always start from the left side. After inserting the node, we have
to analyze the heap property as the new elements can affect the sequence. We will apply a heapify method to create a proper heap. You can
create a Max or Min heap according to your preference or the question asked in the exam.
Deletion: In the deletion process, we replace the element to be removed by the last rightmost element in the heap. After placing the last element in
place of the deleted one, we have to check whether the heap is following the property of the heap or not. If not, we have to set it according to the
heapify method.
https://byjus.com/gate/binary-heaps-notes/ 4/13
2/14/25, 11:46 AM Binary Heaps Notes For GATE | Introduction to Binary Heaps - Data Structures
tech.byjusweb.com/revive/www/delivery/cl.php?
bannerid=82&zoneid=201&sig=9bd29b5e3eed52cfbaa13a2bff86688a551920d65cfe4ac05b71169dbcf79afd&dest=https%3A%2
fta-gate-hts%3Futm_source%3Dbep_crm%26utm_medium%3Dseo_pages%26utm_campaign%3Dgate_fta)
600
Advantages of Heap
1. Heap data structure preferred graph algorithms.
https://byjus.com/gate/binary-heaps-notes/ 5/13
2/14/25, 11:46 AM Binary Heaps Notes For GATE | Introduction to Binary Heaps - Data Structures
4. The Heap method is also used in the Priority Queue.
Start your FREE GATE Preparation Today (https://ad-
With BYJU'S Exam Prep Free Trial
5. It allows you to access variables globally.
Array Index 1 2 3 4 5 6 7 8 9
Value 40 30 20 10 15 16 17 8 4
Now consider that a value 35 is inserted into this heap. After insertion, the new heap is
Q. A max-heap is a heap where the value of each parent is greater than or equal to the value of its children. Which of the following is a max-heap?
(A)
(B)
(C)
(D)
https://byjus.com/gate/binary-heaps-notes/ 6/13
2/14/25, 11:46 AM Binary Heaps Notes For GATE | Introduction to Binary Heaps - Data Structures
tech.byjusweb.com/revive/www/delivery/cl.php?
bannerid=82&zoneid=201&sig=9bd29b5e3eed52cfbaa13a2bff86688a551920d65cfe4ac05b71169dbcf79afd&dest=https%3A%2
fta-gate-hts%3Futm_source%3Dbep_crm%26utm_medium%3Dseo_pages%26utm_campaign%3Dgate_fta)
Keep learning and stay tuned to get the latest updates on GATE Exam (https://byjus.com/gate/gate-exam/) along with GATE Eligibility Criteria
(https://byjus.com/gate/gate-eligibility-criteria/), GATE 2023 (https://byjus.com/gate/), GATE Admit Card (https://byjus.com/gate/gate-admit-
card/), GATE Syllabus for CSE (Computer Science Engineering) (https://byjus.com/gate/gate-syllabus-for-computer-science-engineering/), GATE
CSE Notes (https://byjus.com/gate/gate-notes-for-cse/), GATE CSE Question Paper (https://byjus.com/gate/gate-cse-question-paper/), and
more.
Also Explore,
Trees (https://byjus.com/gate/tree-notes/)
Comments
Leave a Comment
Your Mobile number and Email id will not be published. Required fields are marked *
*
https://byjus.com/gate/binary-heaps-notes/ 7/13
2/14/25, 11:46 AM Binary Heaps Notes For GATE | Introduction to Binary Heaps - Data Structures
*
Start your FREE GATE Preparation Today
Type your message or doubt here...
(https://ad-
With BYJU'S Exam Prep Free Trial
tech.byjusweb.com/revive/www/delivery/cl.php?
bannerid=82&zoneid=201&sig=9bd29b5e3eed52cfbaa13a2bff86688a551920d65cfe4ac05b71169dbcf79afd&dest=https%3A%2
fta-gate-hts%3Futm_source%3Dbep_crm%26utm_medium%3Dseo_pages%26utm_campaign%3Dgate_fta)
Post My Comment
Mobile Number
Submit
https://byjus.com/gate/binary-heaps-notes/ 8/13
2/14/25, 11:46 AM Binary Heaps Notes For GATE | Introduction to Binary Heaps - Data Structures
tech.byjusweb.com/revive/www/delivery/cl.php?
bannerid=82&zoneid=201&sig=9bd29b5e3eed52cfbaa13a2bff86688a551920d65cfe4ac05b71169dbcf79afd&dest=https%3A%2
fta-gate-hts%3Futm_source%3Dbep_crm%26utm_medium%3Dseo_pages%26utm_campaign%3Dgate_fta)
(https://ad-tech.byjusweb.com/revive/www/delivery/cl.php?
bannerid=86&zoneid=111&OXLCA=1&sig=3db76378521bec0285a81b7e6a8da07d85217d30e75f4f11adb618a720fad2f3&dest=https%3A%2F%2Fbyjusexam
test-
series%3FopenLogin%3Dy%26jsdelay%3Dfalse%26utm_campaign%3DbyjusIntegration%26utm_medium%3DTSWidget%26utm_source%3DGovtExam)
https://byjus.com/gate/binary-heaps-notes/ 9/13
2/14/25, 11:46 AM Binary Heaps Notes For GATE | Introduction to Binary Heaps - Data Structures
tech.byjusweb.com/revive/www/delivery/cl.php?
bannerid=82&zoneid=201&sig=9bd29b5e3eed52cfbaa13a2bff86688a551920d65cfe4ac05b71169dbcf79afd&dest=https%3A%2
fta-gate-hts%3Futm_source%3Dbep_crm%26utm_medium%3Dseo_pages%26utm_campaign%3Dgate_fta)
(https://ad-tech.byjusweb.com/revive/www/delivery/cl.php?
bannerid=140&zoneid=333&OXLCA=1&sig=ccd79e0a55a8e5b9608e31c0c3dfdb360e88822b79068da642840fce9ddf9a79&dest=https%3A%2F%2Fbyju
year-
papers%3FopenLogin%3Dy%26jsdelay%3Dfalse%26utm_campaign%3DbyjusIntegration%26utm_medium%3DPYSP%26utm_source%3DGATE)
https://byjus.com/gate/binary-heaps-notes/ 10/13
2/14/25, 11:46 AM Binary Heaps Notes For GATE | Introduction to Binary Heaps - Data Structures
tech.byjusweb.com/revive/www/delivery/cl.php?
bannerid=82&zoneid=201&sig=9bd29b5e3eed52cfbaa13a2bff86688a551920d65cfe4ac05b71169dbcf79afd&dest=https%3A%2
fta-gate-hts%3Futm_source%3Dbep_crm%26utm_medium%3Dseo_pages%26utm_campaign%3Dgate_fta)
(https://ad-tech.byjusweb.com/revive/www/delivery/cl.php?
bannerid=141&zoneid=334&OXLCA=1&sig=6676ae51bd547f3c7a2405450200c1681f75006eee40eeaca90ff958c23cc5a2&dest=https%3A%2F%2Fbyjusex
test%2Fgate-exams%3Futm_source%3Dbyjus.com%26utm_medium%3Dgate-sidebanner%26utm_campaign%3Dbyjus-exam-prep-
scholarship-test-2024-21-jan)
Introduction To Array
https://byjus.com/gate/binary-heaps-notes/ 11/13
2/14/25, 11:46 AM Binary Heaps Notes For GATE | Introduction to Binary Heaps - Data Structures
tech.byjusweb.com/revive/www/delivery/cl.php?
Introduction To Recursion
bannerid=82&zoneid=201&sig=9bd29b5e3eed52cfbaa13a2bff86688a551920d65cfe4ac05b71169dbcf79afd&dest=https%3A%2
fta-gate-hts%3Futm_source%3Dbep_crm%26utm_medium%3Dseo_pages%26utm_campaign%3Dgate_fta)
String In C
Introduction To Tree
Decomposition In DBMS
COURSES EXAMS
https://byjus.com/gate/binary-heaps-notes/ 12/13
2/14/25, 11:46 AM Binary Heaps Notes For GATE | Introduction to Binary Heaps - Data Structures
RESOURCES Blog (https://blog.byjus.com/)
Start your FREE GATE Preparation Today (https://ad-
CAT College Predictor (/free-cat-prep/cat-college-predictor/) BYJU'S Give (/give/)
With BYJU'S Exam Prep Free Trial
Worksheets (/worksheets/)
Calculators (/calculators/)
Formulas (/formulas/)
FOLLOW US
(https://www.facebook.com/byjuslearningapp/) (https://in.linkedin.com/company/byjus)
Disclaimer (/disclaimer/) Privacy Policy (/tnc_app/#privacydesc) Terms of Services (/tnc_app/#tncdesc) Sitemap (/sitemap.xml)
https://byjus.com/gate/binary-heaps-notes/ 13/13