首页 文章

Trie实现 - getWords和getWordsWithPrefix

提问于
浏览
0

我正在尝试用Java学习和实现Trie数据结构 . 这不是我正在尝试学习和实现trie的家庭作品,使用Arrays,而不是Map . 我正在寻找一种方法来获取trie中的所有单词,并在我的TrieNode实现中按照前缀获取所有单词

下面是我的Java代码

import java.util.Arrays;

public class Solution
{
  private TrieNode root;

  public Solution()
  {
    root = new TrieNode();
  }

  public void insert(String word)
  {
    TrieNode current = root;
    for(int index = 0;index < word.length();index++)
    {
      char value = word.charAt(index);
      if(!current.containsKey(value))
      {
        current.put(value, new TrieNode()); 
      }
      current = current.get(value);
    }
    current.setEnd();
  }

  public TrieNode searchPrefix(String word)
  {
    TrieNode current = root;
    for(int index = 0;index < word.length();index++)
    {
      char value = word.charAt(index);
      if(current.containsKey(value))
      {
        current = current.get(value);
      }
      else
      {
        return null;
      } 
    }
    return current;
  }

  public boolean search(String word)
  {
    TrieNode node = searchPrefix(word);
    return node != null && node.isEnd();
  }

  public boolean startsWith(String word)
  {
    TrieNode node = searchPrefix(word);
    return node != null;
  }

  // Return [the, there, by, bye]
  public List<String> getWords(TrieNode root)
  {
     StringBuilder builder = new StringBuilder();
     List<String> words = new ArrayList<String>();
     getWords(words, root, builder);
     return words;
  }

  public void getWords(List<String> words, TrieNode root, StringBuilder builder)
 {
    if(root.isEnd())
    {
      words.add(builder.toString());
      builder.delete(0, builder.length());
    }

    for(int index = 0;index < 26;index++)
    {
       if(root.links[index] != null)
       {
          builder.append((char) (index + 97));
          getWords(words, root.links[index], builder);
       }
     }
  }

  // Return [by, bye] when prefix = by
  public List<String> getWords(String prefix)
  {
    List<String> words = new ArrayList<String>();

    if(root.isEnd())
    {

    }

    for(int index = 0;index < 26;index++)
    {

    }
    return words;
  }



  public static void main(String[] args)
  {
    Solution trie = new Solution();
    trie.insert("the");
    trie.insert("there");
    trie.insert("by");
    trie.insert("bye");

    System.out.println(trie.search("thee"));
    System.out.println(trie.search("the"));
    System.out.println(trie.startsWith("by"));
    System.out.println(trie.search("by"));
    System.out.println(trie.wordCount(trie.root));
    System.out.println(trie.getWords(trie.root));
  }
}

class TrieNode
{
  public TrieNode[] links;
  private final int R = 26;
  private boolean isEnd;

  public TrieNode()
  {
    links = new TrieNode[R];
  }

  public boolean isEnd()
  {
    return isEnd;
  }

  public void setEnd()
  {
    this.isEnd = true;
  }

  public boolean containsKey(char value)
  {
    return links[value - 'a'] != null;
  }

  public void put(char value, TrieNode node)
  {
    links[value - 'a'] = node;
  }

  public TrieNode get(char value)
  {
    return links[value - 'a'];
  }
}

我正在寻找一种方法,使得getWords最终不会得到过滤器(isEnd())

getWords => [by, e, the, re]

任何输入都会有所帮助 .

谢谢 !

1 回答

  • 0

    不确定你需要帮助的确切功能,但我快速实现了空功能 .

    // Return [by, bye] when prefix = by
    public List<String> getWords(String prefix)
    {
        TrieNode trieNode = searchPrefix(prefix);
        if (trieNode != null) {
            List<String> words = new ArrayList<>();
            addAllWords(trieNode, prefix, words);
            return words;
        }
        return Collections.emptyList();
    }
    
    private void addAllWords(TrieNode node, String word, List<String> words)
    {
        if (node.isEnd()) {
            words.add(word);
        }
        for(int index = 0;index < 26;index++)
        {
            TrieNode next = node.get((char)(index + 'a'));
            if (next != null) {
                addAllWords(next, word + (char)(index + 'a'), words);
            }
        }
    }
    

    在TrieNode中添加get(int索引)可以简化一些事情,现在来回转换char .

相关问题