Huffman树的编码实现

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
struct huffman_node {
char c;
int weight;
int treeweight;
huffman_node * left;
huffman_node * right;
vector<int> code;
};
class Huffmantree
{
private:
string str;
vector<huffman_node*>huffmanline;
public:
huffman_node* root;
vector<char> huffman_code;
Huffmantree();
int Read(fstream &f);
void Creatree();//在创建树之前要对节点数组进行排序
void Sortline();//对节点数组排序,便于创建树
huffman_node* mergetree(huffman_node*& htl1, huffman_node*& htl2);
void Gethuffmancode();
void print_leaf();
};
Huffmantree::Huffmantree()
{
huffman_node* root = new huffman_node;
root->left = NULL;
root->weight = 0;
}
int Huffmantree::Read(fstream& f)
{
int i = 0;
cout << "duqudaowenjian \n";
getline(f, str);
while(str!="")
{
cout << "读取成功\n";
char a;
for (istringstream sin(str); sin >> a; ++i)
{
huffman_node* add = new huffman_node;
add->c = a;
huffmanline.push_back(add);
for (int k = 0; k<huffmanline.size(); k++)
{
if (a == huffmanline[k]->c)
{
huffmanline.pop_back();
break;
}
}
for (int j = 0;j < huffmanline.size(); j++)
{
if (a == huffmanline[j]->c)
{
(huffmanline[j]->weight)++;
}
}
}
}
getline(f, str);
return i;//返回所有字符长度


}
void Huffmantree::Sortline()//对节点数组排序,为创建树做 准备工作
{
if (huffmanline.size() == 0)
cout << "错误\n";
else {
for (int i = 0; i < huffmanline.size(); ++i)
{
cout << "第一层第" << i << "次循环\n";
for (int j = i + 1; j < huffmanline.size(); ++j)
{
if (huffmanline[i]->weight < huffmanline[j]->weight)
{
int transnumber;
transnumber = huffmanline[i]->weight;
huffmanline[i]->weight = huffmanline[j]->weight;
huffmanline[j]->weight = transnumber;
}


}
}
}
}
huffman_node* Huffmantree::mergetree(huffman_node*& htl1, huffman_node*& htl2)
{
huffman_node* parent = new huffman_node;
parent->left = htl2;
htl2->treeweight = 0;
parent->right = htl1;
htl1->treeweight = 0;
return parent;
}
void Huffmantree::Creatree()
{
huffman_node* miner;
huffman_node* bigger;
int length = huffmanline.size();
miner = huffmanline[length - 1];
bigger = huffmanline[length - 2];
for (int i = length - 2; i>0;)
{
huffman_node* next = new huffman_node;
next = mergetree(miner, bigger);
next->c = '\0';
miner = next; --i;
bigger = huffmanline[i];
}
root = mergetree(miner, bigger);
cout << "创建成功\n";
}
void Huffmantree::Gethuffmancode()
{
huffman_node* Queue[100];
huffman_node* p;
int front, rear;
front = rear = 0;
Queue[rear++] = root;
while (rear != front)
{
p = Queue[front++];
if (p->left->left&&p->left->right)
{
Queue[rear++] = p->left;
}
else
{
for (int n = 0; n < rear; n++)
{
p->code.push_back(Queue[n]->treeweight);
}
}
if (p->right)Queue[rear++] = p->right;


}


}
void Huffmantree::print_leaf()
{
for (int i = 0; i < huffmanline.size(); i++)
{
cout << huffmanline[i]->c << "的编码是" << "\t";
for (int j = 0; j < huffmanline[i]->code.size(); j++)
cout << huffmanline[i]->code[j];
}

}

#include"haff.h"
using namespace std;
int main()
{
fstream f;
f.open("zhr.txt");
Huffmantree BT;
BT.Read(f);
BT.Sortline();
BT.Creatree();
BT.Gethuffmancode();
BT.print_leaf();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值