为什么我的输出在霍夫曼编码中打印#
Why My output is printing # in Huffman coding
所以我正在实施霍夫曼编码问题,我在其中编写了以下代码,下面是附加的输出。谁能告诉我为什么我在输出中得到 #:-
#include<bits/stdc++.h>
using namespace std;
class HuffTree
{
public:
int val;
char letter;
HuffTree* left, * right;
HuffTree(int val, char letter)
{
this->val = val;
this->letter = letter;
this->left = nullptr;
this->right = nullptr;
}
};
vector<pair<int, char>> FrequencyCount(string s)
{
map<char, int> mp;
for (int i = 0;i < s.size();i++)
mp[s[i]]++;
vector<pair<int, char>> v;
for (auto it : mp)
v.push_back(make_pair(it.second, it.first));
return v;
}
class Compare
{
public:
bool operator() (HuffTree* a, HuffTree* b)
{
return (a->val > b->val);
}
};
vector<pair<char, string >> huffmannCodes;
void printCodes(HuffTree* root, string str)
{
if (!root)
return;
if (root->val != '#')
huffmannCodes.push_back(make_pair(root->letter, str));
printCodes(root->left, str + "0");
printCodes(root->right, str + "1");
}
void makeTree(vector<pair<int, char>>& frq)
{
priority_queue<HuffTree*, vector<HuffTree*>, Compare> pq;
for (auto it : frq)
pq.push(new HuffTree(it.first, it.second));
while (pq.size() > 1)
{
HuffTree* left = pq.top();
pq.pop();
HuffTree* right = pq.top();
pq.pop();
HuffTree* newNode = new HuffTree(left->val + right->val, '#');
newNode->left = left;
newNode->right = right;
pq.push(newNode);
}
printCodes(pq.top(), "");
}
int main()
{
string s;
cin >> s;
vector<pair<int, char>> freqcount = FrequencyCount(s);
sort(freqcount.begin(), freqcount.end());
makeTree(freqcount);
for (auto it : huffmannCodes)
cout << it.first << "-->" << it.second << endl;
}
当我 运行 程序时,这就是我在输出中得到的:-
output
谁能告诉我为什么我的输出包含 # 作为节点。
任何帮助将不胜感激。
if (root->val != '#')
应该是 if (root->letter != '#')
.
所以我正在实施霍夫曼编码问题,我在其中编写了以下代码,下面是附加的输出。谁能告诉我为什么我在输出中得到 #:-
#include<bits/stdc++.h>
using namespace std;
class HuffTree
{
public:
int val;
char letter;
HuffTree* left, * right;
HuffTree(int val, char letter)
{
this->val = val;
this->letter = letter;
this->left = nullptr;
this->right = nullptr;
}
};
vector<pair<int, char>> FrequencyCount(string s)
{
map<char, int> mp;
for (int i = 0;i < s.size();i++)
mp[s[i]]++;
vector<pair<int, char>> v;
for (auto it : mp)
v.push_back(make_pair(it.second, it.first));
return v;
}
class Compare
{
public:
bool operator() (HuffTree* a, HuffTree* b)
{
return (a->val > b->val);
}
};
vector<pair<char, string >> huffmannCodes;
void printCodes(HuffTree* root, string str)
{
if (!root)
return;
if (root->val != '#')
huffmannCodes.push_back(make_pair(root->letter, str));
printCodes(root->left, str + "0");
printCodes(root->right, str + "1");
}
void makeTree(vector<pair<int, char>>& frq)
{
priority_queue<HuffTree*, vector<HuffTree*>, Compare> pq;
for (auto it : frq)
pq.push(new HuffTree(it.first, it.second));
while (pq.size() > 1)
{
HuffTree* left = pq.top();
pq.pop();
HuffTree* right = pq.top();
pq.pop();
HuffTree* newNode = new HuffTree(left->val + right->val, '#');
newNode->left = left;
newNode->right = right;
pq.push(newNode);
}
printCodes(pq.top(), "");
}
int main()
{
string s;
cin >> s;
vector<pair<int, char>> freqcount = FrequencyCount(s);
sort(freqcount.begin(), freqcount.end());
makeTree(freqcount);
for (auto it : huffmannCodes)
cout << it.first << "-->" << it.second << endl;
}
当我 运行 程序时,这就是我在输出中得到的:-
output
谁能告诉我为什么我的输出包含 # 作为节点。
任何帮助将不胜感激。
if (root->val != '#')
应该是 if (root->letter != '#')
.