C++ class实现Huffman树(完整代码)
Posted Wecccccccc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ class实现Huffman树(完整代码)相关的知识,希望对你有一定的参考价值。
代码如下:
#include <iostream>
#include <string>
using namespace std;
const unsigned int n = 8;//字符数NUM,这里的字符数为8
const unsigned int m = 2 * n - 1;//结点总数
const float MAX = 1e8;
class HTNode
{
friend class HuffmanTree;
private:
float weight;
int parent;
int lchild;
int rchild;
};
class HuffmanTree
{
public:
HuffmanTree();
~HuffmanTree() = default;
void PrintCode();
private:
void DFS(int i, int step);
void Select(int k, int &s1, int &s2);
HTNode HT[m + 1];
int code[m + 1];
};
HuffmanTree::HuffmanTree()
{
int s1, s2;
for (int i = 1; i <= m; i++)
{
HT[i].weight = 0;
HT[i].parent = -1;
HT[i].lchild = -1;
HT[i].rchild = -1;
}
cout << "Please input the weight" << endl;
for (int i = 1; i <= n; i++)cin >> HT[i].weight;
for (int i = n + 1; i <= m; i++)
{
Select(i - 1, s1, s2);
HT[s1].parent = i;
HT[s2].parent = i;
HT[i].lchild = s1;
HT[i].rchild = s2;
HT[i].weight = HT[s1].weight + HT[s2].weight;
cout << HT[i].weight << " " << "(" << HT[s1].weight << "," << HT[s2].weight << ")" << endl;//该输出为测试数据使用
}
}
void HuffmanTree::Select(int k, int &s1, int &s2)
{
HT[0].weight = MAX;
s1 = s2 = 0;
for (int i = 1; i <= k; i++)
if (HT[i].weight != 0 && HT[i].parent == -1)
{
if (HT[i].weight < HT[s1].weight)
{
s2 = s1;
s1 = i;
}
else
if (HT[i].weight < HT[s2].weight) s2 = i;
}
}
void HuffmanTree::DFS(int i,int step)
{
if (HT[i].lchild == -1 && HT[i].rchild == -1)
{
cout << HT[i].weight << " = ";
for (int i = 0; i < step; i++)
cout << code[i];
cout << endl;
return;
}
if (HT[i].lchild != -1 )
{
code[step] = 0;
DFS(HT[i].lchild,step+1);
}
if (HT[i].rchild != -1 )
{
code[step] = 1;
DFS(HT[i].rchild, step+1);
}
}
void HuffmanTree::PrintCode()
{
for (int i = 0; i < m + 1; i++) code[i] = false;
DFS(m, 0);
}
int main()
{
HuffmanTree t;
cout << endl;
t.PrintCode();
return 0;
}
测试结果:
动态内存分配写法:
#include <iostream>
using namespace std;
const int MAX = 1e8;
class HuffmanNode
{
friend class HuffmanTree;
private:
double w;
int par;
int lch;
int rch;
};
class HuffmanTree
{
friend class HuffmanTree;
public:
HuffmanTree() ;
~HuffmanTree();
void PrintCode();
private:
void dfs(int i,int s);
void Select(int n, int &s1, int &s2);
HuffmanNode *HT;
int *path;
int m;
};
void HuffmanTree::Select(int n, int &s1, int &s2)
{
s1 = s2 = 0;
HT[0].w = MAX;
for (int i = 1; i <= n; i++)
{
if (HT[i].par==-1 && HT[i].w!=0)
if (HT[i].w < HT[s1].w)
{
s2 = s1;
s1 = i;
}
else
if (HT[i].w < HT[s2].w) s2 = i;
}
}
HuffmanTree::HuffmanTree() :HT(nullptr), path(nullptr)
{
cout << "Please input node:" << endl;
int n;
cin >> n;
m = 2 * n - 1;
HT = new HuffmanNode[m + 1];
for (int i = 1; i <= m; i++)
{
HT[i].par = -1;
HT[i].lch = -1;
HT[i].rch = -1;
HT[i].w = 0;
}
for (int i = 1; i <= n; i++) cin >> HT[i].w;
for (int i = n + 1; i <= m; i++)
{
int s1, s2;
Select(i - 1, s1, s2);
HT[s1].par = i;
HT[s2].par = i;
HT[i].lch = s1;
HT[i].rch = s2;
HT[i].w = HT[s1].w + HT[s2].w;
cout << HT[i].w << " " << "(" << HT[s1].w << "," << HT[s2].w << ")" << endl;
}
}
void HuffmanTree::dfs(int i,int s)
{
if (HT[i].lch == -1 && HT[i].rch == -1)
{
cout << HT[i].w << " = ";
for (int i = 0; i < s; i++) cout << path[i];
cout << endl;
return;
}
if (HT[i].lch != -1)
{
path[s] = 0;
dfs(HT[i].lch, s + 1);
}
if (HT[i].rch != -1)
{
path[s] = 1;
dfs(HT[i].rch, s + 1);
}
}
void HuffmanTree::PrintCode()
{
path = new int[m + 1];
for (int i = 0; i < m + 1; i++) path[i] = 0;
dfs(m,0);
delete[]path;
path = nullptr;
}
HuffmanTree::~HuffmanTree()
{
delete[] HT;
HT = nullptr;
}
int main()
{
HuffmanTree t;
t.PrintCode();
return 0;
}
测试结果:
以上是关于C++ class实现Huffman树(完整代码)的主要内容,如果未能解决你的问题,请参考以下文章