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 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:...
(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...
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...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
1. you will just write your name in bytes - once in UTF8 bytes, once in...
1. you will just write your name in bytes - once in UTF8 bytes, once in UTF16 bytes. Name :Andrew Submit: 1. Your .java file 2. A screenshot showing your code ran and gave the right output. ---- /** * In this program you will write your first name in bytes in 2 different encodings. * Then convert the byte array to a String and print it out. * * TODO in lecture: show students how I google to find...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...