haffuman 编码c++实现
来源:互联网 发布:神州泰岳 知乎 编辑:程序博客网 时间:2024/04/25 23:16
为了考研很久没有搞技术了,现在海大考上已经很多天了,无意看到以前考研上机做的考研真题,顺便就粘贴上来冒个泡.
#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std ;class HaffumanNode{public: int wight ; //权值 char ch ; HaffumanNode *left ; HaffumanNode *right ; HaffumanNode(char _ch,int _wight,HaffumanNode *_left = NULL,HaffumanNode *_right = NULL) ; HaffumanNode(const HaffumanNode &haffuman) ; ~HaffumanNode() ; bool isLeaf() ;} ;HaffumanNode::HaffumanNode(const HaffumanNode &haffuman) { wight = haffuman.wight ; ch = haffuman.ch ; left = haffuman.left ; right = haffuman.right ;}HaffumanNode::HaffumanNode(char _ch,int _wight,HaffumanNode *_left,HaffumanNode *_right){ ch = _ch ; wight = _wight ; left = _left ; right = _right ;}//处理left和rightHaffumanNode::~HaffumanNode(){}//使用权重作为比较函数的参考值,按降序排bool HaffumanCompare(const HaffumanNode &left,const HaffumanNode &right){ return (left.wight > right.wight) ;}//判断本节点是叶子节点bool HaffumanNode::isLeaf(){ return (left == NULL && right == NULL) ;}vector<HaffumanNode> ListHaffuman ; //haffuman源处理序列//构建哈夫曼源序列void GetHaffumanNodeList(string str){ int strLen ; int i ; vector<HaffumanNode>::iterator it ; HaffumanNode *p ; strLen = str.length() ; for(i = 0 ; i < strLen ;i++) { for(it = ListHaffuman.begin() ; it != ListHaffuman.end() ; it++) { if(it->ch == str[i]) //如果存在了这个字符,权值+1 { it->wight++ ; break ; } } if(it == ListHaffuman.end()) //如果这个字符没有存在,加入容器 { p = new HaffumanNode(str[i],1) ; ListHaffuman.push_back(*p) ; } }}/*显示哈夫曼源字符和权重*/void ShowListHaffuman(void){ vector<HaffumanNode>::iterator it ; cout<<"字符"<<'\t'<<"权重"<<endl ; for(it = ListHaffuman.begin() ;it != ListHaffuman.end() ; it++) { cout<<it->ch<<'\t'<<it->wight<<endl ; } cout<<endl ;}/*打印各叶子节点的编码*/void PrintLeafCode(HaffumanNode &tree,string bincode){ //中序遍历 if(tree.left != NULL) { bincode += '0' ; PrintLeafCode(*tree.left,bincode) ; bincode = bincode.substr(0,bincode.length() - 1) ; } if(tree.isLeaf()) { cout<<tree.ch<<": "<<bincode.c_str()<<endl ; } if(tree.right != NULL) { bincode += '1' ; PrintLeafCode(*tree.right,bincode) ; bincode = bincode.substr(0,bincode.length() - 1) ; }}/*生成哈夫曼树*/void CreatHaffumanTree(vector<HaffumanNode> &ListHaffuman){ int vecSize ; while((vecSize = ListHaffuman.size()) > 1) //如果源序列存在1个以上的节点,则继续构造哈夫曼树 { sort(ListHaffuman.begin(),ListHaffuman.end(),HaffumanCompare) ; //按降序排 HaffumanNode *Leftnode = new HaffumanNode( ListHaffuman[vecSize - 2]) ; //取出最后两个小的 HaffumanNode *Rightnode = new HaffumanNode(ListHaffuman[vecSize - 1]) ; ListHaffuman.pop_back() ; //删除取出后的两个小的 ListHaffuman.pop_back() ; if(Leftnode->wight > Rightnode->wight) //使左节点永远比由节点的权重小 { swap(*Leftnode,*Rightnode) ; } //构造父亲节点 HaffumanNode *father = new HaffumanNode('*',(Leftnode->wight + Rightnode->wight),Leftnode,Rightnode) ; ListHaffuman.push_back(*father) ; //将父节点放入源序列 } //已经完成哈夫曼树的构建 string bincode ; HaffumanNode *tree = new HaffumanNode(ListHaffuman.front()) ; PrintLeafCode(*tree,bincode) ; //遍历哈夫曼树}int main(){ char str[50] ; gets(str) ; GetHaffumanNodeList(str) ; //创建haffuman源序列 sort(ListHaffuman.begin(),ListHaffuman.end(),HaffumanCompare) ; //降序排列 ShowListHaffuman() ; //显示字符和权重 cout<<endl ; cout<<endl ; CreatHaffumanTree(ListHaffuman) ; return 0 ;}
0 0
- haffuman 编码c++实现
- 数据结构课程设计(Haffuman编码)
- 基于哈夫曼(haffuman)算法的文件压缩的实现(C语言
- (转)基于哈夫曼(haffuman)算法的文件压缩的实现(C语言)
- haffuman树
- Huffman编码C实现
- BASE64编码 C实现
- c 实现哈夫曼编码
- C 实现 哈夫曼编码
- c语言实现哈夫曼编码
- c程序实现香农编码
- C语言实现Base64编码
- C语言实现Base64编码
- 哈弗曼编码C语言实现
- c语言实现哈夫曼编码
- C语言实现哈夫曼编码
- 行程编码c语言实现
- C语言实现base64编码
- 查询、关键字
- 在线购物领域类图
- js适配屏幕大小
- java设置模式--单例模式
- Date对象之将时间转为毫秒数的方法对比
- haffuman 编码c++实现
- MyEclipse中Web项目的重命名问题
- Ubuntu下安装单机版Storm运行环境
- poj 3264 Balanced Lineup (RMQ算法 模板题)
- 程序员面试金典——高度最小的BST
- 给EditText的drawableRight属性的图片设置点击事件
- 简单的爬取豆瓣纸书名与简介和图片
- 动态规划汇总
- [BZOJ1218][HNOI2003]激光炸弹(扫描线+线段树)