【问题标题】:Trie data structure returning null even after insertion in java即使在 java 中插入后,Trie 数据结构也会返回 null
【发布时间】:2023-09-25 12:44:01
【问题描述】:

我有一个 TrieNode 类,它定义了我的 trie 节点。 我有一个使用 TrieNode 构建 trie 的 trie 类。 我的意图是将字符串插入到 trie 中。我不明白为什么我的程序的输出为空。有一些我无法理解的非常根本的错误!在主类搜索中返回假...我期待真。对于 x 的每个值,下一个 for 循环都返回 false。我期待 x = 7 为“not null”,因为“h”存储在 7。

TrieNode 类:

package trie;

public class TrieNode {
    int colour;
    char letter;
    TrieNode child[];

    public TrieNode(int colour, char letter, int len){
        this.colour = colour;
        this.letter = letter;
        this.child = new TrieNode[len];
    }

    public TrieNode(int len){
        this.colour = 0;
        this.letter = '\\';
        this.child = new TrieNode[len];
    }

    public int getColour(){
        return this.colour;
    }

    public void setColour(int colour){
        this.colour = colour;
    }

    public char getLetter(){
        return this.letter;
    }

    public void setLetter(char letter){
        this.letter = letter;
    }

    public TrieNode getChild(int x){
        return this.child[x];
    }
}

我的 Trie 类:

package trie;

public class Trie {

    TrieNode root;

    public Trie(int len){
        root = new TrieNode(len);
    }

    public void insert(String str){
        str = str.toLowerCase();
        int length = str.length();
        TrieNode temp = root;
        for (int x = 0; x < length; x++){
            char ch = str.charAt(x);
            temp = temp.getChild((int)ch-97);
            System.out.println((int)ch-97);
            if(temp==null){
                temp = new TrieNode(0,ch,26);
            }
            if(temp!=null){
                System.out.println(temp.getLetter());
            }
            if(x==length-1){
                temp.setColour(1);
            }
        }
    }

    public boolean search(String str){
        str = str.toLowerCase();
        int length = str.length();
        TrieNode temp = root;
        for(int x = 0; x<length; x++){
            char ch = str.charAt(x);
            temp = temp.getChild((int)ch-97);
            if(temp==null || (x==length-1 && temp.getColour()==0)){
                return false;
            }else{
                System.out.println(temp.getLetter());
            }
        }
        return true;
    }
}

主类:

package trie;

public class Main {

    public static void main(String[] args) {
        Trie trie = new Trie(26);

        trie.insert("hello");
        System.out.println(trie.search("hello"));
        for(int x=0;x<=25;x++){
            System.out.println(x+"  "+trie.root.getChild(x));
        }
    }

}

【问题讨论】:

  • 堆栈跟踪在哪里以及您在哪里获得 NPE?
  • 上述代码中没有。但是对于 x 的所有值, trie.root.getChild(x) 都是空的。 trie.insert("hello") 应该在 trie.root.child[7] 中插入 'h' 并且它不应该为空,因为该节点在 insert(String) 函数中被初始化!

标签: java nullpointerexception trie


【解决方案1】:

你的错误在这里:

this.child = new TrieNode[len];

您将首先设置此数组中的每个 TriNode,换句话说,如果您尝试以下代码:

public TrieNode getChild(int x){
    System.out.println("Child["+x+"] "+this.child[x]);
    return this.child[x];
}

你会发现所有的孩子都是空的!!!!

【讨论】:

  • 基本上,我登陆的是一个'null',所以根本没有形成trie。我只是在更新温度,然后就丢失了。我认为@Kai 也有同样的意思。谢谢!
【解决方案2】:

您从未对TrieNode child[]; 设置任何内容,因此它始终是TrieNode 类型的空数组。

【讨论】:

  • 类 TrieNode 的构造函数有this.child = new TrieNode[len]。请解释更多。
  • 我认为您可能来自不同的语言,在 Java 中,所有 new TrieNode[len] 所做的是创建一个带有 len 条目的 array,但是每个条目都将为空。即使它们不为空,您的代码仍然没有指定哪个子项具有有效条目。