霍夫曼树

作者在 2006-12-13 19:37:00 发布以下内容
#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);
        printf("%d ",Weight);
        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;
    unsigned int c;
    unsigned int f;
    int start;

    if(Count<=1) return;
    TotalLength=Count*2-1;
    (*HT)=(HuffmanTree)malloc((TotalLength+1)*sizeof(HTNode));

    p=((*HT)++);
    for(i=1;i<=Count;i++)
    {
        (*HT).Parent=0;
        (*HT).rChild=0;
        (*HT).lChild
作业草稿 | 阅读 2623 次
文章评论,共0条
游客请输入验证码