对一串字符进行huffman编码并解码

对一串字符进行huffman编码并解码,第1张

#include <stdlib.h>

#include <iostream.h>

#include <stdio.h>

#include <string.h>

#define OVERFLOW -1

typedef struct

{

char letter

int weight

int parent

int lchild

int rchild

}HTNode,*HuffmanTree

typedef char * *HuffmanCode

void Select(HuffmanTree &HT,int i,int &s1,int &s2)

{

/*选择森林中,根结点的权值最小和次小的两个树,

*将其根结点的下标号记入s1和s2中

*/

int j, k

for(k = 1k <ik++)

{

if(HT[k].parent != NULL)

continue

s1 = k/*init the number*/

break

}

for(j = 1j <ij++)

{

if(HT[j].parent != NULL)

continue

if(HT[j].weight <HT[s1].weight)

s1 = j

}

for(k = 1k <= ik++)

{

if(HT[k].parent != NULL || k == s1)

continue

s2 = k

break

}

for(j = 1j <ij++)

{

if(HT[j].parent != NULL)

continue

if(HT[j].weight <= HT[s2].weight &&j != s1)

s2 = j

}

}

void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,char *zi,int *w,int n)

{

HuffmanTree p

int m,i,s1,s2,f,c

int Istart = 1

char *cd

if(n <= 1)

return

m = 2*n-1

if(!(HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode))))

exit(OVERFLOW)

for(p=HT+1,i=1i<=n++i,++zi,++p,++w)

{

/*生成独立的森林*/

p->parent = NULL

p->letter = *zi

p->lchild = NULL

p->rchild = NULL

p->weight = *w

}

for(i<=m++i,++p)

{

(*p).weight=0

(*p).parent=0

(*p).lchild=0

(*p).rchild=0

}

for(i=n+1i<=m++i)

{

Select(HT,i-1,s1,s2)

HT[s1].parent=i

HT[s2].parent=i

HT[i].lchild=s1

HT[i].rchild=s2

HT[i].weight=HT[s1].weight+HT[s2].weight

}

HC=(HuffmanCode)malloc((n+1)*sizeof(char *))

cd=(char*)malloc(n*sizeof(char))/*临时的code存储*/

cd[n-1]='\0'

for(i=1i<=n++i)

{

Istart = n - 1

/*按已生成的哈夫曼树,得到各个字符的哈夫曼编码

*/

for(c = i, f = HT[i].parentf != 0c = f, f = HT[f].parent)

if(HT[f].lchild == c)

cd[--Istart] = '0'

else

cd[--Istart] = '1'

HC[i] = (char *)malloc((n - Istart) * sizeof(char))

strcpy(HC[i], &cd[Istart])

}

free(cd)

}

void main()

{

HuffmanTree HT

HuffmanCode HC

int i,j,yu

char zi[9]={'A','B','C','D','E','F','G','H'}

int w[100]

char z

char c[100]

z='A'

cout<<endl

for(i=0i<=7i++)

{

cout<<"please input the weight for "<<z<<":"

cin>>w[i]

z++

}

HuffmanCoding(HT,HC,zi,w,8)

cout<<endl

cout<<"char weight huffmancode"<<endl

for(i=1i<=8i++)

cout<<HT[i].letter<<" "<<HT[i].weight<<" "<<HC[i]<<endl

cout<<"please input the text:"

cin>>c

cout<<"The code is:"

for(i=0i <strlen(c) i++)

/*根据字符的哈夫曼编码,将输入的文本(变量c表示的)翻译成电码

*/

cout<<HC[(c[i] - 'A' + 1)]

cout<<endl

cout<<"Enter the code:"

cin>>c

j=strlen(c)

yu=15

i=1

cout<<"The text is:"

while(i <= j)

{

while(HT[yu].lchild != 0)/*因为是完全二叉树*/

{

if(c[i-1] == '0')

{

/*用哈夫曼树,将输入的电码(变量c表示的)翻译成文本,

说明:变量名c在程序中

*/

yu = HT[yu].lchild

i++

continue

}

if(c[i-1]== '1')

{

yu=HT[yu].rchild

i++

continue

}

}

/*显示由部分电码译码得到的字符,并准备对后面的电码进行译码*/

cout<<HT[yu].letter

yu = 15

}

cout<<endl

}

Matlab自带Huffman函数(ps:你拼写错了)

huffmandeco Huffman decoder

huffmandict Generate Huffman code dictionary for a source with known probability model

huffmanenco Huffman encoder

密码生成:

symbols = [1 2 3]% Data symbols

p = [0.1 0.1 0.8]% Probability of each data symbol

dict = huffmandict(symbols,p) % Create the dictionary.

dict{1,:} % Show one row of the dictionary.

加密解密:

sig = repmat([3 3 1 3 3 3 3 3 2 3],1,50)% Data to encode

symbols = [1 2 3]% Distinct data symbols appearing in sig

p = [0.1 0.1 0.8]% Probability of each data symbol

dict = huffmandict(symbols,p)% Create the dictionary.

hcode = huffmanenco(sig,dict)% Encode the data.

dhsig = huffmandeco(hcode,dict)% Decode the code.

/**********************************************************************

 * Name:   哈夫曼编码源代码

 * 实现过程:着先通过 HuffmanTree() 函数构造哈夫曼树,然后在主函数 main()中

 *           自底向上开始(也就是从数组序号为零的结点开始)向上层层判断,若在

 *           父结点左侧,则置码为 0,若在右侧,则置码为 1。最后输出生成的编码。

 ************************************************************************/

#include <stdio.h>

#include<stdlib.h>

 

#define MAXBIT      100

