#include <iostream> #include <queue> #include <string.h> #include <vector> #include <algorithm> using namespace std; char Table[26]; struct Node { int freq; char val; Node * left; Node * right; Node():left(NULL), right(NULL) , freq(0) , val('0'){} }; class Cmp { public: bool operator() (const Node * a, const Node * b) const { return a->freq > b->freq; // 从小到大 ,freq 小的 优先级别高 } }; priority_queue<Node* , vector<Node*> , Cmp> myQueue; void BuildTree() { for (int i = 0; i < 26; ++ i) { if (Table[i] > 0) { Node * node = new Node(); node->freq = Table[i]; node->val =(char) (i + 'A'); myQueue.push(node); } } while (myQueue.size() > 1) { Node * f = myQueue.top(); myQueue.pop(); Node * s = myQueue.top(); myQueue.pop(); Node * tmp = new Node(); tmp->freq = f->freq + s->freq; tmp->left = f; tmp->right = s; myQueue.push(tmp); } //cout << myQueue.top()->freq<<endl; } struct PrintNode { int freq; char val; string code; }; vector<PrintNode> vpn; bool cmp(const PrintNode & a , const PrintNode & b) { return a.freq > b.freq; } void Print( Node * node , string res) { if (node == NULL) { return; } if (node->val != '0') { PrintNode pn; pn.val = node->val; pn.freq = node->freq; pn.code = res; vpn.push_back(pn); //cout <<node->val <<" | " << node->freq <<" | "<< res <<endl; return ; } Print(node->left , res + "0"); Print(node->right, res + "1"); delete node->left; delete node->right; } int main() { int N; memset(Table , 0 , sizeof(Table)); cin >> N; for (int i = 0; i < N; ++i) { char ch ; cin >> ch; if (ch >= 'A' && ch <= 'Z') { ++Table[ch - 'A']; } } BuildTree(); Node * root = myQueue.top(); myQueue.pop(); Print(root , ""); sort(vpn.begin() , vpn.end() , cmp); for (int i = 0; i < vpn.size(); ++i) { cout <<vpn[i].val << " "<<vpn[i].freq <<" " << vpn[i].code<<endl; } return 0; } 具体的构成算法不再这里讨论了,
您还没有登录,请您登录后再发表评论
哈夫曼编码(Huffman Coding)是一种非常经典的编码方式,实现起来也很简单,在实际的笔试面试过程中有可能会遇到,这里介绍一下它的原理和一个使用优先队列的实现版本。 一 编码原理 哈夫曼编码是一种可变长的...
14、哈夫曼树 88 BinTreeNode.h 88 BinaryTree.h 89 MinHeap.h 92 Huffman.h 95 Test.cpp 96 15、树 97 QueueNode.h 97 LinkQueue.h 97 TreeNode.h 100 Tree.h 100 test.cpp 110 16、B+树 111 BTreeNode.h 111 BTree...
14、哈夫曼树 149 BinTreeNode.h 149 BinaryTree.h 151 MinHeap.h 156 Huffman.h 161 Test.cpp 163 15、树 164 QueueNode.h 164 LinkQueue.h 165 TreeNode.h 169 Tree.h 170 test.cpp 187 16、B+树 189 BTreeNode.h ...
14、哈夫曼树 166 BinTreeNode.h 166 BinaryTree.h 169 MinHeap.h 174 Huffman.h 180 Test.cpp 182 15、树 183 QueueNode.h 183 LinkQueue.h 184 TreeNode.h 189 Tree.h 190 test.cpp 208 16、B+树 210 BTreeNode.h ...
目录 出版说明 献词 简介 第1章 综述 ... 哈夫曼(Huffman)编码 小结 问题 实验 编程作业 第9章 红-黑树 第10章 2-3-4树和外部存储 第11章 哈希表 第12章 堆 第13章 图 第14章 带权图 第15章 应用场合
哈夫曼(Huffman)编码 小结 问题 实验 编程作业 第9章 红-黑树 第10章 2-3-4树和外部存储 第11章 哈希表 第12章 堆 第13章 图 第14章 带权图 第15章 应用场合 附录A 运行专题applet和示例程序 附录B 进一步学习 附录...
哈夫曼(Huffman)编码 小结 问题 实验 编程作业 第9章 红-黑树 第10章 2-3-4树和外部存储 第11章 哈希表 第12章 堆 第13章 图 第14章 带权图 第15章 应用场合 附录A 运行专题applet和示例程序 附录B 进一步学习 ...
哈夫曼(Huffman)编码 小结 问题 实验 编程作业 第9章 红-黑树 本章讨论的方法 平衡树和非平衡树 使用RBTree专题applet 用专题applet做试验 旋转 插入一个新节点 删除 红-黑树的效率 红-黑树的实现...
3、从终端读入字符集大小为n(即字符的个数),逐一输入n个字符和相应的n个权值(即字符出现的频度),建立哈夫曼树,进行编码并且输出。 注:可用C或C++编写。 4、用邻接矩阵或邻接图实现一个有向图的...
相关推荐
哈夫曼编码(Huffman Coding)是一种非常经典的编码方式,实现起来也很简单,在实际的笔试面试过程中有可能会遇到,这里介绍一下它的原理和一个使用优先队列的实现版本。 一 编码原理 哈夫曼编码是一种可变长的...
14、哈夫曼树 88 BinTreeNode.h 88 BinaryTree.h 89 MinHeap.h 92 Huffman.h 95 Test.cpp 96 15、树 97 QueueNode.h 97 LinkQueue.h 97 TreeNode.h 100 Tree.h 100 test.cpp 110 16、B+树 111 BTreeNode.h 111 BTree...
14、哈夫曼树 149 BinTreeNode.h 149 BinaryTree.h 151 MinHeap.h 156 Huffman.h 161 Test.cpp 163 15、树 164 QueueNode.h 164 LinkQueue.h 165 TreeNode.h 169 Tree.h 170 test.cpp 187 16、B+树 189 BTreeNode.h ...
14、哈夫曼树 166 BinTreeNode.h 166 BinaryTree.h 169 MinHeap.h 174 Huffman.h 180 Test.cpp 182 15、树 183 QueueNode.h 183 LinkQueue.h 184 TreeNode.h 189 Tree.h 190 test.cpp 208 16、B+树 210 BTreeNode.h ...
14、哈夫曼树 149 BinTreeNode.h 149 BinaryTree.h 151 MinHeap.h 156 Huffman.h 161 Test.cpp 163 15、树 164 QueueNode.h 164 LinkQueue.h 165 TreeNode.h 169 Tree.h 170 test.cpp 187 16、B+树 189 BTreeNode.h ...
14、哈夫曼树 149 BinTreeNode.h 149 BinaryTree.h 151 MinHeap.h 156 Huffman.h 161 Test.cpp 163 15、树 164 QueueNode.h 164 LinkQueue.h 165 TreeNode.h 169 Tree.h 170 test.cpp 187 16、B+树 189 BTreeNode.h ...
目录 出版说明 献词 简介 第1章 综述 ... 哈夫曼(Huffman)编码 小结 问题 实验 编程作业 第9章 红-黑树 第10章 2-3-4树和外部存储 第11章 哈希表 第12章 堆 第13章 图 第14章 带权图 第15章 应用场合
哈夫曼(Huffman)编码 小结 问题 实验 编程作业 第9章 红-黑树 第10章 2-3-4树和外部存储 第11章 哈希表 第12章 堆 第13章 图 第14章 带权图 第15章 应用场合 附录A 运行专题applet和示例程序 附录B 进一步学习 附录...
哈夫曼(Huffman)编码 小结 问题 实验 编程作业 第9章 红-黑树 第10章 2-3-4树和外部存储 第11章 哈希表 第12章 堆 第13章 图 第14章 带权图 第15章 应用场合 附录A 运行专题applet和示例程序 附录B 进一步学习 ...
哈夫曼(Huffman)编码 小结 问题 实验 编程作业 第9章 红-黑树 本章讨论的方法 平衡树和非平衡树 使用RBTree专题applet 用专题applet做试验 旋转 插入一个新节点 删除 红-黑树的效率 红-黑树的实现...
3、从终端读入字符集大小为n(即字符的个数),逐一输入n个字符和相应的n个权值(即字符出现的频度),建立哈夫曼树,进行编码并且输出。 注:可用C或C++编写。 4、用邻接矩阵或邻接图实现一个有向图的...
哈夫曼(Huffman)编码 小结 问题 实验 编程作业 第9章 红-黑树 本章讨论的方法 平衡树和非平衡树 使用RBTree专题applet 用专题applet做试验 旋转 插入一个新节点 删除 红-黑树的效率 红-黑树的实现...