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
Posted: Thu May 05, 2022 12:41 pm
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;
}
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;
}