I have been assigned as a school project to check the the frequency of occurrence of 26 lowercase letters, and then they are encoded by Hoffman code. In this assignment, the basic requirement is (1)to read the given text file and open it on the terminal and (2)to output the number of occurrence of lowercase letters and their corresponding encoded Hoffman code.
I was able to accomplish the (2) task but whenever I am trying to output the text on the terminal, my code was not able to count the occurrence of the lowercase letters and output their respective Huffman code.
terminal showing only the text content
output without the inclusion of code for reading the text on terminal
Here is my code for the assignment:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <bits/stdc++.h>
int ch;
FILE *file;
using namespace std;
struct Node
{
// data members of each node of Huffman tree - alphabet and its frequency
char data;
unsigned freq;
// pointers to left and right childs
Node *left, *right;
// constructor to initialize the data members of the Huffman tree node
Node(char d, unsigned f)
{
data = d;
freq = f;
left = NULL;
right = NULL;
}
};
struct compare
{
// overloading () to compare which of left,right childs have higher frequency
bool operator()(Node* l, Node* r)
{
return (l->freq > r->freq);
}
};
// recursive function to display huffman codes
void display(struct Node* root, string str)
{
// if tree is empty return
if (!root)
return;
if (root->data != '$')
cout << root->data << ": " << str << "\n";
// recursively call left, right childs
display(root->left, str + "0");
display(root->right, str + "1");
}
// build huffman tree
void createHuffmanTree(char data[], int freq[], int size)
{
struct Node *left, *right, *top;
// Create a min heap using STL
priority_queue<Node*, vector<Node*>, compare> minHeap;
// push alphabets, frequency into minheap as leaf nodes
for (int i = 0; i < size; ++i)
minHeap.push(new Node(data[i], freq[i]));
// repeat until heap size becomes one
while (minHeap.size() != 1)
{
// Extract two least frequent items from minheap
left = minHeap.top();
minHeap.pop();
right = minHeap.top();
minHeap.pop();
// Create new internal node with two least frequent items as child nodes
// frequency of internal node is equal to sum of frequency of child nodes
top = new Node('$', left->freq + right->freq);
top->left = left;
top->right = right;
// push newly created internal node into minheap
minHeap.push(top);
}
// building Huffman tree is done
// display Huffman codes for alphabets
cout << "The Huffman codes for lower case characters: " << endl;
display(minHeap.top(), "");
}
// reading text file
void readTxt() {
FILE * txt;
char ch;
if (txt != NULL)
{
// open file to read english article
txt = fopen("D:\\data3.txt", "r");
do
{
ch = fgetc(txt);
putchar(ch);
} while(ch != EOF);
//fclose(txt);
} else {
cout << "There is no such .txt file in the system." << endl;
exit(0);
}
}
int main()
{
int charCount = 0;
int freq[26] = {0};
readTxt();
// reads english article from file and counts frequency of each lowercase alphabet
while (1)
{
ch = fgetc(file);
if (ch == EOF)
break;
if (ch >= 'a' && ch <= 'z')
{
freq[ch - 'a']++;
// count total number of lowercase alphabets in file
charCount++;
}
}
fclose(file);
// display total number of characters in file
cout << "Number of lowercase alphabets in file: " << charCount << endl;
char arr[] = { 'a','b','c','d','e','f',
'g','h','i','j','k','l',
'm','n','o','p','q','r',
's','t','u','v','w','x',
'y','z' };
// display frequency of lowercase characters
cout << "Frequency of lowercase characters: " << endl;
int freqCount = 0;
for(int i = 0; i < 26; i++)
{
if(freq[i]!=0)
{
cout << arr[i] << " : " << freq[i] << endl;
// count number of lowercase alphabets with frequency greater than zero
freqCount++;
}
}
char arr1[freqCount];
int k = 0, freq1[freqCount];
// copy lowercase alphabets with frequency greater than zero into arr1, freq1
for(int i = 0; i < 26; i++)
{
if(freq[i]!=0)
{
arr1[k] = arr[i];
freq1[k++] = freq[i];
}
}
// call method to create Huffman tree
createHuffmanTree(arr1, freq1, freqCount);
return 1;
}
It will be a great help if you guide me through my mistake.
TIA