-
Notifications
You must be signed in to change notification settings - Fork 1
/
A05.05.cpp
134 lines (134 loc) · 2.98 KB
/
A05.05.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//Algorithm 5.5 (P163-4)(A05.05.cpp)
//哈夫曼(Huffman)最优二叉树算法
#include "headers.h"
class ClsNode {
public:
int index;
int p;
int bit;
int parent;
int lchild;
int rchild;
ClsNode(){};
void set(int aindex, int ap, int abit,
int aparent, int alchild, int archild) {
index = aindex;
p = ap;
bit = abit;
parent = aparent;
lchild = alchild;
rchild = archild;
}
bool operator < (ClsNode &right)
{
return p < right.p;
}
bool operator > (ClsNode &right)
{
return p > right.p;
}
ClsNode operator = (ClsNode &right)
{
this->set(right.index, right.p, right.bit,
right.parent, right.lchild, right.rchild);
return *this;
}
};
//返回最优二叉树, 调用者负责释放
ClsNode *Huffman(int *p, int n, int &root)
{
ClsNode *T = new ClsNode[2*n];
ClsNode *H = new ClsNode[n];
for (int i=0; i<n; i++) {
H[i].set(i, p[i], -1, -1, -1, -1);
T[i].set(i, p[i], -1, -1, -1, -1);
}
int m = n; //m:堆中的结点数
ClsNode x, y, u;
makeHeapMinT(H, m);
root = n-1;
for (int i=0; i<n-1; i++) {
x = deleteTopMinT(H, m);
y = deleteTopMinT(H, m);
u.set(++root, x.p+y.p, -1, -1, x.index, y.index);
insertMinT(H, m, u);
T[root] = u;
T[x.index].bit = 0;
T[y.index].bit = 1;
T[x.index].parent = T[y.index].parent = n+i;
}
delete H;
return T;
}
void printHuffmanCoding(ClsNode *T, int root, char *bits, int &level)
{
if (level)
bits[level-1] = T[root].bit+48;
if (T[root].lchild == -1) {
printf("%c(%d):%s\n", T[root].index+65,
T[root].p, bits);
} else {
level++;
printHuffmanCoding(T, T[root].lchild, bits, level);
level++;
printHuffmanCoding(T, T[root].rchild, bits, level);
}
bits[--level] = 0;
}
void printHuffmanTree(ClsNode *T, int root, int &level)
{
printf("%2d: %3d %-3d %4d %4d\n",
root, level, T[root].p, T[root].lchild, T[root].rchild);
if (T[root].lchild != -1) {
level++;
printHuffmanTree(T, T[root].lchild, level);
level++;
printHuffmanTree(T, T[root].rchild, level);
}
--level;
}
//p161-162
void HuffmanTestEx5_6()
{
int p[] = {3, 4, 5, 8, 9, 15, 20, 36};
int root = -1;
int n = 8;
ClsNode *T = Huffman(p, n, root);
char *bits = new char[2*n];
for (int i=0; i<2*n; i++)
bits[i] = 0;
int level = 0;
printf("Character frequency:\n");
for (int i=0; i<n; i++)
printf("%c:%d ", i+65, p[i]);
printf("\n");
printf("Huffman Coding:\n");
printHuffmanCoding(T, root, bits, level);
printf("Huffman Tree:\n");
printf("No: level p lchild rchild\n");
level = 0;
printHuffmanTree(T, root, level);
delete T, bits;
}
void HuffmanTest(int n)
{
int *p = new int[n];
randRangeArr(n, 1, 99, p);
int root = -1;
ClsNode *T = Huffman(p, n, root);
char *bits = new char[2*n];
for (int i=0; i<2*n; i++)
bits[i] = 0;
int level = 0;
printf("Character frequency:\n");
for (int i=0; i<n; i++)
printf("%c:%d ", i+65, p[i]);
printf("\n");
printf("Huffman Coding:\n");
printHuffmanCoding(T, root, bits, level);
printf("Huffman Tree:\n");
printf("No: level p lchild rchild\n");
level = 0;
printHuffmanTree(T, root, level);
delete p, T, bits;
}