From 89dc76c420311e709d7053a156af577ef6880846 Mon Sep 17 00:00:00 2001 From: Powfu <1875065753@qq.com> Date: Tue, 26 Dec 2023 12:43:09 +0000 Subject: [PATCH] =?UTF-8?q?add=202101040022/chapter=5F7/=E6=9E=84=E9=80=A0?= =?UTF-8?q?=E5=93=88=E5=A4=AB=E6=9B=BC=E6=A0=91=E5=92=8C=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=93=88=E5=A4=AB=E6=9B=BC=E7=BC=96=E7=A0=81.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Powfu <1875065753@qq.com> --- ...3\346\233\274\347\274\226\347\240\201.cpp" | 182 ++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 "2101040022/chapter_7/\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.cpp" diff --git "a/2101040022/chapter_7/\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.cpp" "b/2101040022/chapter_7/\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.cpp" new file mode 100644 index 00000000..b31a0462 --- /dev/null +++ "b/2101040022/chapter_7/\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.cpp" @@ -0,0 +1,182 @@ +#include +#include +using namespace std; +#define MAXSIZE 100 + +typedef struct +{ + int weight; + int parent, lchild, rchild; +}HTNode, * HuffmanTree; + +typedef char** HuffmanCode; + +void Select(HuffmanTree HT, int t, int &s1, int &s2) +{ + for (int i = 1; i <= t; i++) + { + if (HT[i].parent == 0) + { + s1 = s2 = i; + break; + } + } + int temp; + for (int i = 1; i <= t; i++) + if (HT[i].parent == 0) + { + if (s1 == s2) + if (HT[i].weight < HT[s1].weight) + s1 = i; + else + s2 = i; + else if (HT[i].weight < HT[s1].weight && HT[i].weight < HT[s2].weight) + { + s2 = s1; + s1 = i; + } + else if (HT[i].weight > HT[s1].weight && HT[i].weight < HT[s2].weight) + s2 = i; + } +} + +void HT_OutPut(HuffmanTree HT, int n) +{ + cout << ">> 该哈夫曼树的存储结构如下:" << endl; + cout << "\t结点\t权重\t父亲\t左孩子\t右孩子" << endl; + for (int i = 1; i < 2*n; i++) + cout << "\t " + << i + << "\t " + << HT[i].weight + << "\t " + << HT[i].parent + << "\t " + << HT[i].lchild + << "\t " + << HT[i].rchild + << endl; +} + +void CreateHuffmanTree(HuffmanTree &HT, int n) +{ + if (n <= 1) + return; + int m = 2 * n - 1; + HT = new HTNode[m + 1]; + for (int i = 0; i <= m; i++) + { + HT[i].weight = 0; + HT[i].parent = 0; + HT[i].lchild = 0; + HT[i].rchild = 0; + } + cout << "依次输入叶子结点的权值: "; + for (int i = 1; i <= n; i++) + cin >> HT[i].weight; + getchar(); + + for (int i = n + 1; i <= m; i++) + { + int s1, s2; + Select(HT, 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; + } +} + +void CreatHuffmanCode(HuffmanTree HT, HuffmanCode& HC, int n) +{ + HC = new char* [n + 1]; + char* cd = new char[n]; + cd[n - 1] = '\0'; + for (int i = 1; i <= n; i++) + { + int start = n - 1; + int c = i; + int f = HT[i].parent; + while (f != 0) + { + start--; + if (HT[f].lchild == c) + cd[start] = '0'; + else + cd[start] = '1'; + c = f; + f = HT[f].parent; + } + HC[i] = new char[n - start]; + strcpy(HC[i], &cd[start]); + } + delete cd; +} + +void HC_OutPut(HuffmanCode HC, int n) +{ + int length = 0; + cout << ">> 下面是哈夫曼编码表: " << endl; + for (int i = 1; i <= n; i++) + { + int j = 0; + cout << "\t编码" << i << ": "; + while (HC[i][j] != '\0') + { + cout << HC[i][j++]; + length++; + } + cout << endl; + } + cout << "\t>> 平均查找长度为: " << (double)length / n << endl; +} + +void TransHuffmanCode(HuffmanTree HT,int n, char* cd) +{ + int start; + for (int i = 1; i <= 2 * n - 1; i++) + if (HT[i].parent == 0) + { + start = i; + break; + } + int p = start; + cout << "译码后的结果: "; + while (*cd != '\0') + { + if (HT[p].lchild == 0 && HT[p].rchild == 0) + { + cout << HT[p].weight << " "; + p = start; + } + if (*cd == '0') + p = HT[p].lchild; + else if (*cd == '1') + p = HT[p].rchild; + cd = cd + 1; + } + cout << endl; +} + +int main() +{ + HuffmanTree HT; + HuffmanCode HC; + char cd[MAXSIZE], ch; + int n; + cout << "请输入您生成的哈夫曼树的叶子结点个数: "; + cin >> n; + CreateHuffmanTree(HT, n); + CreatHuffmanCode(HT, HC, n); + HT_OutPut(HT, n); + HC_OutPut(HC, n); + cout << "请在上述哈夫曼编码中选择你要译码的编码: "; + int i = 0; + while ((ch = getchar()) != '\n') + cd[i++] = ch; + cd[++i] = '\0'; + TransHuffmanCode(HT, n, cd); + system("pause"); + return 0; +} \ No newline at end of file -- Gitee