#include <algorithm>
#include <iostream>
#include <cstdio>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <sstream>
using namespace std;
struct node{
int weight;
int parent,lchild,rchild;//初始化都为-1,表示没有
}*huffmantree;
typedef char **huffmancode;//保存哈夫曼编码
int choosemin(node *ht,int k){
int i=0;
int minn;
int min_weight;
while(ht[i].parent!=-1) i++;
minn=i;
min_weight=ht[i].weight;
for(;i<k;i++){
if(ht[i].parent==-1&&ht[i].weight<min_weight){
min_weight=ht[i].weight;
minn=i;
}
}
ht[minn].parent=1;
return minn;
}
void select_min(node *ht,int i,int &min1,int &min2){
min1=choosemin(ht,i);
min2=choosemin(ht,i);
}
node *create_huffmantree(node *ht,int *wet,int n){//将所有的权值存进去,n是权值个数,也是叶子节点的个数
int total=2*n-1;//不存在有一个孩子的结点
ht=(node *)malloc(total*sizeof(node));
int i;
for(i=0;i<n;i++){
ht[i].lchild=-1;
ht[i].rchild=-1;
ht[i].parent=-1;
ht[i].weight=*wet;
wet++;
}
for(;i<total;i++){//存放的是中间构造的每棵二叉树的根节点
ht[i].lchild=-1;
ht[i].rchild=-1;
ht[i].parent=-1;
ht[i].weight=0;
}
int min1,min2;//找出最小的两个结点
for(i=n;i<total;i++){
select_min(ht,i,min1,min2);
ht[min1].parent=i;
ht[min2].parent=i;
ht[i].lchild=min1;
ht[i].rchild=min2;
ht[i].weight=ht[min1].weight+ht[min2].weight;
}
return ht;
}
//哈夫曼编码 两种方法
//第一种方法 从叶子节点往上
void huffmancoding(node *ht,huffmancode &hc,int n){
hc=(huffmancode)malloc(n*sizeof(char *));
char *code=(char *)malloc(n*sizeof(char));
code[n-1]='\0';
for(int i=0;i<n;i++){
int current=i;
int father=ht[i].parent;
int start=n-1;
while(father!=-1){
if(ht[father].lchild==current) code[--start]='0';
else code[--start]='1';
current=father;
father=ht[father].parent;
}
hc[i]=(char *)malloc(n*sizeof(char));
strcpy(hc[i],code+start);
}
free(code);
}
//第二种方法:从根节点开始
void huffmancoding2(node *ht,huffmancode &hc,int n){
hc=(huffmancode)malloc(n*sizeof(char *));
char *code=(char *)malloc(n*sizeof(char));
int cur=n*2-2;//根节点
int len=0;
for(int i=0;i<=cur;i++){
ht[i].weight=0;//0表示左右孩子都没被遍历过,1表示左孩子被遍历,2表示右孩子被遍历
}
while(cur!=-1){
if(ht[cur].weight==0){
ht[cur].weight=1;
if(ht[cur].lchild!=-1){
code[len++]='0';
cur=ht[cur].lchild;
}
else{
code[len]='\0';
hc[cur]=(char *)malloc((len+1)*sizeof(char));
strcpy(hc[cur],code);
}
}
else if(ht[cur].weight==1){
ht[cur].weight=2;
if(ht[cur].rchild!=-1){
code[len++]='1';
cur=ht[cur].rchild;
}
}
else
{
ht[cur].weight = 0;
cur = ht[cur].parent;
--len;
}
}
free(code);
}
int wet[100005];
int main(){
int n;
huffmancode hc;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&wet[i]);
}
node *t=NULL;
t=create_huffmantree(t,wet,n);
huffmancoding(t,hc,n);
for(int i=0;i<n;i++){
printf("%s\n",hc[i]);
}
return 0;
}
//带权路径长度:SUM(此节点到根节点的路径长度*权值)
#include <iostream>
#include <cstdio>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <sstream>
using namespace std;
struct node{
int weight;
int parent,lchild,rchild;//初始化都为-1,表示没有
}*huffmantree;
typedef char **huffmancode;//保存哈夫曼编码
int choosemin(node *ht,int k){
int i=0;
int minn;
int min_weight;
while(ht[i].parent!=-1) i++;
minn=i;
min_weight=ht[i].weight;
for(;i<k;i++){
if(ht[i].parent==-1&&ht[i].weight<min_weight){
min_weight=ht[i].weight;
minn=i;
}
}
ht[minn].parent=1;
return minn;
}
void select_min(node *ht,int i,int &min1,int &min2){
min1=choosemin(ht,i);
min2=choosemin(ht,i);
}
node *create_huffmantree(node *ht,int *wet,int n){//将所有的权值存进去,n是权值个数,也是叶子节点的个数
int total=2*n-1;//不存在有一个孩子的结点
ht=(node *)malloc(total*sizeof(node));
int i;
for(i=0;i<n;i++){
ht[i].lchild=-1;
ht[i].rchild=-1;
ht[i].parent=-1;
ht[i].weight=*wet;
wet++;
}
for(;i<total;i++){//存放的是中间构造的每棵二叉树的根节点
ht[i].lchild=-1;
ht[i].rchild=-1;
ht[i].parent=-1;
ht[i].weight=0;
}
int min1,min2;//找出最小的两个结点
for(i=n;i<total;i++){
select_min(ht,i,min1,min2);
ht[min1].parent=i;
ht[min2].parent=i;
ht[i].lchild=min1;
ht[i].rchild=min2;
ht[i].weight=ht[min1].weight+ht[min2].weight;
}
return ht;
}
//哈夫曼编码 两种方法
//第一种方法 从叶子节点往上
void huffmancoding(node *ht,huffmancode &hc,int n){
hc=(huffmancode)malloc(n*sizeof(char *));
char *code=(char *)malloc(n*sizeof(char));
code[n-1]='\0';
for(int i=0;i<n;i++){
int current=i;
int father=ht[i].parent;
int start=n-1;
while(father!=-1){
if(ht[father].lchild==current) code[--start]='0';
else code[--start]='1';
current=father;
father=ht[father].parent;
}
hc[i]=(char *)malloc(n*sizeof(char));
strcpy(hc[i],code+start);
}
free(code);
}
//第二种方法:从根节点开始
void huffmancoding2(node *ht,huffmancode &hc,int n){
hc=(huffmancode)malloc(n*sizeof(char *));
char *code=(char *)malloc(n*sizeof(char));
int cur=n*2-2;//根节点
int len=0;
for(int i=0;i<=cur;i++){
ht[i].weight=0;//0表示左右孩子都没被遍历过,1表示左孩子被遍历,2表示右孩子被遍历
}
while(cur!=-1){
if(ht[cur].weight==0){
ht[cur].weight=1;
if(ht[cur].lchild!=-1){
code[len++]='0';
cur=ht[cur].lchild;
}
else{
code[len]='\0';
hc[cur]=(char *)malloc((len+1)*sizeof(char));
strcpy(hc[cur],code);
}
}
else if(ht[cur].weight==1){
ht[cur].weight=2;
if(ht[cur].rchild!=-1){
code[len++]='1';
cur=ht[cur].rchild;
}
}
else
{
ht[cur].weight = 0;
cur = ht[cur].parent;
--len;
}
}
free(code);
}
int wet[100005];
int main(){
int n;
huffmancode hc;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&wet[i]);
}
node *t=NULL;
t=create_huffmantree(t,wet,n);
huffmancoding(t,hc,n);
for(int i=0;i<n;i++){
printf("%s\n",hc[i]);
}
return 0;
}
//带权路径长度:SUM(此节点到根节点的路径长度*权值)