【问题标题】:Trie implementation - getWords and getWordsWithPrefixTrie 实现 - getWords 和 getWordsWithPrefix
【发布时间】:2018-01-23 23:28:35
【问题描述】:

我正在尝试在 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]

任何输入都会有所帮助。

谢谢!

【问题讨论】:

  • 您面临的问题是什么?你的代码没有编译。 ?

标签: java arrays trie


【解决方案1】:

不确定您需要帮助的具体功能,但我快速实现了空功能。

// 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 index) 会简化一些事情,现在 char 会来回转换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-30
    • 2010-10-15
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多