61 Practical 06
61 Practical 06
Theory:
The Huffman coding algorithm is a method used for lossless data compression. It assigns variable-length
codes to input characters, with shorter codes assigned to more frequent characters. This allows for more
efficient encoding of the input data. Huffman coding is typically implemented using a greedy approach,
where at each step, the two least frequent symbols are merged into a single node until only one node
remains, which becomes the root of the Huffman tree.
Example :
Consider the following example where we want to encode the string "ABBCCCDDDEEEEE":
Calculate the frequency of each character:
• A: 1 B: 2 C: 3 D: 3 E: 5
{
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}
void minHeapify(struct MinHeap* minHeap, int idx)
{
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]-
>freq)
smallest = left;
{
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
{
struct MinHeapNode *left, *right, *top;
struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size);
while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
void printCodes(struct MinHeapNode* root, int arr[], int top)
{
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
{
struct MinHeapNode* root = buildHuffmanTree(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
}
int main()
{
char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
int freq[] = { 5, 9, 12, 13, 16, 45 };
int size = sizeof(arr) / sizeof(arr[0]);
HuffmanCodes(arr, freq, size);
return 0;
}
OUTPUT:
Time Complexity :
O(nlogn),where n is the number of unique characters.
Time Complexity is O(nlogn) because we are extracting minimum nodes 2*(n-1) times and each time
whenever a node is being extracted from the heap then a function called heapify() is being called to
rearrange the element according to their priority.This function heapify() takes O(logn) time.
Space Complexity: O(N)