Question

In: Computer Science

Write a C++ program using produces Huffman code for a string of text entered by the...

Write a C++ program using produces Huffman code for a string of text entered by the user. The string given by the user can be either 1 word or 1000 words.

Must accept all ASCII characters.


Please do not copy from the internet. This is my 3rd time posting the same question and I have not received a correct answer.

Solutions

Expert Solution

Using below code ,we can enter single or multiple words also it accept ASCII characters.

#include <iostream>
#include <string>
#include <queue>
#include <unordered_map>
using namespace std;

struct Node
{
   char ch;
   int freq;
   Node *left, *right;
};
Node* getNode(char ch, int freq, Node* left, Node* right) //alocating new tree node
{
   Node* node = new Node();

   node->ch = ch;
   node->freq = freq;
   node->left = left;
   node->right = right;

   return node;
}

// Comparison object to be using
struct comp
{
   bool operator()(Node* l, Node* r)
   {
       return l->freq > r->freq;
   }
};

// traverse the Huffman Tree
void encode(Node* root, string str,
           unordered_map<char, string> &huffmanCode)
{
   if (root == nullptr)
       return;

  
   if (!root->left && !root->right) {
       huffmanCode[root->ch] = str;
   }

   encode(root->left, str + "0", huffmanCode);
   encode(root->right, str + "1", huffmanCode);
}

// decode the encoded string
void decode(Node* root, int &index, string str)
{
   if (root == nullptr) {
       return;
   }

   // found a leaf node
   if (!root->left && !root->right)
   {
       cout << root->ch;
       return;
   }

   index++;

   if (str[index] =='0')
       decode(root->left, index, str);
   else
       decode(root->right, index, str);
}

// decode given input text
void buildHuffmanTree(string text)
{
  
   unordered_map<char, int> freq;
   for (char ch: text) {
       freq[ch]++;
   }

   // priority queue created
   priority_queue<Node*, vector<Node*>, comp> pq;

  
   for (auto pair: freq) {
       pq.push(getNode(pair.first, pair.second, nullptr, nullptr));
   }
   while (pq.size() != 1)
   {
       // Remove the two nodes of highest priority
      
       Node *left = pq.top(); pq.pop();
       Node *right = pq.top();   pq.pop();

      
       //Add the new node
       int sum = left->freq + right->freq;
       pq.push(getNode('\0', sum, left, right));
   }

  
   Node* root = pq.top();

  
  
   unordered_map<char, string> huffmanCode; // traverse the Tree
   encode(root, "", huffmanCode);

   cout << "Huffman Codes are :\n" << '\n';
   for (auto pair: huffmanCode) {
       cout << pair.first << " " << pair.second << '\n';
   }

   cout << "\nOriginal string was :\n" << text << '\n';


   string str = "";
   for (char ch: text) {
       str += huffmanCode[ch];
   }

   cout << "\nEncoded string is :\n" << str << '\n';

   // traverse the Huffman Tree

   int index = -1;
   cout << "\nDecoded string is: \n";
   while (index < (int)str.size() - 2) {
       decode(root, index, str);
   }
}

int main()
{
   string text = "a @ null hi wonderfull hello b ";// Huffman coding algorithm

   buildHuffmanTree(text);

"Please refer to the screenshot of the code to understand the indentation of the code".

PLEASE LIKE...THANK YOU!


Related Solutions

Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. Must accept all ASCII characters.
Write a c or matlab text code(to be copied ) for Huffman coder and Huffman decoder...
Write a c or matlab text code(to be copied ) for Huffman coder and Huffman decoder that asks the user to enter the string and output the Huffman code for every letter and a code for encoding that will have every letter and its Huffman code and output all the possibilities for the real string. you must show a screen of an input and the output for both the encoder and the decoder
Create a C++ program using native C++ (STL is acceptable) that produces Huffman code for a...
Create a C++ program using native C++ (STL is acceptable) that produces Huffman code for a string of text entered by the user. Must accept all ASCII characters. Provide an explanation for how you got frequencies of characters, how you sorted them, and how you got codes.
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an...
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an error message if the input cannot be converted I can give an thumbs up! :)
How would I write a program for C++ that checks if an entered string is an...
How would I write a program for C++ that checks if an entered string is an accepted polynomial and if it is, outputs its big-Oh notation? Accepted polynomials can have 3 terms minimum and no decimals in the exponents.
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
Write a C program that can search for a string within a text file, replace it...
Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT