class HuffmanTree{
private:
typedef struct node {
int weight;
int leftchild, rightchild, parent;
char ch;
}HTNode, * Huffman;
int length = 1;
typedef char** Huffamncode;
Huffman Ht = new HTNode[length];
public:
/**
* 构造函数
@name HuffmanTree(const int* Table)
@param arg1 数字出现的频度表
@return
注意: 要求树的左孩子为权制较小的编码,左孩子的二进制编号为0
*/
HuffmanTree(const int* Table);
/**
* 析构函数
@name ~HuffmanTree()
@param
@return
*/
~HuffmanTree();
/**
* 获取message的霍夫曼编码
@name string Encode(string)
@param arg1 待编码待字符串
@return 对应的霍夫曼编码
*/
string Encode(string message);
/**
* 获取message的霍夫曼解码
@name string Decode(stirng)
@param
@return 解码出的内容
*/
string Decode(string message);
void select_minium( Huffman HT, int k, int& min1, int& min2);
int min(Huffman HT, int k);
};
下面cpp中的代码部分,请各位大佬注意析构函数中我delete[] Ht 时报错,我自己试了一下,就是在类的数据域中直接Huffman Ht = new HTNode[7]这样声明则没有问题,就是不用变长数组,改为定长则delete没有问题。我想请问这是什么原因?
HuffmanTree::HuffmanTree(const int* Table) {
int i;
char zifu[26] = { 'a','b','c','d','e','f','g','h','i','g','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
for (i = 0; i <26; i++)
{
if (*Table==0)
{
break;
}
Ht[i].leftchild = -1;
Ht[i].parent = -1;
Ht[i].rightchild = -1;
Ht[i].weight = *Table;
Ht[i].ch = zifu[i];
Table++;
length += 2;
}
for (; i <length-2; i++)
{
Ht[i].leftchild = -1;
Ht[i].parent = -1;
Ht[i].rightchild = -1;
Ht[i].weight = 0;
}
int min1, min2;
for (i = (length-1)/2; i <length-2; i++)
{
select_minium(Ht, i, min1, min2);
Ht[i].leftchild = min1;
Ht[i].rightchild = min2;
Ht[i].weight = Ht[min1].weight + Ht[min2].weight;
Ht[min1].parent = i;
Ht[min2].parent = i;
}
}
HuffmanTree::~HuffmanTree(){
delete[] Ht;
}
string HuffmanTree::Encode(string message){
Huffamncode Hc;
Hc = new char* [(length-1)/2];
char* code = new char[(length - 1) / 2 ];
code[(length - 1) / 2-1] = '\0';
int i;
for (i = 0; i < (length - 1) / 2; i++)
{
int current = i;
int father = Ht[i].parent;
int start = (length - 1) / 2-1;
while (father != -1)
{
if (Ht[father].leftchild == current)
{
//cout << start << endl;
code[--start] = '0';
current = father;
father = Ht[father].parent;
}
else
{
//cout << start << endl;
code[--start] = '1';
current = father;
father = Ht[father].parent;
}
}
Hc[i] = new char[(length - 1) / 2 - start];
strcpy(Hc[i], code + start);
//for (int j = start; j < 3; j++) cout << code[j];
}
delete[] code;
string mas;
for (unsigned int i = 0; i < message.length(); i++)
{
for (int j = 0; j <(length-1)/2 ; j++)
{
if (message[i] == Ht[j].ch)
{
mas.append(Hc[j]);
}
}
}
delete[]Hc;
return mas;
}
string HuffmanTree::Decode(string message){
string mas;
int m = 0;
int x;
for (unsigned int i = 0; i < message.length()+1; i++)
{
if (m == 0)
{
x = length - 3;
m++;
}
if (Ht[x].leftchild == -1 && Ht[x].rightchild == -1)
{
mas += Ht[x].ch;
i--;
m--;
continue;
}
if (message[i] == '0')
{
x = Ht[x].leftchild;
}
if (message[i] == '1')
{
x = Ht[x].rightchild;
}
}
return mas;
}
void HuffmanTree::select_minium(Huffman HT, int k, int& min1, int& min2)
{
min1 = min(HT, k);
min2 = min(HT, k);
}
int HuffmanTree::min(Huffman HT, int k)
{
int i = 0;
int min;
int min_weight;
while (HT[i].parent != -1)
i++;
min_weight = HT[i].weight;
min = i;
for (; i < k; i++)
{
if (HT[i].weight < min_weight && HT[i].parent == -1)
{
min_weight = HT[i].weight;
min = i;
}
}
HT[min].parent = 1;
return min;
}