Question

In: Computer Science

I want the code below to be edited: Rather than giving the input string inside the...

I want the code below to be edited:

Rather than giving the input string inside the code, I want the program to ask the user for an input and calculate and complete this code.
I have pasted the actual code below, Please edit the input section only so that I can input any string or any sentence as I like. The program must ask the user that "Enter a string/sentence" and take the data to calculate the Huffman code.

#include <bits/stdc++.h> 
#define MAX_TREE_HT 256 
using namespace std; 

map<char, string> codes; 

map<char, int> freq; 

struct MinHeapNode 
{ 
    char data;          
    int freq;           
    MinHeapNode *left, *right; 
  
    MinHeapNode(char data, int freq) 
    { 
        left = right = NULL; 
        this->data = data; 
        this->freq = freq; 
    } 
}; 

struct compare 
{ 
    bool operator()(MinHeapNode* l, MinHeapNode* r) 
    { 
        return (l->freq > r->freq); 
    } 
}; 

void printCodes(struct MinHeapNode* root, string str) 
{ 
    if (!root) 
        return; 
    if (root->data != '$') 
        cout << root->data << ": " << str << "\n"; 
    printCodes(root->left, str + "0"); 
    printCodes(root->right, str + "1"); 
} 

void storeCodes(struct MinHeapNode* root, string str) 
{ 
    if (root==NULL) 
        return; 
    if (root->data != '$') 
        codes[root->data]=str; 
    storeCodes(root->left, str + "0"); 
    storeCodes(root->right, str + "1"); 
} 
  
priority_queue<MinHeapNode*, vector<MinHeapNode*>, compare> minHeap; 

void HuffmanCodes(int size) 
{ 
    struct MinHeapNode *left, *right, *top; 
    for (map<char, int>::iterator v=freq.begin(); v!=freq.end(); v++) 
        minHeap.push(new MinHeapNode(v->first, v->second)); 
    while (minHeap.size() != 1) 
    { 
        left = minHeap.top(); 
        minHeap.pop(); 
        right = minHeap.top(); 
        minHeap.pop(); 
        top = new MinHeapNode('$', left->freq + right->freq); 
        top->left = left; 
        top->right = right; 
        minHeap.push(top); 
    } 
    storeCodes(minHeap.top(), ""); 
} 

void calcFreq(string str, int n) 
{ 
    for (int i=0; i<str.size(); i++) 
        freq[str[i]]++; 
} 

string decode_file(struct MinHeapNode* root, string s) 
{ 
    string ans = ""; 
    struct MinHeapNode* curr = root; 
    for (int i=0;i<s.size();i++) 
    { 
        if (s[i] == '0') 
           curr = curr->left; 
        else
           curr = curr->right; 
  
        if (curr->left==NULL and curr->right==NULL) 
        { 
            ans += curr->data; 
            curr = root; 
        } 
    } 
    return ans+'\0'; 
} 
  
int main() 
{ 
    string str = "Please remove this input and help me put my own input into the program"; 
    string encodedString, decodedString; 
    calcFreq(str, str.length()); 
    HuffmanCodes(str.length()); 

    cout << "Character With there Frequencies:\n"; 
    for (auto v=codes.begin(); v!=codes.end(); v++) 
        cout << v->first <<' ' << v->second << endl; 
  
    for (auto i: str) 
        encodedString+=codes[i]; 
  
    cout << "\nEncoded Huffman data:\n" << encodedString << endl; 
  
    decodedString = decode_file(minHeap.top(), encodedString); 
    cout << "\nDecoded Huffman Data:\n" << decodedString << endl; 
    return 0; 
}

Solutions

Expert Solution

#include <bits/stdc++.h> 
#define MAX_TREE_HT 256 
using namespace std; 

map<char, string> codes; 

map<char, int> freq; 

struct MinHeapNode 
{ 
    char data;          
    int freq;           
    MinHeapNode *left, *right; 
  
    MinHeapNode(char data, int freq) 
    { 
        left = right = NULL; 
        this->data = data; 
        this->freq = freq; 
    } 
}; 

struct compare 
{ 
    bool operator()(MinHeapNode* l, MinHeapNode* r) 
    { 
        return (l->freq > r->freq); 
    } 
}; 

void printCodes(struct MinHeapNode* root, string str) 
{ 
    if (!root) 
        return; 
    if (root->data != '$') 
        cout << root->data << ": " << str << "\n"; 
    printCodes(root->left, str + "0"); 
    printCodes(root->right, str + "1"); 
} 

void storeCodes(struct MinHeapNode* root, string str) 
{ 
    if (root==NULL) 
        return; 
    if (root->data != '$') 
        codes[root->data]=str; 
    storeCodes(root->left, str + "0"); 
    storeCodes(root->right, str + "1"); 
} 
  
priority_queue<MinHeapNode*, vector<MinHeapNode*>, compare> minHeap; 

