【问题标题】:Could anyone tell me why my code for this Huffman encoding algorithm is producing an error? [duplicate]谁能告诉我为什么我的这个霍夫曼编码算法的代码会产生错误? [复制]
【发布时间】:2012-11-06 03:18:37
【问题描述】:

我有一个霍夫曼树和一个字符,我想返回该字符在霍夫曼树中的编码应该是什么。

我已经使用广度优先遍历方法实现了它,每次检查左右树时,我都会检查树的数据是否等于我要查找的字符。不过,每次我向右或向左走时,我都会在编码中添加 0 或 1。最终,当我找到与树的数据相等的字符时,我返回该树的编码值。

代码:

    public static String findCharEncoding(BinaryTree<CharProfile> bTree, char character) {
        Queue<BinaryTree<CharProfile>> treeQueue = new LinkedList<BinaryTree<CharProfile>>();

        // Create a TreeWithEncoding object from the given arguments and add it to the queue
        treeQueue.add(bTree);

        while (!treeQueue.isEmpty()) {
            BinaryTree<CharProfile> t = treeQueue.remove();

->          if (t.getLeft().getData().getCharacter() == character) {
                return t.getLeft().getData().getEncoding();
            }
            if (t.getLeft() != null) {
                t.getLeft().getData().setEncoding(t.getLeft().getData().getEncoding() + "0");
                treeQueue.add(t.getLeft());
            }

            if (t.getRight().getData().getCharacter() == character) {
                return t.getRight().getData().getEncoding();
            }
            if (t.getRight() != null) {
                t.getRight().getData().setEncoding(t.getRight().getData().getEncoding() + "1");
                treeQueue.add(t.getRight());
            }
        }

        // If it gets to here, the while loop was unsuccessful in finding the encoding
        System.out.println("Unable to find.");
        return "-1";
    }

我的实现如下:

        for (int i = 0; i < charOccurrences.size(); i++) {
            char character = charOccurrences.get(i).getCharacter();

            charOccurrences.get(i).setEncoding(findCharEncoding(huffmanTree, character));
            System.out.println(charOccurrences.get(i).getEncoding());
        }

CharProfile 是一个自定义类,包含字符值、字符概率和编码。

它一直在if (t.getLeft().getData().getCharacter() == character) { 行返回 NullPointerExceptionError,我用箭头表示了这一点。我已经尝试过,但我似乎无法弄清楚为什么,除了返回错误的是t.getLeft().getData(),而不是整个事情,或者t.getLeft(),但我仍然无法弄清楚为什么。

【问题讨论】:

  • 首先使用您的调试器进入getData(),并找出它为什么在您不期望的情况下返回null
  • 发现错误的问题可能是找错地方了。引用的代码依赖于始终具有非空数据的树节点,因此如果 t.getRight() 不为空,则 t.getRight().getData() 也不为空。也许树的建筑弄错了。

标签: java binary-tree breadth-first-search huffman-code


【解决方案1】:

如果左边或右边的树分支实际上可以是null--如果这是 Huffman,可能只发生在叶节点上--为什么你在它们上调用 getData之前 你检查了吗?那肯定会导致 NullPointerExeption。

if (t.getLeft() != null) {
  // check nullity and THEN check match
  if (t.getLeft().getData().getCharacter() == character) {
    return t.getLeft().getData().getEncoding();
  }
  t.getLeft().getData().setEncoding(t.getLeft().getData().getEncoding() + "0");
  treeQueue.add(t.getLeft());
}

if (t.getRight() != null) {
  if (t.getRight().getData().getCharacter() == character) {
    return t.getRight().getData().getEncoding();
  }
  t.getRight().getData().setEncoding(t.getRight().getData().getEncoding() + "1");
  treeQueue.add(t.getRight());
}

附言如果您要返回0s 和1s 的位串,您还可能希望避免返回“-1”,并避免在每次findCharEncoding 时设置树节点的编码,但这只是一个疯狂的猜测. :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多