[Java]赫夫曼编码

对某一字符串的最短二进制编码称为赫夫曼编码。

package tree;


import java.util.Stack;

public class HuffmanTree {
    public static int[] min (Note[] note,int n) {   //选出所有树中权值最小的两个

        int min1 = Integer.MAX_VALUE - 1;
        int j = -1;
        int min2 = Integer.MAX_VALUE;
        int k = -1;
        for (int i = 0;i < n; i ++) {
            if (note[i].parent == -1 && note[i].weight < min1) {
                min1 = note[i].weight;
                j = i;
            } else if (note[i].parent == -1 && note[i].weight < min2) {
                min2 = note[i].weight;
                k = i;
            }
        }
        return new int[]{j,k};
    }
    public static void merge (Note[] note, int i, int j, int n) {    //合并两棵树
        note[i].parent = note[j].parent = n;
        note[n] = new Note(note[i].weight + note[j].weight,i,j,-1);
    }
    public static StringBuffer[] getCode (Note[] note) {
        int n = (note.length + 1) / 2;
        StringBuffer[] code = new StringBuffer[n];
        Stack<String> tem = new Stack<>();
        for (int i = 0; i < n; i ++) {
            int c = i;
            int p;
            while(note[c].parent != -1) {
                p = note[c].parent;
                if(note[p].lChild == c) {
                    tem.push("0");
                } else {
                    tem.push("1");
                }
                c = p;
            }
            code[i] = new StringBuffer();
            while (!tem.empty()) {
                code[i].append(tem.pop());
            }
        }
        return code;
    }
    public static StringBuffer getString (Note[] note, String code) {
        StringBuffer str = new StringBuffer();
        int n = note.length;
        int p = n - 1;
        for (int i = 0; i < code.length();i ++) {
            char ch = code.charAt(i);
            if (ch == '0') {
                p = note[p].lChild;
            } else {
                p = note[p].rChild;
            }
            if (note[p].lChild == -1) {
                str.append(note[p].ch);
                p = n - 1;
            }
         }
        return str;
    }
    public static void mid (Note[] note, int t) {  //中序遍历
        if (t == -1) return;
        System.out.print(note[t].weight + ",");
        mid(note,note[t].lChild);
        mid(note,note[t].rChild);
    }
    public static void main(String[] args) {
        int n = 5;       //叶子节点的数量
        char[] ch = new char[]{'a','b','c','d','e'}; //求编码的字符
        int[] weight = new int[]{5,15,40,30,10};   //每个字符的权值
        Note[] note = new Note[2 * n - 1];           //采用顺序存储结构
        for (int i = 0;i < n;i ++) {               //建立每一个叶子节点对象,并把每一个叶子节点看作一棵树
            note[i] = new Note(ch[i],weight[i],-1,-1,-1);
        }
        for (int i = n; i < 2 * n - 1; i ++) {
            int[] min = min(note, i);      //选出权值最小的两个跟节点
            merge(note,min[0],min[1],i);  //合并两个权值最小的树
        }
        mid(note,2 * n - 2);          //中序遍历,输出来看看
        System.out.println();
        StringBuffer[] code = getCode(note);   //获取赫夫曼编码
        for (int i = 0;i < n; i ++) {
            System.out.println(ch[i] + ":" + code[i]);
        }
        StringBuffer string = getString(note, "11111001101110");
        System.out.println(string);
    }
}
class Note {
    int weight;
    char ch;
    int lChild, rChild;
    int parent;
    public Note(int weight, int lChild, int rChild, int parent) {
        this.weight = weight;
        this.lChild = lChild;
        this.rChild = rChild;
        this.parent = parent;
    }
    public Note(char ch, int weight, int lChild, int rChild, int parent) {
        this.ch = ch;
        this.weight = weight;
        this.lChild = lChild;
        this.rChild = rChild;
        this.parent = parent;
    }
}

输出结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

M FS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值