java - 递归霍夫曼解码函数不退出函数

标签 java decode huffman-code

我正在尝试编写一个霍夫曼树解码函数来解码给定的 boolean 数组。我在decode_helper()中使用递归方法,但我一直陷入无限循环,我不确定至于为什么,因为我认为我实现了一个适当的基本情况来停止递归调用。

我尝试过使用不同的基本情况,但我尝试的任何方法似乎都无法阻止递归调用。

public class HuffmanTree {
public class HuffmanTree {

// ******************** Start of Stub Code ******************** //
// ************************************************************ //
/** Node<E> is an inner class and it is abstract.
 * There will be two kinds
 * of Node, one for leaves and one for internal nodes. */
abstract static class Node implements Comparable<Node>{
    /** The frequency of all the items below this node */
    protected int frequency;

    public Node(int freq) {
        this.frequency = freq;
    }

    /** Needed for the Minimum Heap used later in this stub. */
    public int compareTo(Node other) {
        return this.frequency - other.frequency;
    }
}
/** Leaves of a Huffman tree contain the data items */
protected static class LeafNode extends Node {
    // Data Fields
    /** The data in the node */
    protected char data;
    /** Constructor to create a leaf node (i.e. no children) */
    public LeafNode(char data, int freq) {
        super(freq);
        this.data = data;
    }
    /** toString method */
    public String toString() {
        return "[value= "+this.data + ",freq= "+frequency+"]";
    }
}
/** Internal nodes contain no data,
 * just references to left and right subtrees */
protected static class InternalNode extends Node {
    /** A reference to the left child */
    protected Node left;
    /** A reference to the right child */
    protected Node right;

    /** Constructor to create an internal node */
    public InternalNode(Node leftC, Node rightC) {
        super(leftC.frequency + rightC.frequency);
        left = leftC; right = rightC;
    }
    public String toString() {
        return "(freq= "+frequency+")";
    }
}

// Enough space to encode all "extended ascii" values
// This size is probably overkill (since many of the values are not 
//"printable" in the usual sense)
private static final int codex_size = 256;

/* Data Fields for Huffman Tree */
private Node root;


public HuffmanTree(String s) {
    root = buildHuffmanTree(s);
}   

/**
 * Returns the frequencies of all characters in s.
 * @param s
 * @return
 */

//How many times a character shows up in a string
public static int[] frequency(String s) {
    int[] freq = new int[codex_size];
    for (char c: s.toCharArray()) {
        freq[c]++;
    }
    return freq;
}

public String decode(boolean[] coding) {
// TODO Complete decode method
//Function to decode the binary input

String code = "";
Node temp = root;

int i = 0;

if (coding.length == 0) {
    throw new IllegalArgumentException("The given code cannot be empty");
}

for(int j = 0; j < coding.length; j++) {
    if(coding[j] != true && coding[j] != false) {
        throw new IllegalArgumentException("The given code has an invalid 
input");
    }
}

decode_helper(temp, code, coding);

return code;
}



public void decode_helper(Node root, String code, boolean[] coding) {
    int i = 0;

    if(root == null) {
        throw new IllegalArgumentException("Given tree is empty");
    }
    //Base case for the recursion
    if(i != coding.length) {
        if (root instanceof InternalNode) {
            InternalNode n = (InternalNode)root;
            if(coding[i] == false) {
                n.left = (InternalNode)root;
                i++;
                decode_helper(n.left, code, coding);
            }
            if(coding[i] == true) {
                n.right = (InternalNode)root;
                i++;
                decode_helper(n.right, code, coding);
            }
        }
        else if (root instanceof LeafNode) {
            LeafNode l = (LeafNode)root;
            code += l.data;
            i++;
            decode_helper(root, code, coding);
        }
    }
}

最佳答案

问题是因为您正在 decode_helper 方法中初始化 int i = 0。并且该方法被递归调用。由于 i 始终初始化为零,因此它永远不会等于 coding.length ,因此会出现无限循环。

您可能需要在 decode_helper 方法外部初始化 i 并将其传递到其中。

关于java - 递归霍夫曼解码函数不退出函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55732465/

相关文章:

python - ascii 编解码器无法解码字节 0xe9

c - 如何以特定方式排列哈夫曼树

java - 距离编码 (DC) BWT

Java-基于 Https 的 SOAP Web 服务

Python,如何解码二进制编码的十进制(BCD)

java - Weblogic服务器中的@WebServlet

mongodb - 无法将 mongo 文档 ID 解码到结构字段上

java - 如何优化霍夫曼解码?

java - 如何使用 Hibernate for PostgreSQL 创建索引

java - 我的零钱计算器有缺陷?