1/4/2023
Content
• Introduction to Heap
• Representation of Heap
DATA STRUCTURES • Operations on Heaps
• Insertion(item)
• Delete(item)
Heap
• Application of Heap
• Priority Queue
By • Heap Sort
Zainab Malik
3 4
Introduction to Heap Introduction to Heap: Max Heap vs. Min Heap
• A heap is binary tree that satisfies the following properties
• Shape property: Heap must be a complete binary tree
• Order property: It must be either Max heap or Min heap
• Max heap
• For every node in the heap, the value stored in that node is greater than
or equal to the value in each of its children
• Min heap
• For every node in the heap, the value stored in that node is less than or
equal to the value in each of its children
5 6
Heap Representation Operations on Heaps
• Heap is a Complete Binary Tree. This property of Binary • On heaps, only two operations are performed
Heap makes it suitable to be stored in a linear array. • Insertion
• Each node is assigned a numeric label and a node is • Deletion
stored in an array at a position with same index as its
associated label.
1
1/4/2023
7 8
Heap Operation: Insertion(item) Heap Operation: Insertion(item)
• Item is always inserted as last (bottom) child of the original heap. • Insert(item,n,heap):
1. Set n=n+1
• After insertion, shape property remain undisturbed, but the order may
2. Set heap[n]=item
get violated if a larger item (incase of Max Heap) or smaller item
3. Call reheapifyUpward(heap n)
(incase of Min Heap) is inserted.
4. Return
• To satisfy the order property, heap needs to be readjusted in terms of
its structure (reheapifyUpward) • ReheapifyUpward(heap, start)
1. If heap[start] is not a root node then
• ReheapifyUpward: 2. If(heap[parent]<=heap[start]) then
• It involves moving the items up from the last (bottom) position until 3. Set index = index of the child with largest value
either it ends up in a position where the order property satisfied or it 4. Swap heap[parent] and heap[index]
hits the root node. 5. Call repheapifyUpward(heap, parent)
Endif
Endif
5. Return
9 10
Heap Operation: Insertion(item=90) Heap Operation: Deletion()
• Element is always deleted from the root of the heap
• When the element is deleted from the root, it creates a vacant
space in the root position
• As heap must be a complete binary tree, so the vacant space is
filled by last (bottom right) element of the heap
(a) (b) • Like insertion, this replacement ensures the shape property but
disturbs the order property that needs to be satisfied by mean of
reheapify (reheapifyDownward).
• reheapifyDownward:
• It involves moving the element down from the root position until
either it ends up in a position where order property is satisfied or it
hits the leaf node.
(c) (d)
11 12
Heap Operation: Deletion() Heap Operation: Deletion()
• Delete(n,heap):
1. Set item=heap[root]
2. Set heap[root]=heap[n]
3. Set n=n-1
4. Call reheapifyDownward(heap,root)
5. Return item
• ReheapifyDownward(heap, start) (a) (b)
1. If heap[start] is not a leaf node then
2. Set index = index of the child with largest value
3. If(heap[start]<=heap[index]) then
4. Swap heap[index] and heap[start]
5. Call repheapifyDownward(heap, index)
Endif
Endif
6. Return
(c) (d)
2
1/4/2023
13 14
Applications of Heap Priority Queue using Heap
• Priority Queue • Each Node in a heap have two types of information i.e.
• Heap Sort the content and an associated priority
• Heap is build with respect to priority which means that the
element with highest priority will be at root node.
• As in heap we always delete from the root therefore,
whenever a node will be removed for processing it will be
of highest priority
15 16
Priority Heap Operation: Insertion(item) Priority Heap Operation: Deletion()
• Insert(item,n,heap)://item must be an object of Element containing • Delete(n,heap):
both content and priority 1. Set item=heap[root]
1. Set n=n+1 2. Set heap[root]=heap[n]
2. Set heap[n]=item 3. Set n=n-1
3. Call reheapifyUpward(heap n) 4. Call reheapifyDownward(heap,root)
4. Return 5. Return item
• ReheapifyUpward(heap, start) • ReheapifyDownward(heap, start)
1. If heap[start] is not a root node then 1. If heap[start] is not a leaf node then
2. If(heap[parent].priority<=heap[start].priority) then 2. Set index = index of the child with largest priority value
3. Set index = index of the child with largest priority value 3. If(heap[start].priority<=heap[index].priority) then
4. Swap heap[parent] and heap[index] 4. Swap heap[index] and heap[start]
5. Call repheapifyUpward(heap, parent) 5. Call repheapifyDownward(heap, index)
Endif Endif
Endif Endif
5. Return 6. Return
17 18
Applications of Heap Heap Sort
• Priority Queue • HeapSort(a,n) //𝑎 is a linear array and n is the last element of 𝑎
1. Call Heapify(a, n)
• Heap Sort
2. Repeat Step 3 and 4 For i=n to 1 in steps of -1
3. Swap elements a[1] with a[i]
4. Call ReheapifyDownward(a,1) (see slide # 11)
5. Endfor
6. Return
Heapify(a,n)
1. Set index=Parent of node with index n
2. Repeat step 3 For i=index to 1 in setp of -1
3. Call reheapifyDownward(a,i) (see slide # 8)
4. Endif
5. Return
3
1/4/2023
19 20
Heap Sort Heap Sort
Unsorted Array • HeapSort(a,n) //𝑎 is a linear array and n is the last element of 𝑎
1 2 3 4 5 6 7 1. Call Heapify(a, n)
10 5 70 15 12 35 50 2. Repeat Step 3 and 4 For i=n to 1 in steps of -1
3. Swap elements a[1] with a[i]
4. Call ReheapifyDownward(a,1) on Heap from 1 to n-1 (see slide # 11)
5. Endfor
6. Return
Heapify(a,n)
1. Set index=Parent of node with index n
2. Repeat step 3 For i=index to 1 in setp of -1
3. Call reheapifyDownward(a,i) (see slide # 8)
4. Endif
5. Return
Equivalent Binary Tree
21 22
Unsorted Array Unsorted Array
1 2 3 4 5 6 7 1 2 3 4 5 6 7
10 5 70 15 12 35 50 10 5 15 70 15 5 12 35 50 Unsorted Array
1 2 3 4 5 6 7
70 15 50 5 12 35 10
Unsorted Array
1 2 3 4 5 6 7 Unsorted Array
1 2 3 4 5 6 7
10 70 15 70 10 5 12 35 50
70 15 10 50 5 12 35 50 10
23 24
Unsorted Unsorted Sorted
Heap Sort
1 2 3 4 5 6 7 1 2 3 4 5 6 7
70 15 50 5 12 35 10 50 15 10 5 12 35 70
• HeapSort(a,n) //𝑎 is a linear array and n is the last element of 𝑎
1. Call Heapify(a, n)
2. Repeat Step 3 and 4 For i=n to 1 in steps of -1
3. Swap elements a[1] with a[i]
4. Call ReheapifyDownward(a,1) on Heap from 1 to n-1 (see slide # 11)
5. Endfor
6. Return
Heapify(a,n)
1. Set index=Parent of node with index n
2. Repeat step 3 For i=index to 1 in setp of -1
3. Call reheapifyDownwardward(a,i) (see slide # 8)
4. Endif
5. Return
4
1/4/2023
25 26
Unsorted Sorted Unsorted Sorted
1 2 3 4 5 6 7 1 2 3 4 5 6 7
After First Iteration of
1. Repeat Step 3 and 4 For i=n to 1 in steps of -1 50 15 35 5 12 10 70 10 15 35 5 12 50 70
2. Swap elements a[1] with a[i]
3. Call ReheapifyDownward(a,1) on Heap from 1 to n-1 (see slide # 11)
4. Endfor
Unsorted Sorted
1 2 3 4 5 6 7
50 15 35 5 12 10 70
27 28
Unsorted Sorted Unsorted Sorted
After second Iteration of 1 2 3 4 5 6 7 1 2 3 4 5 6 7
1. Repeat Step 3 and 4 For i=n to 1 in steps of -1 35 15 10 5 12 50 70 12 15 10 5 35 50 70
2. Swap elements a[1] with a[i]
3. Call ReheapifyDownward(a,1) on Heap from 1 to
n-1 (see slide # 11)
4. Endfor
Unsorted Sorted
1 2 3 4 5 6 7
35 15 10 5 12 50 70
29 30
Unsorted Array Sorted Unsorted Array Sorted
After Third Iteration of 1 2 3 4 5 6 7 1 2 3 4 5 6 7
1. Repeat Step 3 and 4 For i=n to 1 in steps of -1 15 12 10 5 35 50 70 5 12 10 15 35 50 70
2. Swap elements a[1] with a[i]
3. Call ReheapifyDownward(a,1) (see slide # 11)
4. Endfor
Unsorted Sorted
1 2 3 4 5 6 7
15 12 10 5 35 50 70
5
1/4/2023
31 32
Unsorted Sorted Unsorted Sorted
After 4th Iteration of 1 2 3 4 5 6 7 1 2 3 4 5 6 7
1. Repeat Step 3 and 4 For i=n to 1 in steps of -1 12 5 10 15 35 50 70 10 5 12 15 35 50 70
2. Swap elements a[1] with a[i]
3. Call ReheapifyDownward(a,1) (see slide # 11)
4. Endfor
Unsorted Sorted
1 2 3 4 5 6 7 Unsorted Sorted
12 5 10 15 35 50 70 1 2 3 4 5 6 7
10 5 12 15 35 50 70
After 5th Iteration of
12 1. Repeat Step 3 and 4 For i=n to 2 in steps of -1
1 2. Swap elements a[1] with a[i]
5 10 3. Call ReheapifyDownward(a,1,i-1) (see slide # 11)
4. Endfor
2 3
33 34
Unsorted Sorted Unsorted Sorted
1 2 3 4 5 6 7 1 2 3 4 5 6 7
10 5 12 15 35 50 70 5 10 12 15 35 50 70
Another Example of Heapsort
https://www.cs.usfca.edu/~galles/visualization/HeapSort.html
After 6th Iteration of
1. Repeat Step 3 and 4 For i=n to 1 in steps of -1
2. Swap elements a[1] with a[i]
3. Call ReheapifyDownward(a,1) (see slide # 11)
4. Endfor Sorted Array
1 2 3 4 5 6 7
5 10 12 15 35 50 70
35
Thank You