I need more comments to be able to fully understand what is going on in the code. It's Huffman code. Also would you be a

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

I need more comments to be able to fully understand what is going on in the code. It's Huffman code. Also would you be a

Post by answerhappygod »

I need more comments to be able to fully understand what is
going on in the code. It's Huffman code. Also would you be able to
give a brief explanation on how it works after adding more comments
where it are needed?
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct nodes{
char node; //stores character
int frequency; //frequency of the character
nodes* left; //left child of current node
nodes* right; //right child
public:
nodes(){
node = ' ';
frequency = 0;
}
//initialize the current node
nodes(char name, int frequency){
this->node =
name;
this->frequency =
frequency;
}
};
//function to print huffman code for each character
void print(nodes* temp, string s, char chars[], string
key[]){
if(temp == NULL){
return;
}
else if(temp->node == '\n'){
print(temp->left, s + "0", chars,
key); // Assign 0 to the left node and recur
print(temp->right, s + "1",
chars, key); // Assign 1 to the left node and recur
}
else{
// If this is a leaf node,
//then we print root->data
// We also print the code
for(int i = 0; i < 6; i++){
if (temp->node ==
chars) {
key =
s;
}
}
}
}
//structure to compare
struct compare{
bool operator()(nodes* left, nodes* right) {
return (left->frequency >
right->frequency); // Defining priority on the basis of
frequency
}
};
void Huffman(int freq[], char chars[], string key[]) {
nodes* x;
nodes* y;
nodes* z;
// Declaring priority queue
// using custom comparator
priority_queue <nodes*, vector <nodes*>,
compare> queue;
// Populating the priority queue
for(int i = 0; i < 6; i++){
nodes* temp = new nodes(chars,
freq);
queue.push(temp);
}
//keep on looping till only one node remains in
// the Priority Queue
while(queue.size() > 1){
x = queue.top(); // Node which has
least frequency
queue.pop();//pop it from the
queue
y = queue.top();// Node which has
least frequency
queue.pop(); //pop it from the
queue
z = new nodes('\n', x->frequency
+ y->frequency); // A new node is formed
// with frequency left->freq + right->freq
z->left = x;
z->right = y;
queue.push(z);// Push back node
//created to the Priority Queue
}
string s = "";
print(queue.top(), s, chars, key);
}
// driver functikon
int main(){
int freq[6];
string key [6];
char chars[6] = {'A',
'B',
'C',
'D',
'E',
'F'};
for(int i = 0; i < 6; i++){
cin >> freq;
}
Huffman(freq, chars, key);
for(int i = 0; i < 6; i++){
cout << chars << ":"
<< key << endl;
}
return 0;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply