C++实现哈夫曼编码
作者:南小呗 时间:2022-07-20 20:39:01
本文实例为大家分享了C++实现哈夫曼编码的具体代码,供大家参考,具体内容如下
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int Max = 300;
class tree{
public:
char s;
int num;
tree *left;
tree *right;
tree(){
s= '!';
num = 0;
left = 0;
right = 0;
}
tree(char a,int n,tree* p1,tree* p2){
s = a;
num = n;
left = p1;
right = p2;
}
};
vector<tree *> open;
/*********************************
**中序遍历输出各节点及其哈夫曼编码
*********************************/
void inorder(tree *t,string s){
if(t != 0){
inorder(t->left,s+'0');
if(t->s != '!')
cout<<t->s<<":"<<s<<endl;
inorder(t->right,s+'1');
}
}
int main(){
int a[Max];
for(int i = 0;i < Max;i++)
a[i] = 0; //初始化数组
string s;
cout<<"请输入字符串:";
cin>>s;
vector<char> v;
vector<char>::iterator vit;
for(int i = 0;i < s.length();i ++){
a[s[i]]++; //确定每个字符出现的次数(频率)
vit = find(v.begin(),v.end(),s[i]);
if(vit == v.end()) //相同的字符只保留一个
v.push_back(s[i]);
}
for(int i = 0;i < v.size();i ++){
tree *n = new tree();
n->s = v[i];
n->num = a[v[i]];
open.push_back(n); //存入open表中
}
/************************
**
**构造哈夫曼树
**
*************************/
tree *root;
while(open.size() != 1){
tree *min1,*min2; //min1,min2是当前open表中num值最小的节点
int sit1,sit2;
min1 = open.front();
sit1 = 0;
for(int i = 0;i < open.size();i++){
if(open[i]->num < min1->num){
min1 = open[i];
sit1 = i;
}
}
open.erase(open.begin()+sit1);
min2 = open.front();
sit2 = 0;
for(int i = 0;i < open.size();i++){
if(open[i]->num < min2->num){
min2 = open[i];
sit2 = i;
}
}
open.erase(open.begin()+sit2);
tree *t = new tree('!',min1->num + min2->num,min1,min2); //构造新节点,左右指针指min1和min2
open.push_back(t); //存入open表中
root = t;
}
cout<<"它的哈夫曼编码为:"<<endl;
string s1 = "";
inorder(root,s1);
return 0;
}```
来源:https://blog.csdn.net/TKFEET/article/details/89394104
标签:C++,哈夫曼编码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#图像处理之图像平移的方法
2021-12-16 08:38:37
Java实现将类数据逐行写入CSV文件的方法详解
2023-02-27 17:11:11
java 使用POI合并两个word文档
2022-09-30 12:22:54
springboot+mybatis配置控制台打印sql日志的方法
2023-12-15 15:40:55
![](https://img.aspxhome.com/file/2023/0/97940_0s.jpg)
轻松学习C#的ArrayList类
2022-08-07 18:53:34
Android编程入门之HelloWorld项目目录结构分析
2022-07-23 23:34:40
![](https://img.aspxhome.com/file/2023/1/114361_0s.png)
Java邮件发送程序(可以同时发给多个地址、可以带附件)
2022-09-07 20:08:40
VsCode搭建Java开发环境的方法
2023-06-17 13:29:19
![](https://img.aspxhome.com/file/2023/9/130919_0s.png)
Java 回调callback举例详解
2023-11-11 16:25:09
![](https://img.aspxhome.com/file/2023/2/59592_0s.png)
JNDI简介_动力节点Java学院整理
2023-04-20 03:19:23
Java权重随机的实现方法
2021-10-05 14:27:50
java 服务器接口快速开发之servlet详细教程
2022-11-07 09:37:28
![](https://img.aspxhome.com/file/2023/0/116140_0s.jpg)
Spring实现一个简单的SpringIOC容器
2023-02-06 21:03:43
![](https://img.aspxhome.com/file/2023/7/83447_0s.png)
C#实现对字符串进行大小写切换的方法
2021-07-24 03:30:30
java并发编程之深入理解Synchronized的使用
2023-10-11 09:21:13
![](https://img.aspxhome.com/file/2023/9/63289_0s.png)
java 三种将list转换为map的方法详解
2023-09-13 03:35:39
VSCode配置C语言环境的方法
2022-11-07 18:47:36
![](https://img.aspxhome.com/file/2023/0/105170_0s.png)
C#高级静态语言效率利器之泛型详解
2023-01-24 09:18:02
解决 INSTALL FAILED CONFLICTING PROVIDER的问题方法
2023-01-31 11:42:49
![](https://img.aspxhome.com/file/2023/6/96016_0s.png)
解读java try catch 异常后还会继续执行吗
2022-05-11 01:25:24
![](https://img.aspxhome.com/file/2023/7/66677_0s.png)