简体   繁体   中英

Reconstuct Huffman Tree for Decoding

I have encodings for compressed string data using Huffman Compression ie "more money needed"

Encoding

\n 0110
   1011
d  100
e  11
m  001
n  000
o  010
r  0111
y  1010
**
001010011111101100101000011101010110001111100111000110

I want to reconstruct the Huffman Tree in java to decode the encoding. Any implementation or example for such decoding.

I tried and coded the perfect solution.

public class HuffmanTree {

    public Node root;

    public HuffmanTree(){
        this.root = new Node();
    }

    public void add(char data, String sequence){

        Node temp = this.root;
        int i = 0;
        for(i=0;i<sequence.length()-1;i++){

          if(sequence.charAt(i)=='0'){
                if(temp.left == null){
                    temp.left = new Node();
                    temp = temp.left;
                }
                else{
                   temp = (Node) temp.left;
                }
            }
            else
              if(sequence.charAt(i)=='1'){
                if(temp.right == null){
                    temp.right = new Node();
                    temp = temp.right;
                }
                else{
                    temp = (Node) temp.right;
                }
         }}

        if(sequence.charAt(i)=='0'){

            temp.left = new Node(data); 
           }
        else{
            temp.right = new Node(data); 

        }
        }

    public String getDecodedMessage(String encoding){

        String output = "";
        Node temp = this.root;
        for(int i = 0;i<encoding.length();i++){

            if(encoding.charAt(i) == '0'){
                temp = temp.left;

                if(temp.left == null && temp.right == null){
                    output+= temp.getData();
                    temp = this.root;
                }
            }
            else
            {
                temp = temp.right;
                if(temp.left == null && temp.right == null){
                    output+= temp.getData();
                    temp = this.root;  
                }

            }
        }
        return output;
    }
    // Traversal of reconstructed huffman tree for debugging.
    public void traversal(Node node){

        if(node == null)
              return;
        System.out.println(node);
        traversal(node.left);
        traversal(node.right);

    }

    }


class Node{

    Node left;
    Node right;
    char data;

    public Node(){

    }
    public Node(char data){
        this.data = data;
    }
    public void setData(char data){
        this.data = data;
    }
    public char getData(){
        return this.data;
    }
    @Override
    public String toString(){
       if(this.data == Character.UNASSIGNED){
           return "No Value";
       } 
       else
           return ""+this.data;
    }
}

If you have the encoded message in a text file the space character may cause any issue so I have written code for that one also.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


    public class Test {

       public static void main(String[] bscs){

           HuffmanTree tree = new HuffmanTree();

            String inputFile;
            String outputFile;

            Scanner kb = new Scanner(System.in);
            System.out.println("Please enter the name of the Input File");
            inputFile = kb.nextLine();

            File f = new File(inputFile);
                Scanner fr = null;
            try {
                fr = new Scanner(new File(inputFile));
                fr.nextLine();
                tree.add('\n', fr.nextLine().trim());
                String temp = fr.nextLine();
                if(temp.charAt(0)==' ' && temp.charAt(1)==' ')
                {
                    tree.add(' ', temp.trim());
                }
                else
                    tree.add(temp.charAt(0), temp.substring(1));
                while(fr.hasNext()){
                    temp = fr.nextLine();
                    if(temp.equals("**")){
                        break;
                    }
                    else
                        tree.add(temp.charAt(0), temp.substring(1));
                }
                FileWriter f0 = new FileWriter(new File("decoded.ou"));
                f0.write(tree.getDecodedMessage(fr.nextLine()));
                f0.close();

            } catch (Exception ex) {
               System.out.println(ex.getMessage());
            }


            }

    }

First off, you don't need to reconstruct the Huffman tree. You can simply search linearly for the code that matches the next set of bits. Since it is a prefix code, there is a unique solution. So the first match is the right match.

If you want to make a tree, simply start with the first bit which gives you two choices. 0 left, 1 right. Neither of those is a code, so both branch on the second bit, same thing. One of the four ends there at the code 11 for e. Now branch the remaining three on the third bit. Four of the six end there with a code. Branch the remaining two. Those four all end in a code and you're done. Now you can use the tree to decode, looking at one bit at a time until you get to a code. Emit the code, and start back at the root of the tree for the next bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM