构建一个Trie Tree来保存添加的单词

时间:2014-10-30 18:15:50

标签: java recursion trie

你好我正在构建一个trie树,我的代码看起来对我来说,但是在添加第一个单词后没有添加任何单词。你能看看并告诉我你的想法。

这是我的Trie课程无效:

public class Trie {
    private static class TrieNode {
        public char letter;
        public boolean terminal;
        public TrieNode[] children; 
    }

    private TrieNode root;

    public Trie() {
        root = new TrieNode();
        root.letter = ' ';
        root.terminal = false;
        root.children = null;
    }

    public boolean find( String word ) {
        return find( root, word, 0 );   
    }

    public void add( String word ) {
        add( root, word, 0 );
    }

    public String toString() {
        return traverse( root, "" );
    }

    private static boolean find( TrieNode node, String word, int index ) {
        if(index == word.length())
            return node.terminal;
        if(node.children != null)
            for(int i = 0; i <= node.children.length; i++)
                if(word.charAt(index) == node.children[i].letter)
                    return find(node.children[i], word, index+1);
        return false;
    }

    private static void add( TrieNode node, String word, int index ) {
        if(index == word.length())
            return;
        if(node.children == null){
            node.children = new TrieNode[1];
            TrieNode y = new TrieNode();
            y.letter = word.charAt(index);
            y.children = null;
            node.children[0] = y;
            add(y, word, index +1);
        }
        else if(node.children != null){
            TrieNode y = new TrieNode();
            y.letter = word.charAt(index);
            y.children = null;
            /*for(int x = 0; x <= node.children.length -1 ; x++)
             *  if(node.children[x].letter == word.charAt(index)){
             *  add(node.children[x], word, index +1);
             *  return;
                }*/
            int cutter=0; //ascii val named cutter because its where it cuts the array to add new child
            while (cutter < node.children.length && node.children[cutter].letter < word.charAt(index)){
                cutter += 1;
                if(cutter == node.children.length){
                    TrieNode[] temp = new TrieNode[node.children.length +1];
                    for(int a = 0; a < temp.length -1; a++){
                        temp[a] = node.children[a];
                    }
                    temp[cutter] = y;
                    node.children = temp;
                }
                else if(node.children[cutter].letter != word.charAt(index)){
                    TrieNode[] temp = new TrieNode[node.children.length +1];
                    for(int b = 0; b < cutter; b++)
                        temp[b] = node.children[b];
                    temp[cutter] = y;
                    for(int c = cutter + 1; c < temp.length; c++)
                        temp[c] = node.children[c];
                    node.children = temp;
                    if(word.length() == index)
                        node.children[cutter].terminal = true;
                    add(node.children[cutter], word, index +1);
                }
            }
        }
    }

    private static String traverse( TrieNode node, String prefix ) {
        String trieWords = "";
        if(node.terminal)
            trieWords += prefix +'\n';
        if(node.children != null)
            for(int z=0; z<node.children.length; z++)
                trieWords += traverse(node.children[z], prefix + node.children[z].letter);
        return trieWords;   
    }
}

这是我添加单词的测试代码:

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

    trie.add( "madness" );
    trie.add( "mad" );
    trie.add( "ba" );
    trie.add( "bad" );
    trie.add( "bam" );
    trie.add( "mad" );
    trie.add( "a" );
    trie.add( "an" );
    trie.add( "arm" );
    trie.add( "an" );
    trie.add( "an" );
    trie.add( "kidney" );
    trie.add( "kid" );
    trie.add( "zebra" );

    System.out.println( trie );
}

}

谢谢

0 个答案:

没有答案