java.lang.Object
tk.airshipcraft.commonlib.utils.search.Trie

public final class Trie extends Object

Implements a trie (prefix tree) data structure that provides fast retrieval of strings based on their prefixes. It is commonly used for autocomplete systems, spell checkers, and other applications that require quick searches of a large collection of strings.

This trie supports inserting words and searching for all words that match a given prefix. It can also be used to suggest completions for partial words.

Since:
2023-03-30
Version:
1.0.0
Author:
notzune
  • Method Details

    • getNewTrie

      public static Trie getNewTrie()
      Factory method to create a new Trie with initialized children.
      Returns:
      A new instance of a Trie with an empty string and depth 0.
    • isLeaf

      public boolean isLeaf()
      Checks if this node of the trie is a leaf node (does not have any children).
      Returns:
      True if the node is a leaf, false otherwise.
    • insert

      public void insert(String wordToInsert)
      Inserts a word into the trie. If the word already exists, no changes are made. The insertion process constructs the necessary nodes to represent the word within the trie.
      Parameters:
      wordToInsert - The word to be inserted into the trie.
    • match

      public List<String> match(String prefix)
      Searches the trie for words that match a given prefix.
      Parameters:
      prefix - The prefix to match against words in the trie.
      Returns:
      A list of words that match the given prefix.
    • complete

      public List<String> complete(String[] args)
      Provides auto-completion suggestions based on the input arguments, treating them as space-separated parts of a prefix. This can be useful for command-line applications or chat interfaces where users type in commands or queries.
      Parameters:
      args - An array of strings representing parts of a command or query.
      Returns:
      A list of auto-completion suggestions based on the provided arguments.