当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>C++进阶与实例

霍夫曼树编码的实现

      #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>

    typedef strUCt
    {
        unsigned int Weight;
        unsigned int Parent;
        unsigned int lChild;
        unsigned int rChild;
    }HTNode,*HuffmanTree;

    typedef char **HuffmanCode;

    int LookFor(char *str,char letter,int count);
    void OutputWeight(char *Data,int Length,
                      char **WhatLetter,
                      int **Weight,int *Count);
    void HuffmanCoding(HuffmanTree *HT,
                       HuffmanCode *HC,
                       int *Weight,
                       int Count);
    void Select(HuffmanTree HT,int Count,int *s1,int *s2);
    int main()
    {
        HuffmanTree HT;
        HuffmanCode HC;
        char Data[100];
        char *WhatLetter;
        int *Weight;
        int Count;
        int i;

        printf("Please input the line:");
        /* Example: aaaaaaaaaaaaaabcccccc*/
       scanf("%s",Data);
        printf("\n");

        OutputWeight(Data,strlen(Data),
                     &WhatLetter,
                     &Weight,
                     &Count);

        HuffmanCoding(&HT,&HC,Weight,Count);

        printf(" Letter Weight Code\n");
        for(i=0;i<Count;i++)
        {
            printf(" %c ",WhatLetter[i]);
            printf("%d ",Weight[i]);
            printf("%s\n",HC[i+1]);
        }
        printf("\n");
        getch();
        return 0;
    }

    void HuffmanCoding(HuffmanTree *HT,
                       HuffmanCode *HC,
                       int *Weight,
                       int Count)
    {
        int i;
        int s1,s2;
        int TotalLength;
        HuffmanTree p;
        char* cd;

共2页 首页 上一页 1 2 下一页 尾页 跳转到
相关内容
赞助商链接