C++ class实现Huffman树(完整代码)
生活随笔
收集整理的這篇文章主要介紹了
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;}elseif (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;}elseif (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树(完整代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Steam 平台“黑五”秋季特卖折扣上线
- 下一篇: C++ class实现邻接矩阵存储的图(