05-树10 Huffman Codes (30分)
2016-11-05 16:22
281 查看
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
f
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
, w;
char ch, str
;
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;
}
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
f
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
, w;
char ch, str
;
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;
}
相关文章推荐
- 05-树10 Huffman Codes (30分)
- 05-树9 Huffman Codes (30分)
- 05-树10 Huffman Codes (30分)
- 05-树9 Huffman Codes (30分)
- 哈夫曼树的操作-05-树9 Huffman Codes
- 10-02-05
- 10 05 01 QY
- 10 05 17 手机受伤了
- 数字图像处理实验(10):PROJECT 05-01 [Multiple Uses],Noise Generators 标签: 图像处理MATLAB 2017-05-26 23:36
- 05-树9 Huffman Codes
- 10-03-05
- 05/10/09(夜)
- 10-05-01
- 10 05 02 看病
- 10 05 18 19 维护
- 10 07 05 hf过来
- PAT 05-树8 Huffman Codes
- 《代码大全2》阅读笔记05--Chapter 10 General Issues in Using Variables
- 数字图像处理实验(10):PROJECT 05-01 [Multiple Uses],Noise Generators
- 05/10/04(凌晨)