haffman树c实现
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 100
#define M 2*N-1
typedef char *HuffmanCode[2*M];
typedef struct
{
int weight;//权值
int parent;//父节点
int Lchild;//左
int Rchild;//右
}HTNode,Huffman[M+1];//huffman树
typedef struct Node
{
int weight;//叶子权值
char c;//叶子
int num;//叶子的2进制码长度
}WNode,WeightNode[N];
void CreatWeight(char ch[],int *s,WeightNode CW,int *p)//生成叶子节点字符与权值
{
int i,j,k;
int tag;
*p=0;//叶子节点数
for(i=0;ch[i]!='\0';i++)//记录字符个数放入CW
{
tag=1;
for(j=0;j<i;j++)
if(ch[j]==ch[i])
{
tag=0;
break;
}
if(tag)
{
CW[++*p].c=ch[i];
CW[*p].weight=1;
for(k=i+1;ch[k]!='\0';k++)
{
if(ch[i]==ch[k])
CW[*p].weight++;//权值++
}
}
}
*s=i;//字符串长
}
void CreateHuffmanTree(Huffman ht,WeightNode w,int n)//建立huffmantree
{
int i,j;
int s1,s2;
for(i=1;i<=n;i++)//初始化
{
ht[i].weight=w[i].weight;
ht[i].parent=0;
ht[i].Lchild=0;
ht[i].Rchild=0;
}
for(i=n+1;i<=2*n-1;i++)
{
ht[i].weight=0;
ht[i].parent=0;
ht[i].Lchild=0;
ht[i].Rchild=0;
}
for(i=n+1;i<=2*n-1;i++)
{
for(j=1;j<=i-1;j++)
if(!ht[j].parent)
break;
s1=j;//找第一个双亲!=0节点
for(;j<=i-1;j++)
if(!ht[j].parent)
s1=ht[s1].weight>ht[j].weight?j:s1;
ht[s1].parent=i;
ht[i].Lchild=s1;
for(j=1;j<=i-1;j++)
if(!ht[j].parent)
break;
s2=j;//找第二个双亲!=0节点
for(;j<=i-1;j++)
if(!ht[j].parent)
s2=ht[s2].weight>ht[j].weight?j:s2;
// printf("s2=%d\n",s2);
ht[s2].parent=i;
ht[i].Rchild=s2;
// intf("rchild=%d\n",ht[i].Rchild);
ht[i].weight=ht[s1].weight+ht[s2].weight;//权值++
}
}
void CrtHuffmanNodeCode(Huffman ht,char ch[],HuffmanCode h,WeightNode weight,int m,int n)//叶子节点编码
{
int i,c,p,start;
char *cd;
cd=(char*)malloc(n*sizeof(char));
cd[n-1]='\0';//末尾=0
for(i=1;i<=n;i++)
{
start=n-1;//cd从末尾开始
c=i;
p=ht[i].parent;//p在n+1——2n-1
while(p)//沿父方向遍历至0
{
start--;//向前
if(ht[p].Lchild==c)//与lchild相同=0
cd[start]='0';
else//=1
cd[start]='1';
c=p;
p=ht[p].parent;
}
weight[i].num=n-start;//二进制码长度
h[i]=(char*)malloc((n-start)*sizeof(char));
strcpy(h[i],&cd[start]);//二进制字符复制到h
}
free(cd);
//system("pause");
}
void CrtHuffmanCode(char ch[],HuffmanCode h,HuffmanCode hc,WeightNode weight,int n,int m)//所有字符编码
{
int i,k;
for(i=0;i<m;i++)
{
for(k=1;k<=n;k++)//从weight[k].c中找与ch[i]相等下标k*
if(ch[i]==weight[k].c)
break;
hc[i]=(char*)malloc((weight[k].num)*sizeof(char));
strcpy(hc[i],h[k]);//复制二进制码
}
}
void TrsHuffmanTree(Huffman ht,WeightNode w,HuffmanCode hc,int n,int m)//译码
{
int i=0,j,p;
while(i<m)
{
p=2*n-1;//从父亲遍历到叶子
for(j=0;hc[i][j]!='\0';j++)
{
if(hc[i][j]=='0')
p=ht[p].Lchild;
else
p=ht[p].Rchild;
}
printf("%c",w[p].c);
i++;
}
}
void main()
{
int i;
int n=0;//叶子数
int m=0;//ch长度
char ch[N];//输入字符
Huffman ht;//huffman二叉树
HuffmanCode h;//叶子编码
HuffmanCode hc;//所有节点编码
WeightNode weight;//叶子信息
printf("Huffman\n please intput inf\n");
gets(ch);
CreatWeight(ch,&m,weight,&n);
for(i=1;i<=n;i++)//输出叶子字符和权
printf("%c",weight[i].c);
printf("\nweight");
for(i=1;i<=n;i++)
printf("%d",weight[i].weight);
CreateHuffmanTree(ht,weight,n);
printf("\ti\tweight\tparent\tLChild\tRChild\n");
for(i=1;i<=2*n-1;i++)
printf("\t%d\t%d\t%d\t%d\t%d\n",i,ht[i].weight,ht[i].parent,ht[i].Lchild,ht[i].Rchild);
CrtHuffmanNodeCode(ht,ch,h,weight,m,n);
for(i=1;i<=n;i++)
{
printf("\t%c ",weight[i].c);
printf("%s\n",h[i]);
}
CrtHuffmanCode(ch,h,hc,weight,n,m);
printf("code");
for(i=0;i<m;i++)
printf("%s",hc[i]);
printf("\n");
TrsHuffmanTree(ht,weight,hc,n,m);
}
haffman树c实现的更多相关文章
- Haffman编码(haffman树)
Haffman编码 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 哈弗曼编码大家一定很熟悉吧(不熟悉也没关系,自己查去...).现在给你一串字符以及它们所对应的权值 ...
- 哈夫曼(huffman)树和哈夫曼编码
哈夫曼树 哈夫曼树也叫最优二叉树(哈夫曼树) 问题:什么是哈夫曼树? 例:将学生的百分制成绩转换为五分制成绩:≥90 分: A,80-89分: B,70-79分: C,60-69分: D,<60 ...
- java实现Haffman编码
1.先创建一个树节点类(泛型类),为了方便使用集合的排序方法,泛型类要实现泛型接口Comparable,代码如下 package com.hjp.huffman; /** * Created by J ...
- 文件压缩小项目haffman压缩
文件压缩的原理: 文件压缩总体可以分为有损压缩和无损压缩两类,有损压缩是指对mp3等格式的文件,忽略一些无关紧要的信息,只保留一些关键的信息,但并不因此影响用户对于这些mp3格式文件的体验度,无损压缩 ...
- Huffman Tree
哈夫曼(Huffman)树又称最优二叉树.它是一种带权路径长度最短的树,应用非常广泛. 关于Huffman Tree会涉及到下面的一些概念: 1. 路径和路径长度路径是指在树中从一个结点到另一个结点所 ...
- ZJOI2019一轮停课刷题记录
Preface 菜鸡HL终于狗来了他的省选停课,这次的时间很长,暂定停到一试结束,不过有机会二试的话还是可以搞到4月了 这段时间的学习就变得量大而且杂了,一般以刷薄弱的知识点和补一些新的奇怪技巧为主. ...
- [数据结构] 大纲 - Stan Zhang 数据结构速通教程
* 注: 本文/本系列谢绝转载,如有转载,本人有权利追究相应责任. 2019年4月8日 P1.1 链表 Link:https://www.cnblogs.com/yosql473/p/10727471 ...
- JPEG编码(二)
来自CSDN评论区http://bbs.csdn.net/topics/190980 1. 色彩模型 JPEG 的图片使用的是 YCrCb 颜色模型, 而不是计算机上最常用的 RGB. 关于色彩模型, ...
- c++实验8 哈夫曼编码-译码器
哈夫曼编码-译码器 此次实验的注释解析多加不少---若对小伙伴们有帮助 希望各位麻烦点个关注 多谢 1.哈夫曼树构造算法为: (1)由给定的n个权值{w1,w2,…,wn}构造n棵只有根结点的二叉树, ...
随机推荐
- dnsever 邮件记录
记录,备忘
- oracle数据库读取操作系统的物理文件-转载,待完善
--源地址不详 --创建目录SQL> create directory dir_xls as '/home/oracle'; Directory created. --给用户授权SQL> ...
- ASP.NET实现二级域名(多用户,多商店)
本人所了解有两种方案,可能还有其的方式,希望大家多多讨论! 基本思路: 1. 域名支持泛解析,即是指:把A记录 *.域名.com 解析到服务器IP,服务器IIS中做绑定,绑定时主机头为空; 2. ...
- SGU 138.Games of Chess
时间限制:0.25s 空间限制:4M 题目: n个朋友在一起按照下面的规则依次下棋:在第一局游戏,n个人中的两个开始下棋.在第二局,第一局胜利的人将跟其他人下棋(也可能还是输了第一局人), 在第三局第 ...
- SGU 188.Factory guard
模拟 code #include <iostream> #include <cstdio> #define LEN 1000 using namespace std; int ...
- Java多线程:Semaphore
Semaphore为并发包中提供用于控制某资源同时可以被几个线程访问的类 实例代码: // 允许2个线程同时访问 final Semaphore semaphore = new Semaphore(2 ...
- 【转】ThinkPHP中数据库操作返回值总结
Thinkphp中的Think\Model类提供了数据库的基本CURD(Create.Update.Read及Delete),通过该类可以很便捷的进行操作.Model类及扩展类主要的方法有: Crea ...
- Nginx fastcgi_param解释
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径 fastcgi_param QUERY_STRI ...
- Python Tutorial 学习(二)--Using the Python Interpreter
Using the Python Interpreter 2.1. Invoking the Interpreter The Python interpreter is usually install ...
- CentOS 6.4编译安装淘宝web服务器Tengine
Tengine 是由淘宝核心系统部基于Nginx开发的Web服务器,它在Nginx的基础上,针对大访问量网站的需求,添加了很多功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,淘宝商城 ...