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

4.3 Huffman Algorithm

Uploaded by

Pvr Jr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

4.3 Huffman Algorithm

Uploaded by

Pvr Jr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Algorithm:

Step 1 : Define a Node Structure (struct Node):


 Create a structure to represent nodes in the Huffman tree.
 Each node contains character data, frequency, and pointers to
its left and right children.
Step 2 : Create Node Function (createNode):
 Take character data and frequency as input.
 Allocate memory for a new node, initialize its fields, and return
the created node.
Step 3 : Print Codes Function (printCodes):
 Recursively traverse the Huffman tree from the root to a leaf.
 During traversal, assign 0 to the left edge and 1 to the right
edge.
 Print the Huffman code for a leaf node.
Step 4 : Build Huffman Tree Function (buildHuffmanTree):
 Take arrays of characters ( data[]) and their frequencies
(frequency[]) along with the size of the arrays.
 Create nodes for each character-frequency pair and store
them in an array.
 Iterate until there is only one node in the array (the root of the
Huffman tree):
 Sort the array of nodes based on frequencies.
 Create a new internal node with the two smallest
frequencies as children.
 Remove the two smallest nodes from the array and add
the new internal node.
 Repeat the process until only one node remains in the
array.
Step 5 : Huffman Codes Function (huffmanCodes):
 Build the Huffman tree using the buildHuffmanTree function.
 Initialize an array to store Huffman codes ( arr[]) and a
variable to track the top of the array.
 For each character, print its Huffman code using the
printCodes function.
Step 6 : Main Function (main):
 Declare arrays of characters ( data[]) and their frequencies
(frequency[]) for the example.
 Call the huffmanCodes function to demonstrate the Huffman
coding algorithm.
 Print the generated Huffman codes.
Code:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// Define a structure for a Huffman tree node

struct Node {

char data;

unsigned frequency;

struct Node *left, *right;

};

// Function to create a new node

struct Node* createNode(char data, unsigned frequency) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->frequency = frequency;

newNode->left = newNode->right = NULL;

return newNode;

// Function to print the Huffman codes from the root of the tree

void printCodes(struct Node* root, int arr[], int top, char data) {

// Assign 0 to left edge and recur

if (root->left) {

arr[top] = 0;

printCodes(root->left, arr, top + 1, data);

// Assign 1 to right edge and recur

if (root->right) {
arr[top] = 1;

printCodes(root->right, arr, top + 1, data);

// If this is a leaf node, print the code

if (!(root->left) && !(root->right) && root->data == data) {

printf("%c: ", data);

for (int i = 0; i < top; i++) {

printf("%d", arr[i]);

printf("\n");

// Function to build the Huffman tree

struct Node* buildHuffmanTree(char data[], unsigned frequency[], int size) {

struct Node *left, *right, *top;

// Create an array of nodes

struct Node* nodeArray[size];

// Initialize each node with its data and frequency

for (int i = 0; i < size; i++) {

nodeArray[i] = createNode(data[i], frequency[i]);

// Iterate until there is only one node in the array (the root of the Huffman tree)

while (size > 1) {

// Sort the nodes based on frequency

for (int i = 0; i < size - 1; i++) {

for (int j = 0; j < size - i - 1; j++) {


if (nodeArray[j]->frequency > nodeArray[j + 1]->frequency) {

struct Node* temp = nodeArray[j];

nodeArray[j] = nodeArray[j + 1];

nodeArray[j + 1] = temp;

// Create a new internal node with the two smallest frequencies as children

left = nodeArray[0];

right = nodeArray[1];

top = createNode('$', left->frequency + right->frequency);

top->left = left;

top->right = right;

// Remove the two smallest nodes from the array and add the new internal node

nodeArray[0] = top;

for (int i = 1; i < size - 1; i++) {

nodeArray[i] = nodeArray[i + 1];

size--;

// The remaining node is the root of the Huffman tree

return nodeArray[0];

// Function to perform Huffman coding

void huffmanCodes(char data[], unsigned frequency[], int size) {

// Build the Huffman tree

struct Node* root = buildHuffmanTree(data, frequency, size);


// Create an array to store Huffman codes

int arr[size];

int top = 0;

// Print Huffman codes for each character

for (int i = 0; i < size; i++) {

printCodes(root, arr, top, data[i]);

int main() {

char data[] = {'a', 'b', 'c', 'd', 'e', 'f'};

unsigned frequency[] = {5, 9, 12, 13, 16, 45};

int size = sizeof(data) / sizeof(data[0]);

printf("Huffman Codes:\n");

huffmanCodes(data, frequency, size);

return 0;

Output:

Huffman Codes:

a: 1100

b: 1101

c: 100

d: 101
e: 111

f: 0

Process returned 0 (0x0) execution time : 0.969 s

You might also like