



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























What is the time complexity of this algorithm?
Building a Binary heap incrementally
The time complexity for inserting a leaf node =?
๏จ Theorem: Time complexity of building a binary heap incrementally is O (๐ log ๐). 5
O (log ๐ )
What useful inference can you draw from this Theorem?
Top-down approach
Building a Binary heap incrementally
Top-down approach
Think of alternate approach for building a binary heap
heap property:?
We just need to ensure this property at each node. 8
โEvery node stores value smaller than its children โ
In any complete binary tree , how many nodes satisfy heap property?
all leaf nodes ๏
Bottom-up approach
Does it suggest a new approach to build binary heap?
A new approach to build binary heap
H^98 14 33^37 11 52 32 85^17 25 88 41 21^29 76 47 75 9 57
The first node to be processed.
H^98 14 33^37 11 52 32 85^9 23 88 41 21^29 76 47 75 17 57
The third node to be processed.
H^98 14 33^37 11 52 32 47^9 23 88 41 21^29 76 85 75 17 57
v
H^98 54 21^9 11 33 29 47 17 23 88 41 52^32 76 85 75 37 57^25
v
H^98 54 21^9 11 33 29 47 17 23 88 41 52^32 76 85 75 37 57^25
For node ๐, compare its value with those of its children
If it is smaller than any of its children ๏จ Swap it with smallest child and move down โฆ
Else stop!
๐ โค (๐โ1)/2 Flag = true
Flag ๏ true;
๐๐ + ๐ โค ๐ and H[ ๐๐๐ ]>H[ ๐๐ + ๐ ]