05-树10 Huffman Codes (30分)

In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redundancy Codes", and hence printed his name in the history of computer science. As a professor who gives the final exam problem on Huffman codes, I am encountering a big problem: the Huffman codes are NOT unique. For example, given a string "aaaxuaxz", we can observe that the frequencies of the characters 'a', 'x', 'u' and 'z' are 4, 2, 1 and 1, respectively. We may either encode the symbols as {'a'=0, 'x'=10, 'u'=110, 'z'=111}, or in another way as {'a'=1, 'x'=01, 'u'=001, 'z'=000}, both compress the string into 14 bits. Another set of code can be given as {'a'=0, 'x'=11, 'u'=100, 'z'=101}, but {'a'=0, 'x'=01, 'u'=011, 'z'=001} is NOT correct since "aaaxuaxz" and "aazuaxax" can both be decoded from the code 00001011001001. The students are submitting all kinds of codes, and I need a computer program to help me determine which ones are correct and which ones are not.


Input Specification:
Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤63), then followed by a line that contains all the N distinct characters and their frequencies in the following format:
c[1] f[1] c[2] f[2] ... c[N] f[N]
where c[i] is a character chosen from {'0' - '9', 'a' - 'z', 'A' - 'Z', '_'}, and f[i] is the frequency of c[i] and is an integer no more than 1000. The next line gives a positive integer M (≤1000), then followed by MM student submissions. Each student submission consists of N lines, each in the format:
c[i] code[i]
where c[i] is the i-th character and code[i] is an non-empty string of no more than 63 '0's and '1's.

Output Specification:
For each test case, print in each line either "Yes" if the student's submission is correct, or "No" if not.

Note: The optimal solution is not necessarily generated by Huffman algorithm. Any prefix code with code length being optimal is considered correct.


Sample Input:
7
A 1 B 1 C 1 D 3 E 3 F 6 G 6
4
A 00000
B 00001
C 0001
D 001
E 01
F 10
G 11
A 01010
B 01011
C 0100
D 011
E 10
F 11
G 00
A 000
B 001
C 010
D 011
E 100
F 101
G 110
A 00000
B 00001
C 0001
D 001
E 00
F 10
G 11


Sample Output:
Yes
Yes
No
No


//Huffman编码不唯一
//Huffman编码是最优编码,但最优编码不一定是Huffman编码

//需要满足的条件
//1最优:wpl最小  建立Huffman树计算最小wpl判断学生的提交是否正确
//2无歧义:前缀码  建树过程中检查是否满足前缀码要求

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MinData -1
#define N 64
typedef struct HuTreeNode *HuffmanTree;
typedef struct HNode *Heap;
typedef struct BiTreeNode *BinTree;
struct HuTreeNode {
	int Weight;
	HuffmanTree Left, Right;
};
struct HNode {
	HuffmanTree* Data;
	int Size;
};
struct BiTreeNode {
	int flag;
	BinTree Left, Right;
};

HuffmanTree BuiltHuffman(Heap H);
HuffmanTree DeleteMinHeap(Heap H);
void InsertMinHeap(Heap H, HuffmanTree HT);
int Wpl(HuffmanTree HT, int depth);
void CheckPrefixcode(BinTree BT, char *str, int *flag);
BinTree Delete(BinTree BT);

