0% found this document useful (0 votes)
20 views17 pages

Heap - Core Man Updated

21. Heap_Core man Updated
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)
20 views17 pages

Heap - Core Man Updated

21. Heap_Core man Updated
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/ 17

Data Structures

Lecture : Heap

By
Mukesh Sharma
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Introduction
• Heap Sort
• Heap Sort Complexity
• Review Questions
Introduction
• A binary heap is a complete binary tree in
which every node satisfies the heap property
which states that:
• If B is a child of A, then key(A) ≥ key(B)

• 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.


What is a complete binary tree?
A complete binary tree is a binary tree in which all the levels except the
last level, i.e., leaf node should be completely filled, and all the nodes
should be left-justified.
Max Heap-
In max heap, every node contains greater or equal value element than its child nodes.
Thus, root node contains the largest value element.
Min Heap-
Min Heap conforms to the above properties of heap.
In min heap, every node contains lesser value element than its child nodes.
Thus, root node contains the smallest value element.
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
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).
// Max-Heap data structure in C void insert(int array[], int newNum)
#include <stdio.h> {
int size = 0; if (size == 0)
void swap(int *a, int *b) {
{ array[0] = newNum;
int temp = *b; size += 1;
*b = *a; }
*a = temp; else
} {
void heapify(int array[], int size, int i) array[size] = newNum;
{ size += 1;
if (size == 1) for (int i = size / 2 - 1; i >= 0; i--)
{ {
printf("Single element in the heap"); heapify(array, size, i);
} }
else }
{ }
int largest = i; void deleteRoot(int array[], int num)
int l = 2 * i + 1; {
int r = 2 * i + 2; int i;
if (l < size && array[l] > array[largest]) for (i = 0; i < size; i++)
largest = l; {
if (r < size && array[r] > array[largest]) if (num == array[i])
largest = r; break;
if (largest != i) }
{ swap(&array[i], &array[size - 1]);
swap(&array[i], &array[largest]); size -= 1;
heapify(array, size, largest); for (int i = size / 2 - 1; i >= 0; i--)
} {
} heapify(array, size, i);
} }
}
void printArray(int array[], int size)
{
for (int i = 0; i < size; ++i)
printf("%d ", array[i]);
printf("\n");
}
int main()
{
int array[10];

insert(array, 3);
insert(array, 4);
insert(array, 9);
insert(array, 5);
insert(array, 2);

printf("Max-Heap array: ");


printArray(array, size);

deleteRoot(array, 4);

printf("After deleting an element: ");

printArray(array, size);
}
Heap Sort
Complexity of Heap Sort
Average and Worst case Complexity of Heap sort = O(nlogn).
Questions

You might also like