0% found this document useful (0 votes)
33 views9 pages

Data Structures: Lecture: Heap

The document discusses heaps, which are complete binary trees where each node's value is greater than or equal to its children's values (for a max heap) or less than or equal (for a min heap). It covers insertion and deletion algorithms for heaps, which take O(log n) time, and heap sort, which uses a heap to sort in O(n log n) time by inserting elements into a max heap and deleting them in sorted order. Review questions and visualization resources for heaps are also provided.

Uploaded by

Nikhil Vij
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)
33 views9 pages

Data Structures: Lecture: Heap

The document discusses heaps, which are complete binary trees where each node's value is greater than or equal to its children's values (for a max heap) or less than or equal (for a min heap). It covers insertion and deletion algorithms for heaps, which take O(log n) time, and heap sort, which uses a heap to sort in O(n log n) time by inserting elements into a max heap and deleting them in sorted order. Review questions and visualization resources for heaps are also provided.

Uploaded by

Nikhil Vij
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/ 9

Data Structures

Lecture : Heap

By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Introduction
• Heap Sort
• Heap Sort Complexity
• Review Questions
Introduction
• Heap (MaxHeap): A complete binary tree H with
n elements is called a Heap if each node N of H has the
following property:
“ The value at N is greater than or equal to the value
at each of the children of N.”

• If the value at N is less than or equal to the value at each of


the children of N, then it is called MinHeap.

• Heaps are maintained in memory by using linear array


TREE.
Insertion in Heap
INSHEAP(TREE, N, ITEM)
1. Set N = N +1 and PTR = N
2. Repeat step 3 to 6 while PTR > 1
3. Set PAR = floor(PTR/2)
4. If ITEM < = TREE[PAR] then
Set TREE[PTR] = ITEM and Return
5. Set TREE[PTR] = TREE[PAR]
6. Set PTR = PAR
7. Set TREE[1] = ITEM
8. Return
Deletion in Heap
DELHEAP(TREE, N, ITEM)
1. Set ITEM = TREE[1]
2. Set LAST = TREE[N] and N = N – 1
3. Set PTR = 1, LEFT = 2 and RIGHT = 3
4. Repeat step 5 to 7 while RIGHT <= N
5. If LAST >= TREE[LEFT] and LAST >= TREE[RIGHT] then
Set TREE[PTR] = LAST and Return
6. If TREE[RIGHT] <= TREE[ LEFT] then
Set TREE[PTR] = TREE[LEFT] and PTR = LEFT
Else
Set TREE[PTR] = TREE[RIGHT] and PTR = RIGHT
7. Set LEFT = 2*PTR and RIGHT = LEFT + 1
8. If LEFT = N and LAST < TREE[LEFT] then
Set TREE[PTR] = TREE[LEFT] and PTR = LEFT
9. Set TREE[PTR] = LAST
10. Return
Heap Sort
HEAPSORT(A, N)
1. Repeat for J = 1 to N-1
Call INSHEAP(A, J, A[J+1])
2. Repeat while N > 1
(a) Call DELHEAP(A, N, ITEM)
(b) Set A[N+1] = ITEM
3. Exit
Complexity of Heap Sort
Average and Worst case Complexity of Heap sort = O(nlogn).
Questions
• Min Heap
• https://www.cs.usfca.edu/~galles/visualization
/Heap.html
• Max Heap
• https://visualgo.net/en/heap
• https://yongdanielliang.github.io/animation/w
eb/Heap.html

You might also like