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

Heap - Core Man

This document discusses heaps and heap sort. It defines a max heap as a complete binary tree where each node's value is greater than or equal to its children. It describes algorithms for inserting and deleting elements from a heap in O(log n) time. It then explains how to build a max heap from an array in O(n) time using a max-heapify procedure. Finally, it presents the heap sort algorithm, which uses a max heap to sort an array in O(n log n) time by repeatedly extracting the maximum element.

Uploaded by

Sai Karthik
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)
51 views

Heap - Core Man

This document discusses heaps and heap sort. It defines a max heap as a complete binary tree where each node's value is greater than or equal to its children. It describes algorithms for inserting and deleting elements from a heap in O(log n) time. It then explains how to build a max heap from an array in O(n) time using a max-heapify procedure. Finally, it presents the heap sort algorithm, which uses a max heap to sort an array in O(n log n) time by repeatedly extracting the maximum element.

Uploaded by

Sai Karthik
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/ 10

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
5. Set TREE[PTR] = ITEM and Return
6. Set Tree[PTR] = Tree[Par] and 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 if LAST < TREE[LEFT] then
Set PTR = LEFT
9. Set TREE[PTR] = LAST
10. Return
Build MaxHeap
BUILD_MAX-HEAP(A)
1. heapsize[A] = length[A]
2. Repeat for i = └ length[A]/2 ┘ to 1
3. Call MAX_HEAPIFY(A, i)
4. Exit
Maintaining Heap Property
MAX_HEAPIFY(A, i)
1. Set: l = LEFT (i)
2. Set: r = RIGHT (i)
3. If l <= heapsize [A] and A[l] > A[i], then:
4. largest = l.
5. Else: largest = i.
6. If r <= heapsize [A] and A[r] > A[largest], then:
7. largest = r.
8. If largest != i, then:
9. Exchange A[i] with A[largest].
10. MAX_HEAPIFY (A, largest).
Heap Sort
HEAP_SORT (A)
1. BUILD_MAXHEAP (A)
2. Repeat for i = length[A] to 2
3. Exchange A[1] with A[i].
4. Heapsize[A] = Heapsize [A] ─ 1.
5. Call MAX_HEAPIFY(A, 1).
6. Exit.
Complexity of Heap Sort
Average and Worst case Complexity of Heap sort = O(nlogn).
Questions

You might also like