Question

Write a program that takes a string of characters (including spaces) as input, computes the frequency...

Write a program that takes a string of characters (including spaces) as input, computes the frequency of each character, sorts them by frequency, and outputs the Huffman code for each character.   When you are writing your program, you should first test it on a string of 7 characters, so you can check it. PLEASE NOTE: Your program must work for any text that has upper and lower case letters digits 0 - 9, commas, periods, and spaces. Please submit the source code (cpp) file in standard C++ as one file. Then upload a screenshot of the output as another file.Also submit the cpp source code as trxt attachment to email.
Make sure your program works. Note the deadline. It will be posted an additional 5 days, but points deducted for every day after deadline.   Please manually calcu;ate runtime. Write the function and the order. Please also calculate how many bits were saved by Huffman coding.

Homework Answers

Answer #1

Here i am providing the answer. Hope it helps. please give me a like. it helps me a lot.

OUTPUT--

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define max_height_of_tree 400
struct Node
{
char val;
int freq;
struct Node *left;
struct Node *right;
};
struct MinHeap
{
int heapArraySize;
int size;

struct Node** minHeapArray;
};
void exchange(struct Node** a,struct Node** b)
{
struct Node* t ;
t= *a;
*a = *b;
*b = t;
}
struct Node* getnode(char val, int freq)
{
struct Node* newNode= (struct Node*)malloc(sizeof(struct Node));


newNode->freq = freq;
newNode->val = val;
newNode->left =NULL;
newNode->right=NULL;
return newNode;
}

void minHeapify(struct MinHeap* minHeap, int index)

{

int least,left,right;
least = index;
left = 2 * index + 1;
right = 2 * index + 2;

if (left < minHeap->size && minHeap->minHeapArray[left]->freq < minHeap->minHeapArray[least]->freq)
{
least = left;
}


if (right < minHeap->size && minHeap->minHeapArray[right]->freq < minHeap->minHeapArray[least]->freq)

{
least = right;
}

if (least != index)
{

exchange(&minHeap->minHeapArray[least],&minHeap->minHeapArray[index]);
minHeapify(minHeap, least);
}
}

struct MinHeap* createMinHeap(int heapArraySize)

{

struct MinHeap* minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap));
minHeap->size = 0;

minHeap->heapArraySize = heapArraySize;

minHeap->minHeapArray
= (struct Node**)malloc(minHeap->
heapArraySize * sizeof(struct Node*));
return minHeap;
}

struct Node* extractMin(struct MinHeap* minHeap)

{

struct Node* temp ;
temp= minHeap->minHeapArray[0];
minHeap->minHeapArray[0]= minHeap->minHeapArray[minHeap->size - 1];

--minHeap->size;
minHeapify(minHeap, 0);

return temp;
}
int isSizeOne(struct MinHeap* minHeap)
{

return (minHeap->size == 1);
}
void insert(struct MinHeap* minHeap,
struct Node* Node)

{

++minHeap->size;
int i = minHeap->size - 1;

while (i && Node->freq < minHeap->minHeapArray[(i - 1) / 2]->freq) {

minHeap->minHeapArray[i] = minHeap->minHeapArray[(i - 1) / 2];
i = (i - 1) / 2;
}

minHeap->minHeapArray[i] = Node;
}
void buildMinHeap(struct MinHeap* minHeap)

