20,182,327 2019-2020 "Program Design and Data Structures" Huffman encoding test report

20,182,327 2019-2020 "Program Design and Data Structures" Huffman encoding test report

  • Course: "Programming and Data Structures"
  • Class: 1823
  • Name: Zhao Tianhao
  • Student ID: 20182327
  • Experiment Teacher: Wang Zhiqiang
  • Experiment Date: November 17, 2019
  • Compulsory / Elective: Compulsory

Textbooks Huffman

  • 1, the computer data processing, Huffman coding using variable length coding tables for source symbols (e.g. a letter file) is encoded, wherein the variable length coding table is achieved by a method of occurrence probability of source symbols obtained evaluation, letter appears a high probability of using a shorter code, whereas the machine is low appears longer codes are used, this would mean that the length of the string after encoding, to reduce the expected value, so as to achieve lossless compression data.
  • 2, for example, in English, the highest occurrence probability e, z and the probability of occurrence is minimum. When using a Huffman compression coding on an English, E is likely to represent one bit, and z may spent 25 bits (not 26). With ordinary representation, they are each letter occupies one byte, i.e. 8 bits. Both compared to, e-encoded using general length of 1/8, z is used more than three times. If we can achieve a more accurate estimate for the probability of occurrence of each letter in English, it can greatly improve the lossless compression ratio.

  • 3, also known as optimal binary Huffman tree is a shortest path length weighted binary tree. Weighted path length of the so-called tree, the tree is the weight of all the leaf nodes of the path length multiplied by the value to which the root node (if the root node of layer 0, the root node to the leaf node is a leaf path length layers junction points). Path length is a length of the tree from the root node to each path and recorded as WPL = (W1XL1 + W2XL2 + W3XL3 + ... + WnXLn), N th weights Wi (i = 1,2, .. .n) constituting a binary tree has N leaf nodes, the path length of the corresponding leaf node is Li (i = 1,2, ... n). WPL can prove Huffman tree is minimal.

Huffman tree and testing

  • Reference materials to create java Huffman trees and Huffman --java Huffman coding, while trying to read the code, add your own understanding.
  • Firstly, the Huffman tree node class} [HuffmanNode node using the generic implementation, define the left subtree, right subtree, weights and coding.

    ```
    public class HuffmanTreeNode implements Comparable {
    protected int weight; // 权值
    protected HuffmanTreeNode left; // 左孩子
    protected HuffmanTreeNode right; // 右孩子
    protected HuffmanTreeNode parent; // 父结点
    protected char element;
    protected HuffmanTreeNode(int weight, char element, HuffmanTreeNode left, HuffmanTreeNode right, HuffmanTreeNode parent) {
    this.weight = weight;
    this.element = element;
    this.left = left;
    this.right = right;
    this.parent = parent;
    }
    @Override
    public int compareTo(HuffmanTreeNode o) {
    if (this.weight > o.weight)
    return 1;
    else if (this.weight < o.weight)
    return -1;
    else
    return 0;
    }

···

  • Txt file read write the following code letters, for the letters is converted into Huffman coding.

    ~~~
    public static void main(String[] args) throws IOException {
    File file = new File("C:\Users\12441\Desktop\game\input1.txt");
    Scanner scan = new Scanner(file);
    String s = scan.nextLine();
    System.out.println(s);
    int[] array = new int[26];
    for (int i = 0; i < array.length; i++) {
    array[i] = 0;
    }
    for (int i = 0; i < s.length(); i++) {
    char x = s.charAt(i);
    array[x - 'a']++;
    ···

  • [Class] getEnCoding write the read letter converted into Huffman coding, and specifies the left subtree is 0, 1 right subtree

 public String[] getEncoding() {
        ArrayList<HuffmanTreeNode> arrayList = new ArrayList();
        inOrder(root,arrayList);
        for (int i=0;i<arrayList.size();i++)
        {
            HuffmanTreeNode node = arrayList.get(i);
            String result ="";
            int x = node.element-'a';
            Stack stack = new Stack();
            while (node!=root)
            {
                if (node==node.parent.left)
                    stack.push(0);
                if (node==node.parent.right)
                    stack.push(1);
                node=node.parent;
            }
            while (!stack.isEmpty())
            {
                result +=stack.pop();
            }
            codes[x] = result;
        }
        return codes;
    }
    public HuffmanTreeNode getRoot() {
        return root;
    }
  • After completion of the test preparation of the final implementation class Huffman] [input1.txt read information from, the conversion after Huffman encoding into Huffman coding output.txt.
public class RunHuffmanTree {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\12441\\Desktop\\game\\input1.txt");
        Scanner scan = new Scanner(file);
        String s = scan.nextLine();
        System.out.println(s);
        int[] array = new int[26];
        for (int i = 0; i < array.length; i++) {
            array[i] = 0;
        }
        for (int i = 0; i < s.length(); i++) {
            char x = s.charAt(i);
            array[x - 'a']++;
        }
        System.out.println("打印各字母出现频率:");
        for (int i = 0; i < array.length; i++) {
            System.out.print((char) ('a' + i) + ":" + (double) array[i] / s.length() + " ");
        }
        HuffmanTreeNode[] huffmanTreeNodes = new HuffmanTreeNode[array.length];
        for (int i = 0; i < array.length; i++) {
            huffmanTreeNodes[i] = new HuffmanTreeNode(array[i], (char) ('a' + i), null, null, null);
        }

        HuffmanTree huffmanTree = new HuffmanTree(huffmanTreeNodes);
        System.out.println("打印各字母的编码");
        String[] codes = huffmanTree.getEncoding();
        for (int i = 0; i < codes.length; i++) {
            System.out.println((char) ('a' + i) + ":" + codes[i]);
        }
        //进行编码:
        String result = "";
        for (int i = 0; i < s.length(); i++) {
            int x = s.charAt(i) - 'a';
            result += codes[x];
        }
        System.out.println("编码结果:" + result);
        //写入文件
        File file1 = new File("C:\\Users\\12441\\Desktop\\game\\output.txt");
        FileWriter fileWriter = new FileWriter(file1);
        fileWriter.write(result);
        fileWriter.close();
        //从文件读取
        Scanner scan1 = new Scanner(file1);
        String s1 = scan1.nextLine();
        HuffmanTreeNode huffmanTreeNode = huffmanTree.getRoot();
        //进行解码
        String result2 = "";
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) == '0') {
                if (huffmanTreeNode.left != null) {
                    huffmanTreeNode = huffmanTreeNode.left;
                }
            } else {
                if (s1.charAt(i) == '1') {
                    if (huffmanTreeNode.right != null) {
                        huffmanTreeNode = huffmanTreeNode.right;
                    }
                }
            }
            if (huffmanTreeNode.left == null && huffmanTreeNode.right == null) {
                result2 += huffmanTreeNode.element;
                huffmanTreeNode = huffmanTree.getRoot();
            }
        }
        System.out.println("解码结果:" + result2);
        //写入文件
        File file2 = new File("C:\\Users\\12441\\Desktop\\game\\file2.txt");
        FileWriter fileWriter1 = new FileWriter(file1);
        fileWriter1.write(result2);
        // 判断解码后原来是否一致
        System.out.println("编码解码后原来是否一致:" + result2.equals(s));
    }

}

Experimental results


  • https://gitee.com/besti1823/2012_327_zhao_tianhao.git

Guess you like

Origin www.cnblogs.com/kv888777/p/11895572.html