1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
const int UniqueSymbols = 1 << CHAR_BIT;
const char* SampleString = "Testing";
typedef std::vector<bool> HuffCode;
typedef std::map<char, HuffCode> HuffCodeMap;
class ZNode
{
public:
const int j;
virtual ~ZNode() {}
protected:
ZNode(int j) : j(j) {}
};
class InternalNode : public ZNode
{
public:
ZNode *const left;
ZNode *const right;
InternalNode(ZNode* c0, ZNode* c1) : ZNode(c0->j + c1->j), left(c0), right(c1) {}
~InternalNode()
{
delete left;
delete right;
}
};
class LeafNode : public ZNode
{
public:
const char c;
LeafNode(int j, char c) : ZNode(j), c(c) {}
};
struct NodeCmp
{
bool operator()(const ZNode* lhs, const ZNode* rhs) const { return lhs->j > rhs->j; }
};
ZNode* BuildTree(const int(&frequencies)[UniqueSymbols])
{
std::priority_queue<ZNode*, std::vector<ZNode*>, NodeCmp> trees;
for (int i = 0; i < UniqueSymbols; ++i)
{
if (frequencies[i] != 0)
trees.push(new LeafNode(frequencies[i], (char)i));
}
while (trees.size() > 1)
{
ZNode* childR = trees.top();
trees.pop();
ZNode* childL = trees.top();
trees.pop();
ZNode* parent = new InternalNode(childR, childL);
trees.push(parent);
}
return trees.top();
}
void GenerateCodes(const ZNode* node, const HuffCode& prefix, HuffCodeMap& outCodes)
{
if (const LeafNode* lf = dynamic_cast<const LeafNode*>(node))
{
outCodes[lf->c] = prefix;
}
else if (const InternalNode* in = dynamic_cast<const InternalNode*>(node))
{
HuffCode leftPrefix = prefix;
leftPrefix.push_back(false);
GenerateCodes(in->left, leftPrefix, outCodes);
HuffCode rightPrefix = prefix;
rightPrefix.push_back(true);
GenerateCodes(in->right, rightPrefix, outCodes);
}
int main()
int frequencies[UniqueSymbols] = { 0 };
const char* ptr = SampleString;
while (*ptr != '\0')
++frequencies[*ptr++];
ZNode* root = BuildTree(frequencies);
HuffCodeMap codes;
GenerateCodes(root, HuffCode(), codes);
delete root;
for (HuffCodeMap::const_iterator it = codes.begin(); it != codes.end(); ++it)
{
std::cout << it->first << " ";
std::copy(it->second.begin(), it->second.end(),
std::ostream_iterator<bool>(std::cout));
std::cout << std::endl;
}
{
char ht[] = { "There foes four near times." };
int length = sizeof(ht) / sizeof(char); // <----
char tmp = '_';
int freq = 0;
for (int i = 0; i < length; i++)
{
tmp = ht[i];
freq = 1;
for (int j = i + 1; j < 27; j++)
{
if (ht[i] == ht[j])
{
freq++;
}//if
}//j
cout << tmp << '=';
cout << freq << endl;
};//i
return 0;
}
|