java - Building a Trie Tree to hold words as added -
java - Building a Trie Tree to hold words as added -
hello building trie tree , code right me not add together words after first word added. @ , tell me think.
this trie class not working:
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 ) { homecoming find( root, word, 0 ); } public void add( string word ) { add( root, word, 0 ); } public string tostring() { homecoming traverse( root, "" ); } private static boolean find( trienode node, string word, int index ) { if(index == word.length()) homecoming node.terminal; if(node.children != null) for(int = 0; <= node.children.length; i++) if(word.charat(index) == node.children[i].letter) homecoming find(node.children[i], word, index+1); homecoming 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 cuts array add together new kid 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 = 0; < 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); homecoming triewords; } }
this test code add together words:
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 ); }
}
thank you
java recursion trie
Comments
Post a Comment