Professional Documents
Culture Documents
Heap - Core Man
Heap - Core Man
Heap - Core Man
Data Structures
Lecture : Heap
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.”
insert(array, 3);
insert(array, 4);
insert(array, 9);
insert(array, 5);
insert(array, 2);
deleteRoot(array, 4);
printArray(array, size);
}
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
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.
// Heap Sort in C // Main function to do heap sort
void heapSort(int arr[], int n) {
#include <stdio.h> // Build max heap
for (int i = n / 2 - 1; i >= 0; i--)
// Function to swap the the position of two elements heapify(arr, n, i);
void swap(int *a, int *b) {
int temp = *a; // Heap sort
*a = *b; for (int i = n - 1; i >= 0; i--) {
*b = temp; swap(&arr[0], &arr[i]);
}
// Heapify root element to get highest element at
void heapify(int arr[], int n, int i) { root again
// Find largest among root, left child and right child heapify(arr, i, 0);
int largest = i; }
int left = 2 * i + 1; }
int right = 2 * i + 2; // Print an array
void printArray(int arr[], int n) {
if (left < n && arr[left] > arr[largest]) for (int i = 0; i < n; ++i)
largest = left; printf("%d ", arr[i]);
printf("\n");
if (right < n && arr[right] > arr[largest]) }
largest = right; // Driver code
int main() {
// Swap and continue heapifying if root is not largest int arr[] = {1, 12, 9, 5, 6, 10};
if (largest != i) { int n = sizeof(arr) / sizeof(arr[0]);
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest); heapSort(arr, n);
}
} printf("Sorted array is \n");
printArray(arr, n);
}
Complexity of Heap Sort
Average and Worst case Complexity of Heap sort = O(nlogn).
Huffman coding
Count the
Frequency /
repetition of each
character
Arrange in Ascending
Order according to
Frequency
Huffman coding
Add first two Frequency and make a root node with added frequency and leaf
nodes with individual frequencies and insert the summed frequency back to
the array
Huffman coding
Add first two Frequency and make a root node with added frequency and leaf
nodes with individual frequencies and insert the summed frequency back to
the array
Huffman coding
Mark the left side edge as 0 and right side as 1 , for Each non-leaf node and
Find the code for each node by considering the path of the node from root.
Huffman coding
return temp;
}
// Insertion function
// Heapify void insertMinHeap(struct MinHeap *minHeap, struct MinHNode
void minHeapify(struct MinHeap *minHeap, int idx) { *minHeapNode) {
int smallest = idx; ++minHeap->size;
int left = 2 * idx + 1; int i = minHeap->size - 1;
int right = 2 * idx + 2;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]-
if (left < minHeap->size && minHeap->array[left]->freq < >freq) {
minHeap->array[smallest]->freq) minHeap->array[i] = minHeap->array[(i - 1) / 2];
smallest = left; i = (i - 1) / 2;
}
if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[i] = minHeapNode;
minHeap->array[smallest]->freq) }
smallest = right;
void buildMinHeap(struct MinHeap *minHeap) {
if (smallest != idx) { int n = minHeap->size - 1;
swapMinHNode(&minHeap->array[smallest], &minHeap- int i;
>array[idx]);
minHeapify(minHeap, smallest); for (i = (n - 1) / 2; i >= 0; --i)
} minHeapify(minHeap, i);
} }