欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何用Java實(shí)現(xiàn)啥夫曼編碼

 更新時(shí)間:2013年08月14日 10:25:10   作者:  
在開(kāi)發(fā)手機(jī)程序時(shí),總是希望壓縮網(wǎng)絡(luò)傳輸?shù)男畔?,以減少流量。本文僅以哈夫曼編碼為引導(dǎo),拋磚引玉,實(shí)現(xiàn)壓縮功能

大家可能會(huì)想,程序和第三方提供了很多壓縮方式,何必自己寫壓縮代碼呢?不錯(cuò),如GZIP這樣的壓縮工具很多,可是在某些情況下(如文本內(nèi)容小且字符不重復(fù)),GZIP壓縮后會(huì)比原始文本還要大。所以在某些特殊情況下用自己的壓縮方式可以更優(yōu)。

大家可能早已忘記了在學(xué)校學(xué)習(xí)的哈夫曼知識(shí),可以先在百度百科了解一下哈夫曼知識(shí):http://baike.baidu.com/view/127820.htm

哈夫曼思想:統(tǒng)計(jì)文本字符重復(fù)率,求出各字符權(quán)值,再構(gòu)造出一顆最優(yōu)二叉樹(shù)(又稱哈夫曼樹(shù)),然后給每個(gè)葉子結(jié)點(diǎn)生成一個(gè)以位(bit)為單位的碼值,每個(gè)碼值不能做為其 他碼值的前綴,再將碼值合并以每8個(gè)生成一個(gè)字節(jié)。

復(fù)制代碼 代碼如下:

package com.huffman;

/**
 * 結(jié)點(diǎn)
 * @author Davee
 */
public class Node implements Comparable<Node> {
    int weight;//權(quán)值
    Node leftChild;//左孩子結(jié)點(diǎn)
    Node rightChild;//右孩子結(jié)點(diǎn)
    String huffCode;
    private boolean isLeaf;//是否是葉子
    Character value;

    public Node(Character value, int weight) {
        this.value = value;
        this.weight = weight;
        this.isLeaf = true;
    }

    public Node(int weight, Node leftChild, Node rightChild) {
        this.weight = weight;
        this.leftChild = leftChild;
        this.rightChild = rightChild;
    }

    public void increaseWeight(int i) {
        weight += i;
    }

    public boolean isLeaf() {
        return isLeaf;
    }

    @Override
    public int compareTo(Node o) {
        return this.weight - o.weight;
    }
}


復(fù)制代碼 代碼如下:

package com.huffman;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class HuffmanTree {
    private boolean debug = false;

    private HashMap<Character, Node> nodeMap;
    private ArrayList<Node> nodeList;

    public HuffmanTree() {
        nodeMap = new HashMap<Character, Node>();
        nodeList = new ArrayList<Node>();
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }

    public String decode(Map<String, Character> codeTable, String binary) {
        int begin = 0, end = 1, count = binary.length();
        StringBuffer sb = new StringBuffer();
        while (end <= count) {
            String key = binary.substring(begin, end);
            if (codeTable.containsKey(key)) {
                sb.append(codeTable.get(key));
                begin = end;
            } else {
            }
            end++;
        }
        return sb.toString();
    }

    public String encode(String originText) {
        if (originText == null) return null;

        calculateWeight(originText);

//        if (debug) printNodes(nodeList);

        Node root = generateHuffmanTree(nodeList);

        generateHuffmanCode(root, "");

        if (debug) printNodes(root);

        StringBuffer sb = new StringBuffer();
        for (Character key : originText.toCharArray()) {
            sb.append(nodeMap.get(key).huffCode);
        }
        if (debug) System.out.println("二進(jìn)制:"+sb.toString());

        return sb.toString();
    }

    /**
     * 計(jì)算葉子權(quán)值
     * @param text
     */
    private void calculateWeight(String text) {
        for (Character c : text.toCharArray()) {
            if (nodeMap.containsKey(c)) {
                nodeMap.get(c).increaseWeight(1);//權(quán)值加1
            } else {
                Node leafNode = new Node(c, 1);
                nodeList.add(leafNode);
                nodeMap.put(c, leafNode);
            }
        }
    }

    /**
     * 生成哈夫曼樹(shù)
     * @param nodes
     */
    private Node generateHuffmanTree(ArrayList<Node> nodes) {
        Collections.sort(nodes);
        while(nodes.size() > 1) {
            Node ln = nodes.remove(0);
            Node rn = nodes.remove(0);
            insertSort(nodes, new Node(ln.weight + rn.weight, ln, rn));
        }
        Node root = nodes.remove(0);
        nodes = null;
        return root;
    }

    /**
     * 插入排序
     * @param sortedNodes
     * @param node
     */
    private void insertSort(ArrayList<Node> sortedNodes, Node node) {
        if (sortedNodes == null) return;

        int weight = node.weight;
        int min = 0, max = sortedNodes.size();
        int index;
        if (sortedNodes.size() == 0) {
            index = 0;
        } else if (weight < sortedNodes.get(min).weight) {
            index = min;//插入到第一個(gè)
        } else if (weight >= sortedNodes.get(max-1).weight) {
            index = max;//插入到最后
        } else {
            index = max/2;
            for (int i=0, count=max/2; i<=count; i++) {
                if (weight >= sortedNodes.get(index-1).weight && weight < sortedNodes.get(index).weight) {
                    break;
                } else if (weight < sortedNodes.get(index).weight) {
                    max = index;
                } else {
                    min = index;
                }
                index = (max + min)/2;
            }
        }
        sortedNodes.add(index, node);
    }

    private void generateHuffmanCode(Node node, String code) {
        if (node.isLeaf()) node.huffCode = code;
        else {
            generateHuffmanCode(node.leftChild, code + "0");
            generateHuffmanCode(node.rightChild, code + "1");
        }
    }

    /**
     * 生成碼表
     * @return
     */
    public Map<String, Character> getCodeTable() {
        Map<String, Character> map = new HashMap<String, Character>();
        for (Node node : nodeMap.values()) {
            map.put(node.huffCode, node.value);
        }
        return map;
    }

    /**
     * 打印節(jié)點(diǎn)信息
     * @param root
     */
    private void printNodes(Node root) {
        System.out.println("字符  權(quán)值  哈夫碼");
        printTree(root);
    }

    private void printTree(Node root) {
        if (root.isLeaf()) System.out.println((root.value == null ? "   " : root.value)+"    "+root.weight+"    "+(root.huffCode == null ? "" : root.huffCode));
        if (root.leftChild != null) printTree(root.leftChild);
        if (root.rightChild != null) printTree(root.rightChild);
    }

    /**
     * 打印節(jié)點(diǎn)信息
     * @param nodes
     */
    private void printNodes(ArrayList<Node> nodes) {
        System.out.println("字符  權(quán)值  哈夫碼");
        for (Node node : nodes) {
            System.out.println(node.value+"    "+node.weight+"    "+node.huffCode);
        }
    }
}


