二叉树及其应用
一、 实验内容
实现一个哈夫曼编码系统,系统包括以下功能:
(1) 字符信息统计:读取待编码的源文件txt文档,统计出现的字符及其频率。
(2) 建立哈夫曼树:根据统计结果建立哈夫曼树。
(3) 建立哈夫曼码表:利用得到的哈夫曼树,将各字符对应的编码表保存在文件结果txt文档中。
(4) 对源文件进行编码:根据哈夫曼码表,将原txt文件中的字符转换成相应的编码文件。
1 #include<iostream> 2 #include<string> 3 #include<malloc.h> 4 #include<stdlib.h> 5 #include<fstream> 6 #include<cassert> 7 #include<cstdio> 8 #include<stdio.h> 9 using namespace std; 10 #define OK 1 11 #define OVERFLOW -1 12 #define ERROR 0 13 #define MAXSIZE 100 14 int Sum[26]; 15 int i = -1; 16 char c[100]; 17 int typedef Status; 18 typedef char **HuffmanCode; 19 void Chu()//初始化数组 20 { 21 for (int i = 0; i < 26; i++){ 22 Sum[i] = 0; 23 } 24 } 25 typedef struct{ 26 int weight; 27 int parent, lchild, rchild; 28 29 }HTNode,*HuffmanTree;//哈夫曼树定义 30 void Select(HuffmanTree &HT, int m, int &s1, int &s2)//选出最小数 31 { 32 //cout << m << endl; 33 int j,t; 34 s1 = 1; 35 s2 = 1; 36 for (j = 1; j <=m; j++){ 37 if (HT[j].weight>HT[s1].weight){ 38 s1 = j; 39 s2 = j; 40 41 } 42 } 43 for (j = 1; j <= m; j++){ 44 if(HT[j].parent == 0){ 45 if (HT[j].weight < HT[s1].weight) 46 s1 = j; 47 } 48 } 49 for (j = 1; j <= m; j++){ 50 if (HT[j].parent == 0 && j != s1){ 51 if (HT[j].weight < HT[s2].weight) 52 s2 = j; 53 } 54 } 55 //cout <<'\n'<< HT[s1].weight<< '\t' <<HT[s2].weight << endl; 56 } 57 void CreateHuffmanTree(HuffmanTree &HT, int n,int &s1, int & s2){//哈夫曼树构造 58 if (n <= 1)return; 59 int m = n * 2 - 1; 60 HT = new HTNode[m + 1]; 61 for (int i = 1; i <=m; ++i){ 62 HT[i].parent = 0; HT[i].lchild = 0; HT[i].rchild = 0; 63 } 64 for (int i = 1; i <= n; ++i){ 65 HT[i].weight=Sum[i-1]; 66 // cout << HT[i].weight; 67 } 68 for (int i = n+1; i <= m; ++i){ 69 Select(HT, i - 1, s1, s2); 70 HT[s1].parent = i; 71 HT[s2].parent = i; 72 HT[i].lchild = s1; 73 HT[i].rchild = s2; 74 HT[i].weight = HT[s1].weight + HT[s2].weight; 75 //cout << HT[i].weight << endl; 76 } 77 } 78 void CreatHuffmanCode(HuffmanTree HT, HuffmanCode &HC, int n){//从叶子到跟逆向求出每个字哈夫曼编码,存在HC中 79 char cd[7]; 80 HC = new char*[n + 1]; 81 cd == new char[n]; 82 cd[n - 1] = '\0'; 83 int start; 84 int c, f; 85 for (int i = 1; i <=n; i++){ 86 start = n - 1; 87 c = i; f = HT[i].parent; 88 //cout << f << endl; 89 //if (i == 4)cout << "55555555"; 90 while (f != 0){ 91 --start; 92 if (HT[f].lchild == c) cd[start] = '0'; 93 else cd[start] = '1'; 94 c = f; f = HT[f].parent; 95 } 96 //cout << start<<"oooo"<<endl; 97 //cout << n - start<<"adfas"<<endl; 98 HC[i] = new char[n - start]; 99 strcpy_s(HC[i], strlen(&cd[start]) + 2, &cd[start]); 100 cout << i<<endl; 101 cout << HC[i]<<"qqqqqqq"<<endl; 102 } 103 104 // delete cd; 105 //cout << "退出循环"; 106 } 107 void readTxt(string file){ 108 ifstream infile; 109 infile.open(file.data()); 110 assert(infile.is_open()); 111 while (!infile.eof()){ 112 i++; 113 infile >> c[i]; 114 cout << c[i] << endl; 115 } 116 //for (int M = 0; M < i; M++)cout << c[M]; 117 infile.close(); 118 } 119 void Txt_cun(){ 120 Chu(); 121 for (int I = 0; I < i; I++) 122 { 123 if (c[I] == 'a' || c[I] == 'A'){ 124 Sum[0] += 1; 125 } 126 else if (c[I] == 'b' || c[I] == 'B'){ 127 Sum[1] +=1 ; 128 } 129 else if (c[I] == 'c' || c[I] == 'C'){ 130 Sum[2] += 1; 131 } 132 else if (c[I] == 'd' || c[I] == 'D'){ 133 134 Sum[3] +=1; 135 } 136 else if (c[I] == 'e' || c[I] == 'E'){ 137 138 Sum[4] += 1; 139 } 140 else if (c[I] == 'f' || c[I] == 'F'){ 141 142 Sum[5] += 1; 143 } 144 else if (c[I] == 'g' || c[I] == 'G'){ 145 146 Sum[6] += 1; 147 } 148 else if (c[I] == 'h' || c[I] == 'H'){ 149 150 Sum[7] += 1; 151 } 152 else if (c[I] == 'I' || c[I] == 'i'){ 153 154 Sum[8] += 1; 155 } 156 else if (c[I] == 'j' || c[I] == 'J'){ 157 158 Sum[9] += 1; 159 } 160 else if (c[I] == 'k' || c[I] == 'K'){ 161 162 Sum[10] += 1; 163 } 164 else if (c[I] == 'l' || c[I] == 'L'){ 165 166 Sum[11]+= 1; 167 } 168 else if (c[I] == 'm' || c[I] == 'M'){ 169 170 Sum[12] += 1; 171 } 172 else if (c[I] == 'N' || c[I] == 'n'){ 173 174 Sum[13] += 1; 175 } 176 else if (c[I] == 'o' || c[I] == 'O'){ 177 178 Sum[14] += 1; 179 } 180 else if (c[I] == 'p' || c[I] == 'P'){ 181 182 Sum[15] += 1; 183 } 184 else if (c[I] == 'q' || c[I] == 'Q'){ 185 186 Sum[16] += 1; 187 } 188 else if (c[I] == 'r' || c[I] == 'R'){ 189 190 Sum[17] += 1; 191 } 192 else if (c[I] == 's' || c[I] == 'S'){ 193 194 Sum[18] += 1; 195 } 196 else if (c[I] == 't' || c[I] == 'T'){ 197 198 Sum[19] += 1; 199 } 200 else if (c[I] == 'u' || c[I] == 'U'){ 201 202 Sum[20] += 1; 203 } 204 else if (c[I] == 'v' || c[I] == 'V'){ 205 206 Sum[21] += 1; 207 } 208 else if (c[I] == 'w' || c[I] == 'W'){ 209 210 Sum[22] += 1; 211 } 212 else if (c[I] == 'x' || c[I] == 'X'){ 213 214 Sum[23] += 1; 215 } 216 else if (c[I] == 'y' || c[I] == 'Y'){ 217 218 Sum[24] += 1; 219 } 220 else if (c[I] == 'z' || c[I] == 'Z'){ 221 222 Sum[25] += 1; 223 } 224 }//for (int m = 0; m < 26; m++)cout << Sum[m] << endl; 225 }//对字母分配权值 226 int main(){ 227 readTxt("字符信息统计.txt"); 228 //cout << i; 229 Txt_cun(); 230 int M = 26; 231 for (int Z=0; Z < M; Z++){ 232 if (Sum[Z] == 0){ 233 for (int X = Z + 1; X < M; X++){ 234 Sum[X - 1] = Sum[X]; 235 } 236 M--; 237 Z--; 238 } 239 240 } 241 HuffmanTree A; 242 HuffmanCode HC; 243 int s1, s2; 244 CreateHuffmanTree(A, M, s1, s2); 245 CreatHuffmanCode(A, HC, M);// cout << "vvvvvvvvvv"; 246 ofstream outfile; 247 outfile.open("编码表.txt", ios::trunc); 248 for (int L = 1; L <= M; L++){ 249 outfile << HC[L] << "\n"; 250 } 251 outfile.close(); 252 //cout << HC; 253 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异