权值哈夫曼树
#include<iostream>
using namespace std;
#include<queue>
#include<string>
class Node{
public:
int val;
Node* left;
Node* right;
Node* parent;
Node(int val):val(val),left(NULL),right(NULL),parent(NULL){}
Node(int val,Node* left,Node* right,Node* parent):val(val),left(left),right(right),parent(parent){}
};
class Compare{
public:
bool operator()(Node* a,Node* b){
return a->val > b->val;
}
};
class HuffmanTree{
private:
priority_queue<Node*,vector<Node*>,Compare> nodes;
Node* root;
public:
HuffmanTree();
~HuffmanTree();
Node* create();
void encode(Node* root,string code);
void preOrder(Node* root);
void inOrder(Node* root);
void postOrder(Node* root);
void destroyTree(Node* root);
};
HuffmanTree::HuffmanTree(){
while(!nodes.empty()) nodes.pop();
int a[] = {4,3,5,1,2};
int len = sizeof(a) / sizeof(a[0]);
for(int i=0;i<len;i++){
nodes.push(new Node(a[i]));
}
root = NULL;
}
HuffmanTree::~HuffmanTree(){
destroyTree(root);
}
Node* HuffmanTree::create(){
while(nodes.size() > 1){
Node* node1 = nodes.top();
nodes.pop();
Node* node2 = nodes.top();
nodes.pop();
Node* root = new Node(node1->val + node2->val);
root->left = node1;
root->right = node2;
node1->parent = root;
node2->parent = root;
nodes.push(root);
}
root = nodes.top();
nodes.pop();
return root;
}
void HuffmanTree::encode(Node* root,string code){
if(root->left == NULL&&root->right == NULL)
cout<<root->val<<" 被编码为 "<<code<<endl;
if(root->left!=NULL){
code += "0";
encode(root->left,code);
code.erase(code.end()-1);
}
if(root->right!=NULL){
code += "1";
encode(root->right,code);
code.erase(code.end()-1);
}
}
void HuffmanTree::preOrder(Node* root){
if(root == NULL)
return;
cout<< root->val<<" ";
preOrder(root->left);
preOrder(root->right);
}
void HuffmanTree::inOrder(Node* root){
if(root == NULL)
return;
inOrder(root->left);
cout<< root->val<<" ";
inOrder(root->right);
}
void HuffmanTree::postOrder(Node* root){
if(root == NULL)
return;
postOrder(root->left);
postOrder(root->right);
cout<< root->val<<" ";
}
void HuffmanTree::destroyTree(Node* root){
if(root == NULL)
return;
destroyTree(root->left);
destroyTree(root->right);
delete root;
}
int main(){
/*创建哈夫曼树*/
HuffmanTree* huffmanTree = new HuffmanTree();
Node* root = huffmanTree->create();
cout << "先序遍历:";
huffmanTree->preOrder(root);
cout << endl;
cout << "中序遍历:";
huffmanTree->inOrder(root);
cout << endl;
cout << "后序遍历:";
huffmanTree->postOrder(root);
cout << endl;
cout << "哈夫曼编码:" << endl;
huffmanTree->encode(root, "");
return 0;
}
基本哈夫曼树
#include <iostream>
using namespace std;
#define LEAFNUM 10 //叶子节点数,也就是权值树
#define HUFNUM 2*LEAFNUM
#define MAXWEIGHT 999.9
//*********存储结构***********
class HufTree;
//***** Node**********
class NODE
{
private:
char Data; //节点的数据域
double Weight; //节点的权值域
int Lchild,Rchild,Parent; //节点的左孩子右孩子及双亲域
public:
NODE() //构造函数
{
Data = '\0';
Weight = 0;
Lchild = -1;
Rchild = -1;
Parent = -1; //给节点的数据初始化
}
int Re_L(){return Lchild;}
int Re_R(){return Rchild;}
char Re_Data(){return Data;}
double Re_Weight(){return Weight;}
friend class HufTree; //声明友元
};//Node
//********HufTree类**********
class HufTree
{
private:
int NodeNum;
NODE HufArry[HUFNUM];
public:
HufTree(){NodeNum = 0;}
void SetHuf(int,double,char); //设置权值与数据域
void CreatHuf(); //创建哈夫曼树
void SelectMin(int,int&,int&); //查找哈夫曼树种两个权值最小的树
void Find_Root_and_Print(); //查找树根节点位置
void PrintHuf(int); //遍历哈夫曼树
};//huftree
void HufTree::SetHuf(int i,double wei,char ch)
{
HufArry[i].Data = ch;
HufArry[i].Weight = wei;
}
void HufTree::CreatHuf()
{
cout<<"每次查询两个最小树的位置:"<<endl;
for(int i = LEAFNUM; i < HUFNUM - 1; i++)
{
int p1 = 0;
int p2 = 0;
SelectMin(i,p1,p2); //找出当前树种权值最小的两颗树
cout<<p1<<" < - > "<<p2<<endl;
HufArry[p1].Parent = i; //设置两颗最小树的双亲
HufArry[p2].Parent = i;
HufArry[i].Lchild = p1; //设置这棵3节点的树的根的权值以及孩子
HufArry[i].Rchild = p2;
HufArry[i].Weight = HufArry[p1].Weight + HufArry[p2].Weight;
}
cout<<"************************"<<endl;
}
void HufTree::SelectMin(int i,int &p1,int &p2)
{
int least1 = MAXWEIGHT;
int least2 = MAXWEIGHT;
for(int j = 0; j < i; j++)
{
if(HufArry[j].Parent == -1)
{
if(HufArry[j].Weight < least1)
{
least2 = least1;
least1 = HufArry[j].Weight;
p2 = p1;
p1 = j;
}
else
{
if(HufArry[j].Weight < least2)
{
least2 = HufArry[j].Weight;
p2 = j;
}
}
}
}
}
void HufTree::Find_Root_and_Print()
{
int root;
for(int i = 0; i < HUFNUM - 1; i++)
{
if(HufArry[i].Parent == -1)
{
root = i;
break;
}
}
PrintHuf(root);
}
void HufTree::PrintHuf(int position)
{
if(NodeNum == LEAFNUM)
{
return;
}
else
{
if(HufArry[position].Data != '\0') //如果是叶子节点
{
cout<<"权值:"<<HufArry[position].Weight<<"<-> 数据:"<<HufArry[position].Data<<" 此节点为叶子"<<endl;
NodeNum = NodeNum + 1;
}
else
{
cout<<"权值:"<<HufArry[position].Weight<<" 此节点无数据域,不是叶子"<<endl;
PrintHuf(HufArry[position].Lchild);
PrintHuf(HufArry[position].Rchild);
}
}
}
int main()
{
HufTree Tree;
cout<<"请输入"<<LEAFNUM<<"对(权值,数据):"<<endl;
double wei;
char ch;
for(int i = 0; i < LEAFNUM; i++)
{
cin>>wei;
cin>>ch;
Tree.SetHuf(i,wei,ch);
}
Tree.CreatHuf(); //创建哈夫曼树
Tree.Find_Root_and_Print(); //遍历哈夫曼树
return 0;
}
/*测试数据
1 a
2 b
5 c
7 d
4 e
13 f
3 g
6 h
11 i
8 l
*/