#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实现的更多相关文章

  1. Haffman编码(haffman树)

    Haffman编码 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 哈弗曼编码大家一定很熟悉吧(不熟悉也没关系,自己查去...).现在给你一串字符以及它们所对应的权值 ...

  2. 哈夫曼(huffman)树和哈夫曼编码

    哈夫曼树 哈夫曼树也叫最优二叉树(哈夫曼树) 问题:什么是哈夫曼树? 例:将学生的百分制成绩转换为五分制成绩:≥90 分: A,80-89分: B,70-79分: C,60-69分: D,<60 ...

  3. java实现Haffman编码

    1.先创建一个树节点类(泛型类),为了方便使用集合的排序方法,泛型类要实现泛型接口Comparable,代码如下 package com.hjp.huffman; /** * Created by J ...

  4. 文件压缩小项目haffman压缩

    文件压缩的原理: 文件压缩总体可以分为有损压缩和无损压缩两类,有损压缩是指对mp3等格式的文件,忽略一些无关紧要的信息,只保留一些关键的信息,但并不因此影响用户对于这些mp3格式文件的体验度,无损压缩 ...

  5. Huffman Tree

    哈夫曼(Huffman)树又称最优二叉树.它是一种带权路径长度最短的树,应用非常广泛. 关于Huffman Tree会涉及到下面的一些概念: 1. 路径和路径长度路径是指在树中从一个结点到另一个结点所 ...

  6. ZJOI2019一轮停课刷题记录

    Preface 菜鸡HL终于狗来了他的省选停课,这次的时间很长,暂定停到一试结束,不过有机会二试的话还是可以搞到4月了 这段时间的学习就变得量大而且杂了,一般以刷薄弱的知识点和补一些新的奇怪技巧为主. ...

  7. [数据结构] 大纲 - Stan Zhang 数据结构速通教程

    * 注: 本文/本系列谢绝转载,如有转载,本人有权利追究相应责任. 2019年4月8日 P1.1 链表 Link:https://www.cnblogs.com/yosql473/p/10727471 ...

  8. JPEG编码(二)

    来自CSDN评论区http://bbs.csdn.net/topics/190980 1. 色彩模型 JPEG 的图片使用的是 YCrCb 颜色模型, 而不是计算机上最常用的 RGB. 关于色彩模型, ...

  9. c++实验8 哈夫曼编码-译码器

    哈夫曼编码-译码器 此次实验的注释解析多加不少---若对小伙伴们有帮助 希望各位麻烦点个关注 多谢 1.哈夫曼树构造算法为: (1)由给定的n个权值{w1,w2,…,wn}构造n棵只有根结点的二叉树, ...

随机推荐

  1. HTML5 文件域+FileReader 读取文件并上传到服务器(三)

    一.读取文件为blob并上传到服务器 HTML <div class="container"> <!--读取要上传的文件--> <input type ...

  2. java 反射调用支付SDK

    在android开发中会遇到各种SDK的接入,很是麻烦.最初在想能不能把所有的SDK都 融合到一个当中,发现有点异想天开.但是也可以解决SDK资源不小心没有引入,导致程序调用接口崩溃问题.经过查资料, ...

  3. ui线程和后台线程异步

    private void Button_Click(object sender, RoutedEventArgs e) { CreateElementOnSeperateThread(() => ...

  4. 在使用Kettle的集群排序中 Carte的设定——(基于Windows)

    本片文章主要是关于使用Kettle的UI界面: Spoon来实现基于集群的对数据库中的数据表数据进行排序的试验. 以及在实验过程中所要开启的Carte服务的一些配置文件的设置, 还有基于Windows ...

  5. C#和asp.net中链接数据库中 参数的几种传递方法

    #region 参数传递方法第一种 //参数设置方法(第一种) //SqlParameter sp = new SqlParameter("@Name", str_Name); / ...

  6. 在往oracle中插数据时,如何处理excel读取的时间空值

    //若从excel中读取的时间值为空值时,做如下转换 string YDKGSJ = string.Empty; if (dbdata.Rows[i]["约定开工时间"].ToSt ...

  7. 【转】WF4.0 (基础篇)

    转自:http://www.cnblogs.com/foundation/category/215023.html 作者:WXWinter  ——  兰竹菊梅★春夏秋冬☆ —— wxwinter@16 ...

  8. 【POJ3580】【splay版】SuperMemo

    Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...

  9. javascript中的基本数据类型

    在javascipt中有五大基本数据类型,列表如下: 1.数字 他们又又包括(正负整数,浮点数)十进制数,十六进制数与八进制数,指数和特殊数值NaN,(Infinity,-Infinity)正负无穷 ...

  10. 去除后台ckeditor的style="...."的样式

    .cnt_text .text img {/*width :auto !important;*/height :auto !important; max-width:660px;} 高度自适应,高度让 ...