How to make an array of pointer. What did i did wrong in the function below. I t
ID: 3695127 • Letter: H
Question
How to make an array of pointer. What did i did wrong in the function below. I tried to write codes based on the conditions shown in the comments but its not working. Can some one help me with that?
/*
Creates an array of HuffmanNodes (more specifically, pointers to HuffmanNodes)
based on the given character-frequency pairs.
characters: an array of char
frequencies: an array of int
length: the length of characters and frequencies arrays
returns: an array of HuffmanNode*
Note: For 0 <= i < length, the frequency of the character characters[i]
is found at frequencies[i].
*/
HuffmanNode **genHuffmanNodes(char *characters, int *frequencies, int length)
{
// TODO
cout << "genHuffmanNodes activated! " << endl;
HuffmanNode **arr;
//arr = new (HuffmanNode*[length]);
//HuffmanNode *arr;
for (int i = 0;i < length; i++)
{
arr[i]->character = characters[i];
arr[i]->frequency = frequencies[i];
}
for (int i=0; i<length;i++)
{
cout << "starting of the loop: " << endl;
cout << "i is: " << i << endl;
cout << "character[i] is :" << arr[i]->character << endl;
cout << "frequency[i] is :" << arr[i]->frequency << endl;
}
return arr;
}
Explanation / Answer
Program:
#include<iostream>
#include<fstream>
#include<deque>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
using namespace std;
struct Node
{
double frequency;
char alphabet;
Node *left;
Node *right;
Node::Node()
{
frequency = 0;
alphabet = ' ';
left = NULL;
right = NULL;
}
};
struct Code{
char alp;
string code;
};
vector<Code> allCodes;
void BuildHuffmanTree();
void getCode(Node *root, string soFar);
void printTree(Node *root);
int main()
{
string plainText;
string codedText = "";
cout << "Please enter the text you would like to convert: " << endl;
cin >> plainText;
BuildHuffmanTree();
for(int i = 0; i < plainText.size(); i++)
{
char temp;
string tempA;
temp = plainText[i];
for(int j = 0; j < allCodes.size(); j++)
{
if(allCodes[j].alp == temp)
codedText.append(allCodes[j].code);
}
}
cout << codedText << endl;
return 0;
}
void BuildHuffmanTree()
{
ifstream input;
input.open("FrequencyTable1.txt");
ofstream output;
output.open("HuffmanCode.txt");
deque<Node> frequencyVector;
Node *Root;
double check = 0.000;
Node test;
vector<char> alphabets;
//Storing the frequency into a deque.
for(int k = 0; k < 26; k++)
{
Node temp;
char tempChar;
double tempFrequency;
input >> tempChar;
input >> tempFrequency;
temp.alphabet = tempChar;
temp.frequency = tempFrequency/100;
check+= tempFrequency;
temp.left = NULL;
temp.right = NULL;
alphabets.push_back(tempChar);
frequencyVector.push_back(temp);
}
sort(frequencyVector.begin(), frequencyVector.end());
//cout << check << endl;
for(int i = 0; i < frequencyVector.size(); i++)
{
cout << frequencyVector[i].frequency << " ";
}
cout << endl;
//Creating the Huffman Tree.
while(frequencyVector.size() >= 2)
{
Node tempOne;
Node tempTwo;
Node anotherTemp;
double tempFrequency = 0;
double tempChar;
anotherTemp.left = &frequencyVector.front();
tempFrequency += frequencyVector.front().frequency;
frequencyVector.pop_front();
anotherTemp.right = &frequencyVector.front();
tempFrequency += frequencyVector.front().frequency;
frequencyVector.pop_front();
anotherTemp.frequency = tempFrequency;
frequencyVector.push_back(anotherTemp);
sort(frequencyVector.begin(), frequencyVector.end());
cout << endl;
}
//Getting the Huffman code for each alphabet.
Root = &frequencyVector.front();
getCode(Root, "");
//printTree(&Root);
for(int i = 0; i < allCodes.size(); i++)
{
cout << allCodes[i].alp << " :" << allCodes[i].code << endl;
output << allCodes[i].alp << " :" << allCodes[i].code << endl;
}
}
bool operator < (const Node &one, const Node &two)
{
if(one.frequency < two.frequency)
return 1;
else
return 0;
}
void getCode(Node *root, string soFar)
{
if(root == NULL)
return;
if(!(root->left) && !(root->right))
{
Code temp;
temp.alp = root->alphabet;
temp.code = soFar;
allCodes.push_back(temp);
}
else
{
//Add a 0 if we go left.
getCode(root->left,(soFar.append("0")));
soFar.erase(soFar.size()-1);
//Add a 1 if we go right.
getCode(root->right,(soFar.append("1")));
}
}
void printTree(Node *root)
{
if (!root) return;
printTree(root->left);
cout << root->alphabet<< " ";
printTree(root->right);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.