4.3 Huffman Algorithm
4.3 Huffman Algorithm
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char data;
unsigned frequency;
};
newNode->data = data;
newNode->frequency = frequency;
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) {
if (root->left) {
arr[top] = 0;
if (root->right) {
arr[top] = 1;
printf("%d", arr[i]);
printf("\n");
// Iterate until there is only one node in the array (the root of the Huffman tree)
nodeArray[j + 1] = temp;
// Create a new internal node with the two smallest frequencies as children
left = nodeArray[0];
right = nodeArray[1];
top->left = left;
top->right = right;
// Remove the two smallest nodes from the array and add the new internal node
nodeArray[0] = top;
size--;
return nodeArray[0];
int arr[size];
int top = 0;
int main() {
printf("Huffman Codes:\n");
return 0;
Output:
Huffman Codes:
a: 1100
b: 1101
c: 100
d: 101
e: 111
f: 0