哈夫曼树的构建以及哈夫曼编码的输出

实验目的:哈夫曼树的构建以及哈夫曼编码的输出

实验思想:1.先构建一个哈夫曼树

          2.每个叶子节点为结点的名称

          3.然后进行遍历

          4.向左为0 向右为1

            5.存入一个字符数组中 最后在输出

①   头文件的构建:

typedef struct

{

     int weight;

     intparent,lchild,rchild;

}HTNode,*HuffmanTree;

typedef char * * HuffmanCode ;

voidHuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,int*w,int n);

voidSelect(HuffmanTree HT,int m,int &s1,int&s2);

②   函数文件的内容:

 #include "stdafx.h"

#include"Huffman.h"

voidHuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,int*w,int n)

{

    if(n<=1)return;

    int m=2*n-1;

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

    int i;

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

    {

       HT[i].weight=w[i];

       HT[i].parent=HT[i].rchild=HT[i].lchild=0;

    }

    for(;i<=m;i++)

    {

       HT[i].weight=HT[i].parent=HT[i].rchild=HT[i].lchild=0;

    }

    int s1=0,s2=0;

    for(i=n+1;i<=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*));

    char *cd;

    cd=(char *)malloc(n*sizeof(char));

    cd[n-1]='\0';

    int start;

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

    {

       start=n-1;

       int c=0;int f=0;

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

       {

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

               cd[--start]='0';

           else

              cd[--start]='1';

       }

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

       strcpy(HC[i],&cd[start]);

    }

    free(cd);

}

 

voidSelect(HuffmanTree HT,int n,int &s1,int&s2)  //选择函数

    int minn=99999998,maxx=99999999; 

   s1=s2=0; 

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

    { 

        if(HT[i].parent==0) 

        { 

           if(HT[i].weight

           { 

               minn=HT[i].weight;s1=i;                } 

           } 

        } 

        intt=HT[s1].weight; 

        HT[s1].weight=maxx; 

        minn=99999998; 

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

        { 

            if(HT[i].parent==0) 

            { 

                if(HT[i].weight

                { 

                    minn=HT[i].weight; 

                    s2=i; 

                } 

            } 

        } 

        HT[s1].weight=t; 

主函数文件:

// huffman.cpp : Defines theentry point for the console application.

//

 

#include "StdAfx.h"

#include "Huffman.h"

intmain(int argc, char*argv[])

{

    //printf("Hello World!\n");

    HuffmanTreeHT;

    HuffmanCodeHC;

    char c[100];

    int n,i;

    printf("输入你想要输入的结点数目:\n");

    scanf_s("%d",&n);

    fflush(stdin);

    printf("\n");

    printf("<---------------------------------------------------------------------->\n");

    int a[100];

    int *w;

    w=a;

    printf("输入%d个结点的权值: \n",n);

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

    {

       printf("输入第%d结点的名称以及权值:",i);

       scanf("%c %d",&c[i],&a[i]);

       fflush(stdin);

    }

    w=a;

   HuffmanCoding(HT,HC,w,n);

    printf("<---------------------------------------------------------------------->\n");

       printf("哈夫曼编码的输出:\n");

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

    {

 

       printf("%c:%s",c[i],HC[i]);

       printf("\n");

    }

    printf("<---------------------------------------------------------------------->\n");

   system("pause");

    return 0;

}

你可能感兴趣的:(数据结构)