简体   繁体   English

搜索树实现

[英]Search Trie implementation

I have written a code StringTrie.java implemented through interface Trie.java:我写了一个通过接口 Trie.java 实现的代码 StringTrie.java:

package il.ac.tau.cs.sw1.trie;
import java.util.Set;

/**
 * Represents a generic trie data structure, where K is the type of keys that
 * are saved in the trie, and V is the type of values that can be saved for each
 * key.
 */
public interface Trie<K, V> {

    /**
     * Adds the input key to the data structure with the given value. If the key
     * is invalid for this trie (implementation-dependent) an exception is
     * thrown.
     */
    void addKey(K key, V value);

    /**
     * Searches all the keys in the trie structure with the given prefix. If
     * there are no such keys - an empty Set is returned. Otherwise, all the values saved
     * for these keys are added to the output value set. If the prefix is invalid for this
     * trie (implementation-dependent) an exception is thrown.
     * 
     * @post the return value is not null
     */
    Set<V> searchByPrefix(K prefix);

}

along with StringTrieNode.java implemented through TrieNode.java:连同通过 TrieNode.java 实现的 StringTrieNode.java:

package il.ac.tau.cs.sw1.trie;

import java.util.List;
import java.util.Set;

/**
 * Represents a node in a generic trie, where K is the type of keys that are
 * saved in the trie, and V is the type of values that can be saved for each
 * key.
 */
public interface TrieNode<K, V> {

    /**
     * Adds the key suffix to this trie node, with the given value, by
     * recursively adding the suffix of this suffix to the relevant child node.
     * 
     * @pre suffix is a valid suffix for this trie (implementation-dependent)
     * @post if the key ends in this trie node, the input value is added to the
     *       set of values returned by getValues().
     */
    void addSuffix(K suffix, V value);

    /**
     * Returns the set of values for keys that end in this node
     */
    Set<V> getValues();

    /**
     * Returns the child trie node such that the input suffix is saved in it or
     * in its descendant
     * 
     * @pre suffix is a valid suffix for this trie (implementation-dependent)
     * @post if no key with such a suffix exists in the trie, or if suffix ends
     *       in the current node, return null; otherwise return the relevant
     *       child node
     */
    TrieNode<K, V> getNext(K suffix);

    /**
     * Returns a list with all the child trie nodes of this node. The list may
     * contain null values.
     * 
     * @post $ret != null
     */
    List<TrieNode<K, V>> getNexts();

}

now, I have implemented all functions (correctly, I hope) and now I only have a problem with Set searchByPrefix(K prefix);, I can't find an algorithm that works + I have the problem with the constructor.现在,我已经实现了所有功能(我希望是正确的),现在我只有 Set searchByPrefix(K prefix); 的问题,我找不到有效的算法 + 我的构造函数有问题。 the user call it like this: Trie names = new StringTrie<>();so the first has to be there.用户这样称呼它: Trie names = new StringTrie<>(); 所以第一个必须在那里。 but only the second initials the fields.但只有字段的第二个首字母。 is it OK?可以吗?

I would recommend you to use a existing implementation.我建议您使用现有的实现。

Here is a very good examples that I already used multiple times:这是我已经多次使用的一个很好的例子:

https://github.com/rkapsi/patricia-trie https://github.com/rkapsi/patricia-trie

Here is an example to use the prefix search:以下是使用前缀搜索的示例:

PatriciaTrie<String, String> trie = new PatriciaTrie<String, String>(StringKeyAnalyzer.CHAR);
trie.put("prefix1", value);
trie.put("prefix2", value);

map = trie.prefixMap("prefix");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM