-1

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

}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
  • where's stack trace and where exactly you get NPE? – SMA Dec 14 '14 at 07:28
  • Not in the above code. But trie.root.getChild(x) for all values of x is null. trie.insert("hello") should insert 'h' in the trie.root.child[7] and it should not be null as that node gets initialised inside the insert(String) function! – Shashank Tekriwal Dec 14 '14 at 07:34

2 Answers2

0

You've never set anything to TrieNode child[];, so it'll always be an array of null of type TrieNode.

Kai
  • 15,284
  • 6
  • 51
  • 82
  • constructor of class TrieNode has `this.child = new TrieNode[len]`. Please explain more. – Shashank Tekriwal Dec 14 '14 at 07:58
  • I think you may be coming from a different language, in Java all `new TrieNode[len]` does is creating an `array` with `len` entries, however each entry would be null. And even if they weren't null, your code still doesn't specify which child has valid entry. – Kai Dec 14 '14 at 08:09
0

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 !!!!

Ahmad MOUSSA
  • 2,729
  • 19
  • 31
  • basically, i was landing on a 'null' and so no trie was being formed at all. I was only updating temp and that was then lost. I think @Kai also meant the same. Thanks! – Shashank Tekriwal Dec 14 '14 at 09:53