{

int n = minHeap->size - 1;
int i;

for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
void printArr(int arr[], int n)
{
int i;
for (i = 0; i < n; ++i)
{
cout<< arr[i];
}
cout<<"\n";
}
int lastNode(struct Node* node)

{
return ((!(node->left) )&&( !(node->right)));
}
struct MinHeap* cBuildheap(char val[], int freq[], int size)

{

struct MinHeap* minHeap = createMinHeap(size);

for (int i = 0; i < size; ++i)
minHeap->minHeapArray[i] = getnode(val[i], freq[i]);

minHeap->size = size;
buildMinHeap(minHeap);

return minHeap;
}

struct Node* buildTree(char val[], int freq[], int size)

{
struct Node *left, *right, *top;
struct MinHeap* minHeap = cBuildheap(val, freq, size);
while (!isSizeOne(minHeap)) {

left = extractMin(minHeap);
right = extractMin(minHeap);
top = getnode('$', left->freq + right->freq);

top->left = left;
top->right = right;

insert(minHeap, top);
}

return extractMin(minHeap);
}
void printOutputCode(struct Node* node, int arr[], int top)

{
if (node->left) {

arr[top] = 0;
printOutputCode(node->left, arr, top + 1);
}
if (node->right) {

arr[top] = 1;
printOutputCode(node->right, arr, top + 1);
}

if (lastNode(node)) {

cout<<"code for "<< node->val <<" => ";
printArr(arr, top);
}
}
void code(char val[], int freq[], int size)

{
struct Node* node
= buildTree(val, freq, size);

int arr[max_height_of_tree], top = 0;

printOutputCode(node, arr, top);
}

int main()
{
int i,j;
string str;
cout<<"Enter the string of characters"<<endl;
getline(cin,str);
int a[256]={0};
for(i=0;i<str.size();i++)
{
a[str[i]]++;
}
int n=0;
for(i=0;i<256;i++)
{

if(a[i]>0)
{
n=n+1;
}
}
j=0;
char arr[n];
int frequency[n];
for(i=0;i<256;i++)
{
if(a[i]>0)
{

arr[j]=char(i);
frequency[j]=a[i];
j++;
}
}
cout<<"characters and frequencies"<<endl;
for(i=0;i<n;i++)
{
cout<<arr[i]<<" "<<frequency[i]<<endl;
}
cout<<"charactes and huffman code"<<endl;
code(arr, frequency, n);
}

Thank you. please upvote.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Python function that computes the frequency of characters in a string s. The function...
Write a Python function that computes the frequency of characters in a string s. The function should return a map where the key is the character and the value is the count. Make sure you include a main function and include comments to document your code.
Write an assembly program that reads characters from standard input until the “end of file” is...
Write an assembly program that reads characters from standard input until the “end of file” is reached. The input provided to the program contains A, C, T, and G characters. The file also may have new line characters (ASCII code 10 decimal), which should be skipped/ignored. The program then must print the count for each character. You can assume (in this whole assignment) that the input doe not contain any other kinds of characters.  the X86 assembly program that simply counts...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex:...
Write a program that accepts an input string from the user and converts it into an...
Write a program that accepts an input string from the user and converts it into an array of words using an array of pointers. Each pointer in the array should point to the location of the first letter of each word. Implement this conversion in a function str_to_word which returns an integer reflecting the number of words in the original string. To help isolate each word in the sentence, convert the spaces to NULL characters. You can assume the input...
(C++) Write a program whose input is two characters and a string, and whose output indicates...
(C++) Write a program whose input is two characters and a string, and whose output indicates the number of times each character appears in the string. Ex: If the input is: n M Monday the output is: 1 1 Ex: If the input is: z y Today is Monday the output is: 0 2 Ex: If the input is: n y It's a sunny day the output is: 2 2 Case matters. Ex: If the input is: n N Nobody...
Write a function that takes as input an English sentence (a string) and prints the total...
Write a function that takes as input an English sentence (a string) and prints the total number of vowels and the total number of consonants in the sentence. The function returns nothing. Note that the sentence could have special characters such as dots, dashes, and so on. write a python program please.
Write a C Language program called iam.c to: Have a string variable called myName initialized to...
Write a C Language program called iam.c to: Have a string variable called myName initialized to your-name-here.  Print on one line a literal string "Hello " concatenated to myName. In Linux Terminal compile your iam.c source code program to produce a .exe executable. Run your iam.exe and take a print screen image. Upload your iam.exe file here.
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
Write a program (O(n), where n is the number of words) that takes as input a...
Write a program (O(n), where n is the number of words) that takes as input a set of words and returns groups of anagrams for those words. Complete your code here - For example, if the list is "debitcard", "elvis", "silent", "badcredit", "lives", "freedom", "listen", "levis", the output should be silent listen debitcard badcredit elvis lives levis Note that freedom has no anagram, and hence is not printed. ///////////////////////////////////////////////$%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% anagram.cpp #include <iostream> #include <unordered_map> #include <vector> #include <algorithm> using namespace...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT