【问题标题】:Recursive Huffman decode function not exiting the function递归霍夫曼解码函数不退出函数
【发布时间】:2019-04-17 16:45:20
【问题描述】:

我正在尝试编写一个 Huffman-tree 解码函数来解码给定的布尔数组。我在 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);
        }
    }
}

【问题讨论】:

  • 请只贴代码的相关部分。很难筛选出这么多代码来找到问题。

标签: java decode huffman-code


【解决方案1】:

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

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

【讨论】:

  • @TNugg 这对你有帮助吗?
猜你喜欢
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多