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

Heap

Uploaded by

surya332ygl
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)
27 views13 pages

Heap

Uploaded by

surya332ygl
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

02 - Heap Sort

Algorithm
23AID204
Advanced Data Structures & Algorithm Analysis

ULLAS S.
Assistant Professor
AMRITA SCHOOL OF COMPUTING
BENGALURU
Heap

• A heap is a complete binary tree, and the binary tree is a tree in which
the node can have utmost two children.
• A complete binary tree is a binary tree in which all the levels except the
last level, i.e., leaf node should be completely filled, and all the nodes
should be left-justified
• There are two types of the heap:
• Min Heap: The value of the parent node should be less than or equal to either
of its children
• Max heap: The value of the parent node is greater than or equal to its children.

Advanced Data Structures & Algorithm Analysis 2


Insertion in the Heap tree

44, 33, 77, 11, 55, 88, 66


• Suppose we want to create the max heap tree. To create the max
heap tree, we need to consider the following two cases:
• First, we have to insert the element in such a way that the property of the
complete binary tree must be maintained.
• Secondly, the value of the parent node should be greater than the either of its
child.

Advanced Data Structures & Algorithm Analysis 3


Insert: 44, 33, 77, 11, 55, 88, 66

• First we add the 44 element in the tree. 44

• 33 will be added at the left of 44.


• 77 and it will be added to the right of the 33 77
44
• As we can observe in the above tree that
it does not satisfy the max heap property, 77
i.e., parent node 44 is less than the child
77. So, we will swap these two values
33 44
Advanced Data Structures & Algorithm Analysis 4
Insert: 44, 33, 77, 11, 55, 88, 66

• The next element is 11. The


node 11 is added to the left of
33
• The next element is 55. To make
it a complete binary tree, we will
add the node 55 to the right of
33

Advanced Data Structures & Algorithm Analysis 5


Insert: 44, 33, 77, 11, 55, 88, 66

• The next element is 55. To make


it a complete binary tree, we will
add the node 55 to the right of
33
• It does not satisfy the property
of the max heap because 33<55

Advanced Data Structures & Algorithm Analysis 6


Insert: 44, 33, 77, 11, 55, 88, 66

• The next element is 88. The left


subtree is completed so we will
add 88 to the left of 44
• It does not satisfy the property
of the max heap because 44<88,
so we will swap these two
values.
• Again, it is violating the max
heap property because 88>77 so
we will swap these two values
Advanced Data Structures & Algorithm Analysis 7
Insert: 44, 33, 77, 11, 55, 88, 66
88
• The next element is 66. To make
a complete binary tree, we will
add the 66 element to the right 55 77
side of 77

11 33 44 66

Advanced Data Structures & Algorithm Analysis 8


Algorithm to insert an element in the max h
eap
// Condition to check whether the value of parent is
insertHeap(A, n, value) less than the given node or not

{ if(A[parent]<A[i])

n=n+1; // n is incremented to insert the new element {

A[n]=value; // assign new value at the nth position swap(A[parent], A[i]);

i = n; // assign the value of n to i i = parent;

// loop will be executed until i becomes 1. }

while(i>1) else

{ {

parent= floor value of i/ return;


2; // Calculating the floor value of i/2
} } }
Advanced Data Structures & Algorithm Analysis 9
Deletion in Heap Tree

• In Deletion in the heap tree, the


root node is always deleted and
it is replaced with the last
element.

Advanced Data Structures & Algorithm Analysis 10


Algorithm to heapify the tree

MaxHeapify(A, n, i) while(r<=n && A[r]>A[largest])


{ {
int largest =i; largest=r;
int l= 2i; }
int r= 2i+1; if(largest!=i)
while(l<=n && A[l]>A[largest]) {
{ swap(A[largest], A[i]);
largest=l; heapify(A, n, largest);
} }}
Advanced Data Structures & Algorithm Analysis 11
Reference

• https://www.javatpoint.com/heap-data-structure

Advanced Data Structures & Algorithm Analysis 12


Advanced Data Structures & Algorithm Analysis 13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy