实验3.1 哈夫曼/译码器

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNODE 100
#define MAXNUM 5000

struct HtNode
{
    int weight;
    int parent,lchild,rchild;
};

typedef struct HtTree
{
    struct HtNode Ht[MAXNODE];
    int root ;
}HtTree,*PHtTree;

HtTree tree;
PHtTree pht = &tree;
char h[5000][500];
int n,i,m;//m:the length of string
char string[100];  //一串要编码的字符
char code[50];      //n个字符
int weight[50];
int _2[300];

void PrintRoot(PHtTree pht, int n);
void PrivateFuc(PHtTree T, int n);
void Select(PHtTree pht,int pos,int *x1,int *x2);
void PrintAll(HtNode *nodes, int root, int n);

void huffman(PHtTree pht,int n,int *w)
{
    int i,x1,x2;
    for(i=0;i<2*n-1;i++)
    {
        pht->Ht[i].parent=-1;
        pht->Ht[i].lchild=-1;
        pht->Ht[i].rchild=-1;
        if(i<n)
            pht->Ht[i].weight=w[i];
        else
            pht->Ht[i].weight=0;
    }
    for(i=0;i<n-1;i++)
    {
        Select(pht,n+i,&x1,&x2);
        pht->Ht[x1].parent=n+i;
        pht->Ht[x2].parent=n+i;
        pht->Ht[n+i].weight=pht->Ht[x1].weight+pht->Ht[x2].weight;
        pht->Ht[n+i].lchild=x1;
        pht->Ht[n+i].rchild=x2;
        pht->root=n+i;
    }
}

void Select(PHtTree pht,int pos,int *x1,int *x2)
{
    int j,m1=MAXNUM,m2=MAXNUM;
    for(j=0;j<pos;j++)
    {
        if(pht->Ht[j].weight<m1&&pht->Ht[j].parent==-1)
        {
            m2=m1;
            *x2=*x1;
            m1=pht->Ht[j].weight;
            *x1=j;
        }
        else if(pht->Ht[j].weight<m2&&pht->Ht[j].parent==-1)
        {
            m2=pht->Ht[j].weight;
            *x2=j;
        }
    }
}

void PrintRoot(PHtTree pht, int n)
{
	if(pht)
	{
		printf("pht root-> %d\n", pht->root);
		PrintAll(pht->Ht, pht->root, 0);
	}
}

void PrintAll(HtNode *nodes, int root, int n)
{
	if(root == -1) return;
	for(int i=0; i<n; ++i) printf("\t");
	printf("pos: %d ; w: %d, %d:%d \n", root, nodes[root].weight, nodes[root].lchild, nodes[root].rchild);
	PrintAll(nodes, nodes[root].lchild, n+1);
	PrintAll(nodes, nodes[root].rchild, n+1);
}

void Traverse(HtNode *nodes, int root, char *pbuffer, int n, int depth)
{
	//printf("root: %d, depth: %d, pbuffer: %s\n", root, depth, pbuffer);
	if( root == -1) return ;
	if( root < n ) strcpy(h[root], pbuffer);

	pbuffer[depth] = '0';
	Traverse(nodes, nodes[root].lchild,pbuffer, n, depth+1);
	pbuffer[depth] = '1';
	Traverse(nodes, nodes[root].rchild,pbuffer, n, depth+1);
	pbuffer[depth] = 0;
}

void Huffmancoding(PHtTree T, int n)
{
	char *pbuffer = (char *)calloc(2500, sizeof(char));
	Traverse(T->Ht, T->root ,pbuffer, n, 0);
	free(pbuffer);
}

int main()
{
    scanf("%d",&n);
    for( i=0; i<n; ++i) scanf("%s",code+i);
    for( i=0; i<n; ++i) _2[code[i]] = i;
    for( i=0; i<n; ++i) scanf("%d", weight+i);
    scanf("%s", string);
    m=strlen(string);

    huffman(pht,n,weight);
    //PrintAll(pht, 1241524);
	Huffmancoding(pht, n);

	for(i=0;i<m;i++)
	{
        printf("%s", h[_2[string[i]]]);
	}
	printf("\n");
	printf("%s",string);
	printf("\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值