0% found this document useful (0 votes)
10 views

Heap Data Structure and Heap Sort.pptx

Uploaded by

Pranjal Kusnake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Heap Data Structure and Heap Sort.pptx

Uploaded by

Pranjal Kusnake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Heap Data Structure and

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)

If a BST has n nodes


• Maximum height of the BST is n-1
• Minimum height of the BST is floor(log2n)
Full Binary Tree
Full binary tree: A binary tree is which each node has exactly 2 or 0
children
Complete Binary Tree
Complete binary tree: A binary tree in which every level, except
possibly the deepest is completely filled. At the final level, all nodes are
as far left as possible

Where would the next node go to maintain a complete tree?


What is the height of a complete binary tree with n nodes?
Perfect Binary Tree
Perfect binary tree: a binary tree with all leaf nodes at the same depth.
All internal nodes have exactly two children.
A perfect binary tree has the maximum number of nodes for a given
height
A perfect binary tree has (2(h+1) - 1) nodes where h is the height of the
tree
• height = 0 -> 1 node
• height = 1 -> 3 nodes
• height = 2 -> 7 nodes
• height = 3 -> 15 nodes
Heap Example

Properties of heap are:


• Completeness Property
• Order Property: For max heap, value of parent must be greater than value of its
children. For min heap, value of parent must be less than the value of its children.
Types of Heap

Heap Representations

Maintaining the Heap Property
Example Maximum-Heapify
Max-Heapify with i=2
Building a Max Heap
Heapsort Algorithm

Discussion
• Heapsort is an excellent algorithm. However, a good implementation
of quicksort beats it usually.
• But the heap data structure itself has enough utility.
• One of the most popular applications of heapsort is its use in building
a priority queue.
• As with heaps, there are two kinds of priority queues: max-priority
queues and min-priority queues.
Priority Queue

Operations
HEAP-MAXIMUM(A)
return A[1]

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

You might also like