类 TrieSET

  • 所有已实现的接口:
    java.lang.Iterable<java.lang.String>

    public class TrieSET
    extends java.lang.Object
    implements java.lang.Iterable<java.lang.String>
    The TrieSET class represents an ordered set of strings over the extended ASCII alphabet. It supports the usual add, contains, and delete methods. It also provides character-based methods for finding the string in the set that is the longest prefix of a given prefix, finding all strings in the set that start with a given prefix, and finding all strings in the set that match a given pattern.

    This implementation uses a 256-way trie. The add, contains, delete, and longest prefix methods take time proportional to the length of the key (in the worst case). Construction takes constant time.

    For additional documentation, see Section 5.2 of Algorithms in Java, 4th Edition by Robert Sedgewick and Kevin Wayne.

    • 构造器概要

      构造器 
      构造器 说明
      TrieSET()
      Initializes an empty set of strings.
    • 方法概要

      修饰符和类型 方法 说明
      void add​(java.lang.String key)
      Adds the key to the set if it is not already present.
      boolean contains​(java.lang.String key)
      Does the set contain the given key?
      void delete​(java.lang.String key)
      Removes the key from the set if the key is present.
      boolean isEmpty()
      Is the set empty?
      java.util.Iterator<java.lang.String> iterator()
      Returns all of the keys in the set, as an iterator.
      java.lang.Iterable<java.lang.String> keysThatMatch​(java.lang.String pattern)
      Returns all of the keys in the set that match pattern, where . symbol is treated as a wildcard character.
      java.lang.Iterable<java.lang.String> keysWithPrefix​(java.lang.String prefix)
      Returns all of the keys in the set that start with prefix.
      java.lang.String longestPrefixOf​(java.lang.String query)
      Returns the string in the set that is the longest prefix of query, or null, if no such string.
      static void main​(java.lang.String[] args)
      Unit tests the TrieSET data type.
      int size()
      Returns the number of strings in the set.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • 从接口继承的方法 java.lang.Iterable

        forEach, spliterator
    • 构造器详细资料

      • TrieSET

        public TrieSET()
        Initializes an empty set of strings.
    • 方法详细资料

      • contains

        public boolean contains​(java.lang.String key)
        Does the set contain the given key?
        参数:
        key - the key
        返回:
        true if the set contains key and false otherwise
        抛出:
        java.lang.IllegalArgumentException - if key is null
      • add

        public void add​(java.lang.String key)
        Adds the key to the set if it is not already present.
        参数:
        key - the key to add
        抛出:
        java.lang.IllegalArgumentException - if key is null
      • size

        public int size()
        Returns the number of strings in the set.
        返回:
        the number of strings in the set
      • isEmpty

        public boolean isEmpty()
        Is the set empty?
        返回:
        true if the set is empty, and false otherwise
      • iterator

        public java.util.Iterator<java.lang.String> iterator()
        Returns all of the keys in the set, as an iterator. To iterate over all of the keys in a set named set, use the foreach notation: for (Key key : set).
        指定者:
        iterator 在接口中 java.lang.Iterable<java.lang.String>
        返回:
        an iterator to all of the keys in the set
      • keysWithPrefix

        public java.lang.Iterable<java.lang.String> keysWithPrefix​(java.lang.String prefix)
        Returns all of the keys in the set that start with prefix.
        参数:
        prefix - the prefix
        返回:
        all of the keys in the set that start with prefix, as an iterable
      • keysThatMatch

        public java.lang.Iterable<java.lang.String> keysThatMatch​(java.lang.String pattern)
        Returns all of the keys in the set that match pattern, where . symbol is treated as a wildcard character.
        参数:
        pattern - the pattern
        返回:
        all of the keys in the set that match pattern, as an iterable, where . is treated as a wildcard character.
      • longestPrefixOf

        public java.lang.String longestPrefixOf​(java.lang.String query)
        Returns the string in the set that is the longest prefix of query, or null, if no such string.
        参数:
        query - the query string
        返回:
        the string in the set that is the longest prefix of query, or null if no such string
        抛出:
        java.lang.IllegalArgumentException - if query is null
      • delete

        public void delete​(java.lang.String key)
        Removes the key from the set if the key is present.
        参数:
        key - the key
        抛出:
        java.lang.IllegalArgumentException - if key is null
      • main

        public static void main​(java.lang.String[] args)
        Unit tests the TrieSET data type.
        参数:
        args - the command-line arguments