void HuffmanCodes(int size) 
{ 
    struct MinHeapNode *left, *right, *top; 
    for (map<char, int>::iterator v=freq.begin(); v!=freq.end(); v++) 
        minHeap.push(new MinHeapNode(v->first, v->second)); 
    while (minHeap.size() != 1) 
    { 
        left = minHeap.top(); 
        minHeap.pop(); 
        right = minHeap.top(); 
        minHeap.pop(); 
        top = new MinHeapNode('$', left->freq + right->freq); 
        top->left = left; 
        top->right = right; 
        minHeap.push(top); 
    } 
    storeCodes(minHeap.top(), ""); 
} 

void calcFreq(string str, int n) 
{ 
    for (int i=0; i<str.size(); i++) 
        freq[str[i]]++; 
} 

string decode_file(struct MinHeapNode* root, string s) 
{ 
    string ans = ""; 
    struct MinHeapNode* curr = root; 
    for (int i=0;i<s.size();i++) 
    { 
        if (s[i] == '0') 
           curr = curr->left; 
        else
           curr = curr->right; 
  
        if (curr->left==NULL and curr->right==NULL) 
        { 
            ans += curr->data; 
            curr = root; 
        } 
    } 
    return ans+'\0'; 
} 
  
int main() 
{ 
    string str; 
    cout<<"Enter a string/sentence"<<endl;
    getline(cin,str);
    
    string encodedString, decodedString; 
    calcFreq(str, str.length()); 
    HuffmanCodes(str.length()); 

    cout << "Character With there Frequencies:\n"; 
    for (auto v=codes.begin(); v!=codes.end(); v++) 
        cout << v->first <<' ' << v->second << endl; 
  
    for (auto i: str) 
        encodedString+=codes[i]; 
  
    cout << "\nEncoded Huffman data:\n" << encodedString << endl; 
  
    decodedString = decode_file(minHeap.top(), encodedString); 
    cout << "\nDecoded Huffman Data:\n" << decodedString << endl; 
    return 0; 
}

I have edited the code as required. Now it asks user for the input.


Related Solutions

I want this code to be written in c++. Take a string of length n as...
I want this code to be written in c++. Take a string of length n as an input from the user such that n>=8 and only lower-case letters (a-z) are allowed as valid string characters. Deleting a letter from the string removes all the occurrences of that letter. The objective is to find the longest possible string such that it is left with only two unique letters and no two consecutive characters in a string are the same. If there...
Java question - How can the code below be modified to accept multi digit input. String...
Java question - How can the code below be modified to accept multi digit input. String e = "1+9+8"; int r = e.charAt(0)-'0'; for (int i = 1; i < e.length(); i+=2){    if (e.charAt(i) == '+'){ r += e.charAt(i+1)-'0'; } else{ r -= e.charAt(i+1)-'0'; } The only built in String methods that can be used are lowercase(), length(), and charAt(). Arrays and parseInt() cannot be used. So we want to know how we can get an answer if a...
The code below is giving an arithmetic overflow. Correct the below given the below code, so...
The code below is giving an arithmetic overflow. Correct the below given the below code, so that the value of s0 is printed before calling the function fun1, inside the function Fun1 and after returning from Fun1 in main. Upload the corrected .asm or .s file in the drop box. .text main: addi $s0,$zero,2 jal Disp jal Fun1 jal Disp j Exit Fun1: addi $sp,$sp,-4 sw $s0,0($sp) addi $s0,$s0,15 jal Disp lw $s0,0($sp) addi $sp,$sp,4 jr $ra Disp: li $v0,1...
using this code under, I want when the user input only letter q an error message...
using this code under, I want when the user input only letter q an error message appears that there is no second smallest, how I can do that? #include <iostream> #include <sstream> #include <vector> #include <algorithm> using namespace std; int secondSmallestNumber(vector<int> &I); int main() {    cout << "Enter the numbers in random order: (close by entering q)" << endl;    vector<int> I;    int input;    stringstream ss;    string str;    bool flag = false;    while (!flag) {        getline(cin, str);        ss.clear();        ss << str;...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
What is the advantage of defining the member functions outside a class rather than defining inside...
What is the advantage of defining the member functions outside a class rather than defining inside a class? How the following are achieved in C++? a) static polymorphism. b) dynamic polymorphism What will happen if you forget to implement the pure virtual function in the derived class? How a function templates differs from function overloading?
c++ I want to flip the string when it's string type. For example, if it is...
c++ I want to flip the string when it's string type. For example, if it is 'apple', I would like to print 'elppa'. how can i do?
What are the potential problems for a company in having an input supplied externally rather than...
What are the potential problems for a company in having an input supplied externally rather than in house?
this is my code I want the opposite i want to convert a postfix expression to...
this is my code I want the opposite i want to convert a postfix expression to infix expression #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c; } char pop(Stack *st) {   char c = st->s[st->top];   st->top--;   //(A+B)*(C+D)   return c; } /* function to check whether a character is an operator or not. this function...
#Java I am reading from the txt file and I put everytihng inside of the String[],...
#Java I am reading from the txt file and I put everytihng inside of the String[], like that: String[] values = str.split(", "); But, now I have to retrieve the data from it one by one. How can I do it? Here is the txt file: OneTime, finish the project, 2020-10-15 Monthly, wash the car, 2020-11-10 Thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT