小编use*_*572的帖子

获取Trie中的单词列表

我希望使用以下代码来检查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 …
Run Code Online (Sandbox Code Playgroud)

java trie

5
推荐指数
4
解决办法
2万
查看次数

标签 统计

java ×1

trie ×1