Project Report
Project Report
Compression and
Decompression
1. Introduction
2. Objective
Programming Language: C
Key Features:
Huffman Tree Construction: Built
using a min-heap to merge nodes with
the lowest frequencies.
P 5. Code Explanation
6. Results
Example output:
7. Conclusion
#include <stdlib.h>
#include <string.h>
char data;
unsigned freq;
} MinHeapNode;
unsigned size;
unsigned capacity;
MinHeapNode** array;
} MinHeap;
char data;
char code[MAX_TREE_HT];
} HuffmanCode;
temp->data = data;
temp->freq = freq;
return temp;
minHeap->size = 0;
minHeap->capacity = capacity;
return minHeap;
MinHeapNode* t = *a;
*a = *b;
*b = t;
// Heapify function
smallest = left;
smallest = right;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
++minHeap->size;
int i = minHeap->size - 1;
i = (i - 1) / 2;
minHeap->array[i] = minHeapNode;
while (minHeap->size != 1) {
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 storeCodes(MinHeapNode* root, char* code, int top, HuffmanCode codes[], int* index) {
if (root->left) {
code[top] = '0';
if (root->right) {
code[top] = '1';
code[top] = '\0';
strcpy(codes[*index].code, code);
codes[*index].data = root->data;
(*index)++;
// Encode File
return;
if (!inputFile || !outputFile) {
return;
char ch;
freq[(unsigned char)ch]++;
rewind(inputFile);
char data[256];
if (freq[i] > 0) {
data[size] = (char)i;
freqs[size] = freq[i];
size++;
HuffmanCode codes[256];
char code[MAX_TREE_HT];
int index = 0;
int bitCount = 0;
if (codes[i].data == ch) {
bitCount++;
if (bitCount == 8) {
buffer = 0;
bitCount = 0;
break;
if (bitCount > 0) {
int originalBits = 0;
int compressedBits = 0;
if (codes[i].data == ch) {
break;
if (codes[i].data == ch) {
break;
fclose(inputFile);
fclose(outputFile);
}
// Decode File
if (!inputFile) {
return;
if (!inputFile || !outputFile) {
return;
int freq[256];
char data[256];
if (freq[i] > 0) {
data[size] = (char)i;
freqs[size] = freq[i];
size++;
fputc(current->data, outputFile);
current = root;
fclose(inputFile);
fclose(outputFile);
int main() {
int choice;
while(1){
printf("3. Exit\n");
printf("******************************************************\n");
scanf("%d", &choice);
if (choice == 1) {
scanf("%s", inputFile);
scanf("%s", compressedFile);
encodeFile(inputFile, compressedFile);
else if (choice == 2) {
scanf("%s", compressedFile);
scanf("%s", outputFile);
decodeFile(compressedFile, outputFile);
printf("Exiting...\n");
return 0;
else {
printf("Invalid choice!\n");
return 0;
}
RESULT OF COMPRESSION