Heap - Core Man

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Lovely Professional University, Punjab

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

• 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.
Type of Heap

• Heap (MaxHeap) • Heap (MinHeap)


Heapify
Insertion in Heap
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
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
// 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);
}
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

Final Table of Character coding


// Huffman Coding in C // Create min heap
struct MinHeap *createMinH(unsigned capacity) {
#include <stdio.h> struct MinHeap *minHeap = (struct MinHeap
#include <stdlib.h> *)malloc(sizeof(struct MinHeap));

#define MAX_TREE_HT 50 minHeap->size = 0;

struct MinHNode { minHeap->capacity = capacity;


char item;
unsigned freq; minHeap->array = (struct MinHNode
struct MinHNode *left, *right; **)malloc(minHeap->capacity * sizeof(struct MinHNode
}; *));
struct MinHeap { return minHeap;
unsigned size; }
unsigned capacity;
struct MinHNode **array; // Function to swap
}; void swapMinHNode(struct MinHNode **a, struct
MinHNode **b) {
// Create nodes struct MinHNode *t = *a;
struct MinHNode *newNode(char item, unsigned freq) { *a = *b;
struct MinHNode *temp = (struct MinHNode *b = t;
*)malloc(sizeof(struct MinHNode)); }

temp->left = temp->right = NULL;


temp->item = item;
temp->freq = freq;

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);
} }

// Check if size if 1 int isLeaf(struct MinHNode *root) {


int checkSizeOne(struct MinHeap *minHeap) { return !(root->left) && !(root->right);
return (minHeap->size == 1); }
}
struct MinHeap *createAndBuildMinHeap(char item[], int freq[],
// Extract min int size) {
struct MinHNode *extractMin(struct MinHeap *minHeap) { struct MinHeap *minHeap = createMinH(size);
struct MinHNode *temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1]; for (int i = 0; i < size; ++i)
minHeap->array[i] = newNode(item[i], freq[i]);
--minHeap->size;
minHeapify(minHeap, 0); minHeap->size = size;
buildMinHeap(minHeap);
return temp; return minHeap;
} }
struct MinHNode *buildHuffmanTree(char item[], int freq[], int // Wrapper function
size) { void HuffmanCodes(char item[], int freq[], int size) {
struct MinHNode *left, *right, *top; struct MinHNode *root = buildHuffmanTree(item, freq,
struct MinHeap *minHeap = createAndBuildMinHeap(item, freq, size);
size);
int arr[MAX_TREE_HT], top = 0;
while (!checkSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap); printHCodes(root, arr, top);
}
top = newNode('$', left->freq + right->freq);
// Print the array
top->left = left;
void printArray(int arr[], int n) {
top->right = right;
int i;
insertMinHeap(minHeap, top); for (i = 0; i < n; ++i)
} printf("%d", arr[i]);
return extractMin(minHeap);
} printf("\n");
}
void printHCodes(struct MinHNode *root, int arr[], int top) {
if (root->left) {
arr[top] = 0; int main() {
printHCodes(root->left, arr, top + 1); char arr[] = {'A', 'B', 'C', 'D'};
} int freq[] = {5, 1, 6, 3};
if (root->right) {
arr[top] = 1;
int size = sizeof(arr) / sizeof(arr[0]);
printHCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) { printf(" Char | Huffman code ");
printf(" %c | ", root->item); printf("\n--------------------\n");
printArray(arr, top);
} HuffmanCodes(arr, freq, size);
}
}
Questions

You might also like