有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java构建字典的数据结构

我正在寻找一些高层次的想法/想法,帮助我为字典构建数据结构。我有一个传统的“产品(药物)搜索系统”,它的速度非常慢,性质非常复杂。我们需要完全重新设计系统,以获得高效且可维护的解决方案

为了简化这个问题,我举了一个“Dictionary”的例子(我希望我的新系统的行为类似Dictionary)

  1. 我应该能够存储单词、描述和几个同义词(相当于普通药物)
  2. 文字不应重复
  3. 同义词也将是单词的实例(它应该包含单词的行为、描述和同义词)
  4. 更快的搜索

用例

  1. 搜索单词时,将显示其含义和同义词
  2. 更快的搜索
  3. 应该可以删除同义词
  4. 添加新词时,应该能够添加到任何现有单词的同义词中

我创建了一个如下所示的数据结构

Class Word {
    String meaning;
    List<Word> synonyms;
}

为了存储单词,我想使用TreeSet

因为

TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in sorted, ascending order. Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly.

或者我可以使用HashMap,其中word和同义词word实例的hashcode相等,这可以实现更快的检索

但我还是看到了很多挑战

  1. 添加新词时,如何与其同义词链接

  2. 当有大量单词时,查找会很慢

  3. 编辑单词也应反映同义词,反之亦然

任何想法/投入/技巧都将受到高度重视


共 (2) 个答案

  1. # 1 楼答案

    对于单词搜索和单词完成要求Trie将是一个快速的选择。看看Java implementations

    In computer science, a trie, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by prefixes), is an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings.

    http://pathakalgo.blogspot.in/2012/11/trie-data-structure-implementation-in.html

    https://www.google.co.in/search?q=Trie&client=ubuntu&channel=cs&oq=Trie&aqs=chrome..69i57j69i60l2.856j0j1&sourceid=chrome&ie=UTF-8

    对于同义词链接,可以维护Map<String, LinkedList<String>>。一旦使用Trie找到一个单词,获取相关的sysnonyms将是O(1)

  2. # 2 楼答案

    您可以使用Trie在字典中存储所有单词。为每个单词(节点)添加一个大纲列表