Question: Please code in C + + , You will need to use PriorityQueue and Hashing here; so , if you have not read how to
Please code in C
You will need to use PriorityQueue and Hashing here; so if you have not read how to use them,
check out Section and Section
In Java or C# to create a BinaryTreeNode with character c and value v the syntax is: BinaryTreeNode node new BinaryTreeNodec v; In C to create a BinaryTreeNode with character
c and value v the syntax is: BinaryTreeNode node new BinaryTreeNodec v;
Let frequenceM ap class variable contain the mapping of each character in the alphabet
to its frequency number of occurrences in the message Implement the encode, buildTree, and
createTable methods using the pseudocodes.
PsuedoCode for Encode function:
root buildTree
createTableroot;
Iterate over the frequencymap and do the following:
let c be the current character key in the frequencymap
let f be the current frequency value in the frequencymap
let code getEncodingc be the codeword for the character
encodingLength encodingLength f length of code
tableSize tableSize length of code
Psuedocode for BuildTree function:
Create a BinaryTreeNode minimum priority queue P Q
Iterate over the frequencymap and do the following:
let c be the current character key in the frequencymap
let f be the current frequency value in the frequencymap
create a binary tree node z for the character c and value f
add z to PQ
While P Q has more than than node, do the following:
get the minimum binary tree node min from PQ and remove it
get the second minimum binary tree node secondM in from PQ and remove it
create a binary tree node z with the character
and value the sum of the values
of min and secondM in
assign min to be the left child of z and secondM in to be the right child of z
add z to PQ
return the minimum from PQ
Psuedocode for CreateTable function
If nodes left child and right child are both null, then insert nodes character as key
and the string encoding as value into the hashtable charToEncodingMapping
Else do the following:
If nodes left child null, createTablenodes left child, encoding
If nodes right child null, createTablenodes right child, encoding
Psuedocode for Decode function:
Let encode and decodedMsg
Let n be the length of the encoded message
For i to i n do the following:
encode encode the character at index i of encodedMsg
If the hashtable encodingToCharMapping contains the key encode, then
let c be the value for the key encode in encodingToCharMapping
decodedMsg decodedMsg c
encode ;
return decodedMsg
Complete the functions as mentioned below in C
#include
#include
#include
#include "BinaryTreeNode.h
#include "BinaryTreeNodeComparator.h
#ifndef HUFFMANENCODERH
#define HUFFMANENCODERH
class HuffmanEncoder
int encodingLength;
int tableSize;
unorderedmap charToEncodingMapping;
unorderedmap frequencyMap;
public:
HuffmanEncoderunorderedmap freqMap
frequencyMap freqMap;
encodingLength tableSize ;
~HuffmanEncoder
void encode
complete this method
private:
BinaryTreeNode buildTree
complete this method
void createTableBinaryTreeNode node string encoding
complete this method
public:
string getEncodingchar c
return charToEncodingMappingc;
int getTableSize
return tableSize;
int getEncodingLength
return encodingLength;
;
Complete functions in this class
#include
#include
using namespace std;
#ifndef HUFFMANDECODERH
#define HUFFMANDECODERH
class HuffmanDecoder
public:
static string decodestring encodedMsg, unorderedmap encodingToCharMapping
complete this method
;
#endif HUFFMANDECODERH
Thank you for your assistance!
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