#define MAXVALUE  10000

#define MAXLEAF     30

#define MAXNODE    MAXLEAF*2 -1

 

typedef struct 

{

    int bit[MAXBIT]

    int start

} HCodeType        /* 编码结构体 */

typedef struct

{

    int weight

    int parent

    int lchild

    int rchild

    int value

} HNodeType        /* 结点结构体 */

 

/* 构造一颗哈夫曼树 */

void HuffmanTree (HNodeType HuffNode[MAXNODE],  int n)

    /* i、j: 循环变量,m1、m2:构造哈夫曼树不同过程中两个最小权值结点的权值,

        x1、x2:构造哈夫曼树不同过程中两个最小权值结点在数组中的序号。*/

    int i, j, m1, m2, x1, x2

    /* 初始化存放哈夫曼树数组 HuffNode[] 中的结点 */

    for (i=0 i<2*n-1 i++)

    {

        HuffNode[i].weight = 0//权值 

        HuffNode[i].parent =-1

        HuffNode[i].lchild =-1

        HuffNode[i].rchild =-1

        HuffNode[i].value=i //实际值,可根据情况替换为字母  

    } /* end for */

 

    /* 输入 n 个叶子结点的权值 */

    for (i=0 i<n i++)

    {

        printf ("Please input weight of leaf node %d: \n", i)

        scanf ("%d", &HuffNode[i].weight)

    } /* end for */

 

    /* 循环构造 Huffman 树 */

    for (i=0 i<n-1 i++)

    {

        m1=m2=MAXVALUE     /* m1、m2中存放两个无父结点且结点权值最小的两个结点 */

        x1=x2=0

        /* 找出所有结点中权值最小、无父结点的两个结点,并合并之为一颗二叉树 */

        for (j=0 j<n+i j++)

        {

            if (HuffNode[j].weight < m1 && HuffNode[j].parent==-1)

            {

                m2=m1 

                x2=x1 

                m1=HuffNode[j].weight

                x1=j

            }

            else if (HuffNode[j].weight < m2 && HuffNode[j].parent==-1)

            {

                m2=HuffNode[j].weight

                x2=j

            }

        } /* end for */

            /* 设置找到的两个子结点 x1、x2 的父结点信息 */

        HuffNode[x1].parent  = n+i

        HuffNode[x2].parent  = n+i

        HuffNode[n+i].weight = HuffNode[x1].weight + HuffNode[x2].weight

        HuffNode[n+i].lchild = x1

        HuffNode[n+i].rchild = x2

 

        printf ("x1.weight and x2.weight in round %d: %d, %d\n", i+1, HuffNode[x1].weight, HuffNode[x2].weight)  /* 用于测试 */

        printf ("\n")

    } /* end for */

  /*  for(i=0i<n+2i++)

    {

        printf(" Parents:%d,lchild:%d,rchild:%d,value:%d,weight:%d\n",HuffNode[i].parent,HuffNode[i].lchild,HuffNode[i].rchild,HuffNode[i].value,HuffNode[i].weight)

                  }*///测试 

} /* end HuffmanTree */

 

//解码 

void decodeing(char string[],HNodeType Buf[],int Num)

{

  int i,tmp=0,code[1024]

  int m=2*Num-1

  char *nump

  char num[1024]

  for(i=0i<strlen(string)i++)

  {

   if(string[i]=='0')

  num[i]=0        

  else

  num[i]=1                    

  } 

  i=0

  nump=&num[0]

  

 while(nump<(&num[strlen(string)]))

 {tmp=m-1

  while((Buf[tmp].lchild!=-1)&&(Buf[tmp].rchild!=-1))

  {

  

   if(*nump==0)

   {

     tmp=Buf[tmp].lchild           

   } 

   else tmp=Buf[tmp].rchild

   nump++

        

  } 

  

  printf("%d",Buf[tmp].value)                                  

 }

 

  

}

 

 

int main(void)

{

    

    HNodeType HuffNode[MAXNODE]            /* 定义一个结点结构体数组 */

    HCodeType HuffCode[MAXLEAF],  cd       /* 定义一个编码结构体数组, 同时定义一个临时变量来存放求解编码时的信息 */

    int i, j, c, p, n

    char pp[100]

    printf ("Please input n:\n")

    scanf ("%d", &n)

    HuffmanTree (HuffNode, n)

   

    

    for (i=0 i < n i++)

    {

        cd.start = n-1

        c = i

        p = HuffNode[c].parent

        while (p != -1)   /* 父结点存在 */

        {

            if (HuffNode[p].lchild == c)

                cd.bit[cd.start] = 0

            else

                cd.bit[cd.start] = 1

            cd.start--        /* 求编码的低一位 */

            c=p                    

            p=HuffNode[c].parent    /* 设置下一循环条件 */

        } /* end while */

        

        /* 保存求出的每个叶结点的哈夫曼编码和编码的起始位 */

        for (j=cd.start+1 j<n j++)

        { HuffCode[i].bit[j] = cd.bit[j]}

        HuffCode[i].start = cd.start

    } /* end for */

    

    /* 输出已保存好的所有存在编码的哈夫曼编码 */

    for (i=0 i<n i++)

    {

        printf ("%d 's Huffman code is: ", i)

        for (j=HuffCode[i].start+1 j < n j++)

        {

            printf ("%d", HuffCode[i].bit[j])

        }

        printf(" start:%d",HuffCode[i].start)

       

        printf ("\n")

        

    }

    printf("Decoding?Please Enter code:\n")

    scanf("%s",&pp)

    decodeing(pp,HuffNode,n)

    getch()

    return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11381695.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存