HDOJ 1053 Huffman编码 自写优先队列的ADT 权当做练习数据结构

//BinHeap.h
#ifndef _BinHeap_H

struct PriorityQueueNode;
struct ElementTypeNode;

typedef struct PriorityQueueNode PQN;
typedef struct PriorityQueueNode *PriorityQueue;

typedef struct ElementTypeNode ETN;
typedef struct ElementTypeNode *ElementType;

/*Methods of Priority Queue*/
PriorityQueue Initialize(int MaxElements);
int IsEmpty(PriorityQueue H);
int IsFull(PriorityQueue H);

/*Warning Type ETN has not been complete*/
void Insert(ETN X, PriorityQueue H);

ETN DeleteMin(PriorityQueue H);
void Destroy(PriorityQueue H);

#endif
//BinHeap.c
#include <stdio.h>
#include "BinHeap.h"
#define MinPQSize 1
#define IrrelevantWord '#'
#define MinKey 0
#define Bin 2

struct PriorityQueueNode
{
	int Capacity;
	int Size;
	ElementType Elements;
};

struct ElementTypeNode
{
	char Word;
	int Key;
};


PriorityQueue Initialize(int MaxElements)
{
	PriorityQueue H;
	
	if(MaxElements < MinPQSize)
		printf("Size Error!PQ size is too small!\n");
	
	H = malloc(sizeof(struct PriorityQueueNode));
	if(H == NULL)
		printf("H Malloc Error!\n");
	
	/*Allocate the array plus one extra for sentinel*/
	H->Elements = malloc( (MaxElements + 1) * sizeof(struct ElementTypeNode));
	if(H->Elements == NULL)
		printf("H->Elements Malloc Error!\n");
	
	H->Capacity = MaxElements;
	H->Size = 0;
	
	H->Elements[0].Word = IrrelevantWord;
	H->Elements[0].Key = MinKey;
	
	return H;
}

void Destroy(PriorityQueue H)
{
	free(H->Elements);
	free(H);
}

int IsFull(PriorityQueue H)
{
	if(H->Size == H->Capacity)
 		return 1;
	else
		return 0;
}

int IsEmpty(PriorityQueue H)
{
	if(H->Size == 0)
		return 1;
	else
	 return 0;
}

void Insert(ETN X, PriorityQueue H)
{
	int i;
	
	if( IsFull(H) )
	{
		printf("Priority Queue is full!\n");
		return ;
	}	
	
	for(i = ++ H->Size; H->Elements[i/Bin].Key > X.Key; i/=Bin)
	{
		H->Elements[i] = H->Elements[i/Bin];
	}
	
	H->Elements[i] = X;
}

ETN DeleteMin(PriorityQueue H)
{
	int i,Child;
	ETN MinElement,LastElement;
	
	if( IsEmpty(H))
	{
		printf("PQ is empty!\n");
		return H->Elements[0];
	}
	
	MinElement = H->Elements[1];
	LastElement = H->Elements[H->Size--];
	
	for(i = 1;i * 2 <= H->Size; i = Child)
	{
		Child = i * 2;
		if(Child != H->Size 
			&& H->Elements[Child + 1].Key < H->Elements[Child].Key)
	 		Child++;
		
		if(LastElement.Key > H->Elements[Child].Key)
			H->Elements[i] = H->Elements[Child];
		else
			break;
	}	
			
	H->Elements[i] = LastElement;
		
	return MinElement;
}

//HuffCode.c
#include "BinHeap.c"
#include <string.h>
#define CharacterTypes 27
#define CharacterLen 100
#define CharacterEnd "END"
#define StandardCharacterLength 8

int HuffCodeLength(PriorityQueue H)
{
	int Length = 0;
	ETN Tmp1,Tmp2;
	
	if(H->Size <= 0)
		return Length;
	if(H->Size == 1)
		return Length + 1;
	
	while(H->Size > 1)
	{
		Tmp1 = DeleteMin(H);
		Tmp2 = DeleteMin(H);
		Length += (Tmp1.Key + Tmp2.Key);
		Tmp1.Key += Tmp2.Key;
		Insert(Tmp1,H);
	}
	DeleteMin(H);
	return Length;
}

int main()
{
	char TheString[CharacterLen];
	char Characters[CharacterTypes];
	PriorityQueue H;
	ETN Tmp;
	int i,StandardResult,HuffCodeResult;
	H = Initialize(CharacterLen);
	while(1)
	{
		gets(TheString);
		if( strcmp(CharacterEnd,TheString) == 0)
			break;

		for(i = 0; i < CharacterTypes; i++)
			Characters[i] = 0;
		/*
		for(i = 0; i < strlen(TheString); i++)
			printf("%c",TheString[i]);
		printf("\n");
		*/
		for(i = 0; i < strlen(TheString); i++)
		{
			if(TheString[i] == '_')
				Characters[CharacterTypes-1]++;
			else	
				Characters[TheString[i]-'A']++;
		}
		/*
		for(i = 0; i < CharacterTypes; i++)
		{
			printf("%d ",Characters[i]);
		}
		printf("\n");
		*/
		
		for(i = 0; (i < CharacterTypes); i++)
	 	{
	 		if( Characters[i] <= 0 ) 
	 			continue;
		 	Tmp.Word = (char)('A' + i);
	 		Tmp.Key = Characters[i];
	 		Insert(Tmp,H);
	 	}
	 	StandardResult = StandardCharacterLength * strlen(TheString);
	 	/*Huffman Code Len*/
	 	HuffCodeResult = HuffCodeLength(H);
	 	printf("%d %d %.1f\n",StandardResult,HuffCodeResult,(float)StandardResult/HuffCodeResult);
	 	/**************/	 	
	}
	
	Destroy(H);
	return 0;
}

并没有具体实现Huffman Tree,直接计算了HuffmanCode的长度。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值