Heap Data Structure and Heap Sort.pptx
Heap Data Structure and Heap Sort.pptx
Heap Sort
Some Important Points Related to Binary Trees
• The height of the binary tree is the longest path from root node to any
leaf node in the tree.
Height = 2
Height = 4
Some Important Points Related to Binary Trees
If binary tree has height h
• Minimum number of nodes is h+1 (in case of left skewed and right
skewed binary tree).
• Maximum number of nodes is 2h+1-1 (full binary tree)
HEAP-EXTRACT-MAX(A)
If Heap-Size[A] < 1
then Error “Heap Underflow”
max = A[1]
A[1] = A[Heap-Size[A]]
Heap-Size[A] = Heap-Size[A] – 1
Max-Heapify(A,1)
return max
Operations
HEAP-INCREASE-KEY(A,i,key)
if key<A[i]
then error “new key is smaller than current key”
A[i] = key
while i>1 and A[i] > A[Parent(i)]
exchange A[i] and A[Parent(i)]
i = Parent(i)
Operations
•