Trie implementation

后端 未结 6 1931
醉梦人生
醉梦人生 2020-12-09 05:19

I am attempting to implement a very simple Trie in Java that supports 3 operations. I\'d like it to have an insert method, a has method (ie is a certain word in the trie),

6条回答
  •  情深已故
    2020-12-09 05:40

    Your has function should probably look like this:

    if (c[val]!=null && word.length()>1) {
        return c[val].has(word.substring(1)); //<-- Change is on this line
    } else if (c[val].flag==true && word.length()==1) {
        ...etc
    

    You perform the recursive call, but you really need to let that value propagate back out to the original caller.

提交回复
热议问题