我正在尝试创建自己的Java Trie版本以获得一个,并获得制作一个所需的知识,但这个项目让我感到困惑。我这里有一个非常基本的破碎特里。
我在Trie中添加了3个单词(通过使用单词的第一个字符作为键,值为附加的TrieNodes)。如果你注意到,我在TrieNode类中有print语句,以便在程序运行时检查isWord的值。我希望单词中的最后一个字符将isWord变量设置为True。从而识别一个单词的结尾(我稍后会用它来获得整个单词)。
当我第一次输入三个单词时,输出会打印每个单词的每个字母,因为它们进入Trie并正确识别哪些字符是单词结尾。
但是,如果在输入单词后立即执行,我会遍历Trie并重新打印Trie中的所有字符及其isWord状态,“Hello”中的“e”现在突然被识别为单词结尾?
我已经倾倒了好几个小时,我只是不明白为什么会这样。以下是工作代码:
package testcode;
import java.util.*;
public class TestCode {
public static Trie t;
public static void main (String[] args){
t = new Trie();
t.addWord("hello");
t.addWord("hi");
t.addWord("soup");
//at this point the output correctly identifies word endings.
t.findWords();
/* but when iterating through the hash map it becomes evident that
* when entering the word 'hi' the 'e' in 'hello' had its isWord variable
* changed to true. I followed the logic and I do not see how or why this
* is happening.
*/
}
}
//This Trie class handles the root trie, and Trie commands.
class Trie{
private TrieNode root;
public Trie(){
root = new TrieNode();
}
public void addWord(String word){
root.addWord(word.toLowerCase());
}
public void findWords(){
root.findWords();
}
}
//Trie Node handles the nodes and words within the trie
class TrieNode{
private TrieNode parent;
private boolean isWord;
private boolean hasChildren;
private char character;
private Map<Character, TrieNode> children = new HashMap<>();
public TrieNode(){
hasChildren = false;
isWord = false;
}
public TrieNode(String word){
this();
addWord(word);
}
public void addWord(String word){
char firstChar = word.charAt(0);
if (children.get(firstChar) == null){
if(word.length() > 1){
hasChildren = true;
children.put(firstChar, new TrieNode(word.substring(1)));
children.get(firstChar).parent = this;
System.out.print(firstChar + "--");
System.out.println(isWord);
}
else{
children.put(firstChar, new TrieNode());
if(character == 'e'){
System.out.println("shits about to go down");
}
isWord = true;
System.out.print(firstChar + "--");
System.out.println(isWord);
}
children.get(firstChar).character = firstChar;
}
else {
children.get(firstChar).addWord(word.substring(1));
}
}
public void findWords(){
for(Character key : children.keySet()){
children.get(key).findWords();
System.out.println(children.get(key).character + " -- " + isWord);
}
}
}
此代码生成以下输出:
o--true
l--false
l--false
e--false
h--false
i--true
p--true
u--false
o--false
s--false
p -- true
u -- false
o -- false
s -- false
o -- true
l -- false
l -- false
e -- true //notice the e here is now suddenly a word ending with isWord = true
i -- true
h -- false
答案 0 :(得分:1)
存在一系列可能的问题。父母/孩子混淆,处理父节点上的叶子案例,无论是在建筑物还是在打印输出等。
我注意到你的旧词&#39;代码,您正在打印子角色但父亲&#39; isWord&#39;标志即可。构建trie之间存在不期望的差异,并且存在子节点&#34;和&#34;创建子节点路径&#34; - 这样就是&#39; isWord&#39;只能在新路径上标记,而不是在现有路径上标记。建立特里似乎似乎设置了'#Word&#39;在父节点而不是叶节点上。
一般而言,嵌套IF案例的意大利面很可能不可靠。代码应尽可能通用 - 将其保留在方法的主流中,除非确实确实属于IF内部。
这里的清洁&amp;正确的代码:
class TrieNode{
private TrieNode parent;
private boolean isWord;
private boolean hasChildren;
private char character;
private Map<Character, TrieNode> children = new HashMap<>();
public TrieNode(){
this.hasChildren = false;
this.isWord = false;
}
public TrieNode (char ch) {
this.character = ch;
this.hasChildren = false;
this.isWord = false;
}
public void addWord (String word){
if (word.length() == 0) {
this.isWord = true;
System.out.println( character + " -- " + isWord);
return;
}
// represent the Child Node;
// --
char firstChar = word.charAt(0);
TrieNode child = children.get( firstChar);
if (child == null){
child = new TrieNode( firstChar);
children.put( firstChar, child);
child.parent = this;
hasChildren = true;
}
// add Remaining Word;
// -- call for 1-length words, as 0-length at Child sets 'IsWord'!
child.addWord( word.substring(1));
// print building here.
System.out.println( character + " -- " + isWord);
}
public void findWords(){
for(Character key : children.keySet()){
children.get(key).findWords();
}
System.out.println( character + " -- " + isWord);
}
}