#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<iomanip>//这个头文件是声明一些 “流操作符”的
//比较常用的有:setw(int);//设置显示宽度,left//right//设置左右对齐。 setprecision(int);//设置浮点数的精确度。
typedef struct TreeCode
{
int start;
int code[2014];
};
typedef struct TreeNode
{
int weight;
int parent,rchild,lchild;
};
void Select(TreeNode a[],int &s1,int &s2,int n)
{
for (int i = 0; i < n; i++)
{
if (a[i].parent == -1)// 初始化s1,s1的双亲为-1
{
s1 = i;
break;
}
}
for (int i = 0; i < n; i++)// s1为权值最小的下标
{
if (a[i].parent == -1 && a[s1].weight > a[i].weight)
s1 = i;
}
for (int j = 0; j < n; j++)
{
if (a[j].parent == -1&&j!=s1)// 初始化s2,s2的双亲为-1
{
s2 = j;
break;
}
}
for (int j = 0; j < n; j++)// s2为另一个权值最小的结点
{
if (a[j].parent == -1 && a[s2].weight > a[j].weight&&j != s1)
s2 = j;
}
}
void HuffmanTree(TreeNode tree[],int l,int str[])
{
for(int i=0;i<2*l-1;i++)
{
tree[i].weight=tree[i].rchild=tree[i].lchild=tree[i].parent=-1;
}
for(int i=0;i<l;i++)
{
tree[i].weight=str[i];
}
for(int i=l;i<2*l-1;i++)
{
int s1,s2;
Select(tree,s1,s2,i);
tree[i].weight=tree[s1].weight+tree[s2].weight;
tree[i].rchild=s2;
tree[i].lchild=s1;
tree[s1].parent=tree[s2].parent=i;
}
}
void BulidCode(TreeCode treecode[],TreeNode tree[],int l)
{
int c,s;
TreeCode p;
for(int i=0;i<l;i++)
{
p.start=l-1;
c=i;
s=tree[c].parent;
while(s!=-1)
{
if(tree[s].lchild==c){
p.code[p.start]=0;
}else{
p.code[p.start]=1;
}
p.start--;
c=s;
s=tree[c].parent;
}
for(int j=p.start+1;j<l;j++)
{
treecode[i].code[j]=p.code[j];
}
treecode[i].start=p.start;
}
}
void Print(TreeNode root[],TreeCode treecode[],int l)
{
cout << "index weight parent lChild rChild code" << endl;
cout << left; // 左对齐输出
for (int i = 0; i < l; i++)
{
cout << setw(5) << i << " ";
cout << setw(6) << root[i].weight << " ";
cout << setw(6) << root[i].parent << " ";
cout << setw(6) << root[i].lchild << " ";
cout << setw(6) << root[i].rchild << " ";
for(int j=treecode[i].start+1;j<l;j++)
{
cout<<treecode[i].code[j];
}
cout<<endl;
}
for(int i=l;i<l*2-1;i++)
{
cout << setw(5) << i << " ";
cout << setw(6) << root[i].weight << " ";
cout << setw(6) << root[i].parent << " ";
cout << setw(6) << root[i].lchild << " ";
cout << setw(6) << root[i].rchild << endl;
}
}
int main()
{
int str[]={2,5,3,7,8,9,11};
int l=sizeof(str)/sizeof(str[0]);
TreeNode *tree=new TreeNode [2*l-1];
HuffmanTree(tree,l,str);
TreeCode *treecode=new TreeCode [2*l-1];
BulidCode(treecode,tree,l);
Print(tree,treecode,l);
}
#include<stdlib.h>
#include<string.h>
#include<iomanip>//这个头文件是声明一些 “流操作符”的
//比较常用的有:setw(int);//设置显示宽度,left//right//设置左右对齐。 setprecision(int);//设置浮点数的精确度。
/*或者可以用#include<bits/stdc++.h>包含所有c++头文件*/
using namespace std;
typedef struct TreeCode
{
int start;
int code[2014];
};
typedef struct TreeNode
{
int weight;
int parent,rchild,lchild;
};
void Select(TreeNode a[],int &s1,int &s2,int n)
{
for (int i = 0; i < n; i++)
{
if (a[i].parent == -1)// 初始化s1,s1的双亲为-1
{
s1 = i;
break;
}
}
for (int i = 0; i < n; i++)// s1为权值最小的下标
{
if (a[i].parent == -1 && a[s1].weight > a[i].weight)
s1 = i;
}
for (int j = 0; j < n; j++)
{
if (a[j].parent == -1&&j!=s1)// 初始化s2,s2的双亲为-1
{
s2 = j;
break;
}
}
for (int j = 0; j < n; j++)// s2为另一个权值最小的结点
{
if (a[j].parent == -1 && a[s2].weight > a[j].weight&&j != s1)
s2 = j;
}
}
void HuffmanTree(TreeNode tree[],int l,int str[])
{
for(int i=0;i<2*l-1;i++)
{
tree[i].weight=tree[i].rchild=tree[i].lchild=tree[i].parent=-1;
}
for(int i=0;i<l;i++)
{
tree[i].weight=str[i];
}
for(int i=l;i<2*l-1;i++)
{
int s1,s2;
Select(tree,s1,s2,i);
tree[i].weight=tree[s1].weight+tree[s2].weight;
tree[i].rchild=s2;
tree[i].lchild=s1;
tree[s1].parent=tree[s2].parent=i;
}
}
void BulidCode(TreeCode treecode[],TreeNode tree[],int l)
{
int c,s;
TreeCode p;
for(int i=0;i<l;i++)
{
p.start=l-1;
c=i;
s=tree[c].parent;
while(s!=-1)
{
if(tree[s].lchild==c){
p.code[p.start]=0;
}else{
p.code[p.start]=1;
}
p.start--;
c=s;
s=tree[c].parent;
}
for(int j=p.start+1;j<l;j++)
{
treecode[i].code[j]=p.code[j];
}
treecode[i].start=p.start;
}
}
void Print(TreeNode root[],TreeCode treecode[],int l)
{
cout << "index weight parent lChild rChild code" << endl;
cout << left; // 左对齐输出
for (int i = 0; i < l; i++)
{
cout << setw(5) << i << " ";
cout << setw(6) << root[i].weight << " ";
cout << setw(6) << root[i].parent << " ";
cout << setw(6) << root[i].lchild << " ";
cout << setw(6) << root[i].rchild << " ";
for(int j=treecode[i].start+1;j<l;j++)
{
cout<<treecode[i].code[j];
}
cout<<endl;
}
for(int i=l;i<l*2-1;i++)
{
cout << setw(5) << i << " ";
cout << setw(6) << root[i].weight << " ";
cout << setw(6) << root[i].parent << " ";
cout << setw(6) << root[i].lchild << " ";
cout << setw(6) << root[i].rchild << endl;
}
}
int main()
{
int str[]={2,5,3,7,8,9,11};
int l=sizeof(str)/sizeof(str[0]);
TreeNode *tree=new TreeNode [2*l-1];
HuffmanTree(tree,l,str);
TreeCode *treecode=new TreeCode [2*l-1];
BulidCode(treecode,tree,l);
Print(tree,treecode,l);
}