本实验构建最优二叉树来实现哈夫曼编码
使用VS2017完成
关于哈夫曼编码的头文件huffman.h
//huffman.h #ifndef HUFFMAN_H #define HUFFMAN_H #define OK 1 #define SIZE 256 struct HTNode { int weight;//权值 int parent;//父节点 int lchild;//左孩子 int rchild;//右孩子 }; typedef HTNode *HuffmanTree;//动态分配数组存储Huffman树 typedef char **HuffmanCode;//动态分配哈夫曼编码表 //void PreorderTraverse(int root, HuffmanTree pHT); int HuffmanCoding(HuffmanCode &pHC, HuffmanTree &pHT); int Select(HuffmanTree pHT, int nSize); void TestHufTree(HuffmanTree pHT); void TestHufCode(int root, HuffmanTree pHT, HuffmanCode pHC); void TestHufTreeN(int root, HuffmanTree pHT); int HfmTree(HuffmanTree &pHT, int *w, int n); #endif
相关实现huffman.cpp
//Huffman.cpp #include<iostream> #include<cstring> #include"huffman.h" #pragma warning( disable : 4996) using namespace std; /* void PreorderTraverse(int root, HuffmanTree pHT) { cout << pHT[root].weight << " ";//访问节点 if (pHT[root].lchild)//左孩子 { PreorderTraverse(pHT[root].lchild, pHT); } if (pHT[root].rchild)//右孩子 { PreorderTraverse(pHT[root].rchild, pHT); } } */ int HuffmanCoding(HuffmanCode &pHC, HuffmanTree &pHT) { // pHC = (HuffmanCode)malloc((SIZE + 1) * sizeof(char*)); //无栈非递归遍历 char cd[SIZE] = { '