Search code examples
javanullpointerexceptiontrie

Trie data structure returning null even after insertion in java


I have a TrieNode class that defines my node of the trie. I have a trie class that uses TrieNode build a trie. My intention is to insert strings into the trie. I don't understand why is the output of my program null. There is some very fundamental mistake which i am unable to catch! In the main class search returns false...i am expecting true. The next for loop returns false for every value of x. i am expecting 'not null' for x = 7 because 'h' is stored at 7.

TrieNode Class:

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];
    }
}

My Trie Class:

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;
    }
}

Main class:

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));
        }
    }

}

Solution

  • Your error is here:

    this.child = new TrieNode[len];
    

    You will firstly set each TriNode in this array, in other words, if you try this code:

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

    You will find that all the childs are null !!!!