int main(void) {
	int i, n, m, wpl, flag, frequency[N], w;
	char ch, str[N];
	Heap H = (Heap)malloc(sizeof(struct HNode));
	HuffmanTree HT;
	BinTree BT;

	scanf("%d\n", &n);
	H->Data = (HuffmanTree*)malloc((n + 1)*sizeof(HuffmanTree));
	H->Data[0] = (HuffmanTree)malloc(sizeof(struct HuTreeNode));
	H->Data[0]->Weight = MinData;
	H->Size = 0;
	for (i = 1;i <= n;i++) {
		ch = getchar();
		if(!isalpha(ch))
			ch = getchar();
		scanf(" %d", &frequency[i]);
		HT = (HuffmanTree)malloc(sizeof(struct HuTreeNode));
		HT->Weight = frequency[i];
		HT->Left = HT->Right = NULL;
		InsertMinHeap(H, HT);
	}
	HT = BuiltHuffman(H);
	w = Wpl(HT, 0);

	ch = getchar();
	scanf("%d\n", &m);
	while (m--) {
		wpl = 0;
		flag = 0;
		BT = (BinTree)malloc(sizeof(struct BiTreeNode));
		BT->flag = 0;
		BT->Left = BT->Right = NULL;
		for (i = 0;i < n;i++) {
			scanf("%c %s\n", &ch, str);
			wpl += strlen(str) * frequency[i+1];
			if(!flag)
				CheckPrefixcode(BT, str, &flag);
		}
		if (flag || wpl > w)
			printf("No\n");
		else printf("Yes\n");
		BT = Delete(BT);
	}
}

//建立Huffman树
HuffmanTree BuiltHuffman(Heap H) {
	int i, k = H->Size - 1;
	HuffmanTree HT;
	for (i = 0;i < k;i++) {
		HT = (HuffmanTree)malloc(sizeof(struct HuTreeNode));
		HT->Left = DeleteMinHeap(H);
		HT->Right = DeleteMinHeap(H);
		HT->Weight = HT->Left->Weight + HT->Right->Weight;
		InsertMinHeap(H, HT);
	}
	return DeleteMinHeap(H);
}
//从最小堆中删除元素
HuffmanTree DeleteMinHeap(Heap H) {
	int i, child;
	HuffmanTree MinItem, LastItem;
	MinItem = H->Data[1];
	LastItem = H->Data[H->Size--];
	for (i = 1;i * 2 <= H->Size;i = child) {
		child = i * 2;
		if (child < H->Size && H->Data[child + 1]->Weight < H->Data[child]->Weight)
			child++;
		if(LastItem->Weight > H->Data[child]->Weight)
			H->Data[i] = H->Data[child];
		else break;
	}
	H->Data[i] = LastItem;
	return MinItem;
}
//向最小堆中插入元素
void InsertMinHeap(Heap H, HuffmanTree HT) {
	int i;
	for (i = ++H->Size; H->Data[i / 2]->Weight > HT->Weight; i /= 2)
		H->Data[i] = H->Data[i / 2];
	H->Data[i] = HT;
}
//计算最优编码长度
int Wpl(HuffmanTree HT, int depth) {
	if (!HT->Left && !HT->Right)
		return depth*HT->Weight;
	else
		return Wpl(HT->Left, depth + 1) + Wpl(HT->Right, depth + 1);
}
//检查是否为前缀码
void CheckPrefixcode(BinTree BT, char *str, int *flag) {
	size_t i;
	for (i = 0;i < strlen(str);i++) {
		if (BT->flag) //非叶子节点存在元素
		{
			*flag = 1;
			return;
		}
		if (str[i] == '0') {
			if (!BT->Left) {
				BinTree T = (BinTree)malloc(sizeof(struct BiTreeNode));
				T->flag = 0;
				T->Left = T->Right = NULL;
				BT->Left = T;
			}
			BT = BT->Left;
		}
		else {
			if (!BT->Right) {
				BinTree T = (BinTree)malloc(sizeof(struct BiTreeNode));
				T->flag = 0;
				T->Left = T->Right = NULL;
				BT->Right = T;
			}
			BT = BT->Right;
		}
	}
	//该节点将写入元素,如果已经存在元素(重合)或者非叶节点(存在子树),则不是前缀码
	if (BT->flag || BT->Left || BT->Right) {
		*flag = 1;
		return;
	}
	BT->flag = 1;
}
//删除树
BinTree Delete(BinTree BT) {
	if (!BT->Left && !BT->Right) {
		free(BT);
		BT = NULL;	
	}
	else {
		if (BT->Left)
			BT->Left = Delete(BT->Left);
		if (BT->Right)
			BT->Right = Delete(BT->Right);
	}
	return BT;
}



  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值