huffman

#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(此节点到根节点的路径长度*权值)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值