gpt4 book ai didi

java - 从 Trie 中获取单词列表

转载 作者:搜寻专家 更新时间:2023-11-01 01:09:24 26 4
gpt4 key购买 nike

我希望使用以下代码不检查 Trie 中是否存在单词匹配,而是返回以用户输入的前缀开头的所有单词的列表。有人能指出我正确的方向吗?我根本无法让它工作......

public boolean search(String s)
{
Node current = root;
System.out.println("\nSearching for string: "+s);

while(current != null)
{
for(int i=0;i<s.length();i++)
{
if(current.child[(int)(s.charAt(i)-'a')] == null)
{
System.out.println("Cannot find string: "+s);
return false;
}
else
{
current = current.child[(int)(s.charAt(i)-'a')];
System.out.println("Found character: "+ current.content);
}
}
// If we are here, the string exists.
// But to ensure unwanted substrings are not found:

if (current.marker == true)
{
System.out.println("Found string: "+s);
return true;
}
else
{
System.out.println("Cannot find string: "+s +"(only present as a substring)");
return false;
}
}

return false;
}

}

最佳答案

我在尝试制作文本自动完成模块时遇到了这个问题。我通过制作一个 Trie 解决了这个问题,其中每个节点都包含它的父节点和子节点。首先,我搜索了从输入前缀开始的节点。然后我在 Trie 上应用了一个遍历,它以它的根作为前缀节点来探索子树的所有节点。每当遇到叶节点时,就意味着找到了从输入前缀开始的单词结尾。从那个叶节点开始,我遍历父节点获取父节点的父节点,并到达子树的根。在这样做的同时,我一直在堆栈中添加节点的键。最后,我采用了前缀并开始通过弹出堆栈来附加它。我一直将单词保存在 ArrayList 中。在遍历结束时,我得到了从输入前缀开始的所有单词。这是带有用法示例的代码:

class TrieNode
{
char c;
TrieNode parent;
HashMap<Character, TrieNode> children = new HashMap<Character, TrieNode>();
boolean isLeaf;

public TrieNode() {}
public TrieNode(char c){this.c = c;}
}

-

public class Trie
{
private TrieNode root;
ArrayList<String> words;
TrieNode prefixRoot;
String curPrefix;

public Trie()
{
root = new TrieNode();
words = new ArrayList<String>();
}

// Inserts a word into the trie.
public void insert(String word)
{
HashMap<Character, TrieNode> children = root.children;

TrieNode crntparent;

crntparent = root;

//cur children parent = root

for(int i=0; i<word.length(); i++)
{
char c = word.charAt(i);

TrieNode t;
if(children.containsKey(c)){ t = children.get(c);}
else
{
t = new TrieNode(c);
t.parent = crntparent;
children.put(c, t);
}

children = t.children;
crntparent = t;

//set leaf node
if(i==word.length()-1)
t.isLeaf = true;
}
}

// Returns if the word is in the trie.
public boolean search(String word)
{
TrieNode t = searchNode(word);
if(t != null && t.isLeaf){return true;}
else{return false;}
}

// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix)
{
if(searchNode(prefix) == null) {return false;}
else{return true;}
}

public TrieNode searchNode(String str)
{
Map<Character, TrieNode> children = root.children;
TrieNode t = null;
for(int i=0; i<str.length(); i++)
{
char c = str.charAt(i);
if(children.containsKey(c))
{
t = children.get(c);
children = t.children;
}
else{return null;}
}

prefixRoot = t;
curPrefix = str;
words.clear();
return t;
}


///////////////////////////


void wordsFinderTraversal(TrieNode node, int offset)
{
// print(node, offset);

if(node.isLeaf==true)
{
//println("leaf node found");

TrieNode altair;
altair = node;

Stack<String> hstack = new Stack<String>();

while(altair != prefixRoot)
{
//println(altair.c);
hstack.push( Character.toString(altair.c) );
altair = altair.parent;
}

String wrd = curPrefix;

while(hstack.empty()==false)
{
wrd = wrd + hstack.pop();
}

//println(wrd);
words.add(wrd);

}

Set<Character> kset = node.children.keySet();
//println(node.c); println(node.isLeaf);println(kset);
Iterator itr = kset.iterator();
ArrayList<Character> aloc = new ArrayList<Character>();

while(itr.hasNext())
{
Character ch = (Character)itr.next();
aloc.add(ch);
//println(ch);
}

// here you can play with the order of the children

for( int i=0;i<aloc.size();i++)
{
wordsFinderTraversal(node.children.get(aloc.get(i)), offset + 2);
}

}


void displayFoundWords()
{
println("_______________");
for(int i=0;i<words.size();i++)
{
println(words.get(i));
}
println("________________");

}



}//

例子

Trie prefixTree;

prefixTree = new Trie();

prefixTree.insert("GOING");
prefixTree.insert("GONG");
prefixTree.insert("PAKISTAN");
prefixTree.insert("SHANGHAI");
prefixTree.insert("GONDAL");
prefixTree.insert("GODAY");
prefixTree.insert("GODZILLA");

if( prefixTree.startsWith("GO")==true)
{
TrieNode tn = prefixTree.searchNode("GO");
prefixTree.wordsFinderTraversal(tn,0);
prefixTree.displayFoundWords();

}

if( prefixTree.startsWith("GOD")==true)
{
TrieNode tn = prefixTree.searchNode("GOD");
prefixTree.wordsFinderTraversal(tn,0);
prefixTree.displayFoundWords();

}

关于java - 从 Trie 中获取单词列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2794381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com