復(fù)制代碼 代碼如下:

package com.test;

import java.util.Map;

import com.huffman.HuffUtils;
import com.huffman.HuffmanTree;

public class Test {
    public static void main(String[] args) {
        String originText = "abcdacaha";
        HuffmanTree huffmanTree = new HuffmanTree();
        huffmanTree.setDebug(true);//測(cè)試
        String binary = huffmanTree.encode(originText);
        byte[] bytes = HuffUtils.binary2Bytes(binary);
        Map<String, Character> codeTable = huffmanTree.getCodeTable();
        int lastByteNum = binary.length() % 8;
        System.out.println(bytes.length);
        //將bytes、codeTable、 lastByteNum傳遞到服務(wù)器端
        //省略。。。。。。

        /*
                         服務(wù)器端解析
                         接收到參數(shù),并轉(zhuǎn)換成bytes、relationMap、 lastByteNum
        */
        String fullBinary = HuffUtils.bytes2Binary(bytes, lastByteNum);
        System.out.println("服務(wù)器二進(jìn)制:"+fullBinary);
        String retrieveText = huffmanTree.decode(codeTable, fullBinary);
        System.out.println("恢復(fù)文本:"+retrieveText);
    }
}

相關(guān)文章

  • SpringCloud配置客戶端ConfigClient接入服務(wù)端

    SpringCloud配置客戶端ConfigClient接入服務(wù)端

    這篇文章主要為大家介紹了SpringCloud配置客戶端ConfigClient接入服務(wù)端,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Go Java算法之解碼方法示例詳解

    Go Java算法之解碼方法示例詳解

    這篇文章主要為大家介紹了Go Java算法之解碼方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 自定義類加載器以及打破雙親委派模型解析

    自定義類加載器以及打破雙親委派模型解析

    這篇文章主要介紹了自定義類加載器以及打破雙親委派模型解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Java 集合系列(二)ArrayList詳解

    Java 集合系列(二)ArrayList詳解

    這篇文章主要介紹了Java集合系列ArrayList,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Java如何向主函數(shù)main中傳入?yún)?shù)

    Java如何向主函數(shù)main中傳入?yún)?shù)

    這篇文章主要介紹了Java如何向主函數(shù)main中傳入?yún)?shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Spring?Boot?集成JWT實(shí)現(xiàn)前后端認(rèn)證的示例代碼

    Spring?Boot?集成JWT實(shí)現(xiàn)前后端認(rèn)證的示例代碼

    小程序、H5應(yīng)用的快速發(fā)展,使得前后端分離已經(jīng)成為了趨勢(shì),本文主要介紹了Spring?Boot?集成JWT實(shí)現(xiàn)前后端認(rèn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Java并發(fā)編程Callable與Future的應(yīng)用實(shí)例代碼

    Java并發(fā)編程Callable與Future的應(yīng)用實(shí)例代碼

    這篇文章主要介紹了Java并發(fā)編程Callable與Future的應(yīng)用實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • SSM框架前后端信息交互實(shí)現(xiàn)流程詳解

    SSM框架前后端信息交互實(shí)現(xiàn)流程詳解

    這篇文章主要介紹了SSM框架前后端信息交互實(shí)現(xiàn)流程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • java.lang.String類的使用

    java.lang.String類的使用

    這篇文章主要介紹了java.lang.String類的使用,以及字符串的相關(guān)知識(shí),需要了解相關(guān)知識(shí)的小伙伴可以參考該篇文章
    2021-08-08
  • 基于tomcat8 編寫字符編碼Filter過(guò)濾器無(wú)效問(wèn)題的解決方法

    基于tomcat8 編寫字符編碼Filter過(guò)濾器無(wú)效問(wèn)題的解決方法

    下面小編就為大家分享一篇基于tomcat8 編寫字符編碼Filter過(guò)濾器無(wú)效問(wèn)題的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01

最新評(píng)論