Class Trie

java.lang.Object
com.digicert.validation.psl.Trie

public class Trie extends Object
A Trie (prefix tree) implementation for storing and searching strings. The Trie supports insertion and search operations.

A Trie is a tree-like data structure that is used to store a dynamic set of strings, where the keys are usually strings. It is particularly useful for tasks such as autocomplete, spell checking, and IP routing. The Trie allows for efficient retrieval of strings by their prefixes, making it a powerful tool for various text processing applications.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final TrieNode
    Represents the root node in the Trie.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new Trie with an empty root node.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    insert(String word)
    Inserts a word into the Trie.
    boolean
    search(String word)
    Searches for a word in the Trie.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • root

      private final TrieNode root
      Represents the root node in the Trie.
  • Constructor Details

    • Trie

      public Trie()
      Constructs a new Trie with an empty root node.

      This constructor initializes the Trie with a root node that has no children. The root node acts as a placeholder and does not store any character. It is the foundation upon which the rest of the Trie is built.

  • Method Details

    • insert

      public void insert(String word)
      Inserts a word into the Trie.

      This method takes a string as input and inserts it into the Trie. It iterates over each character in the word, creating a new node if the character is not already present in the Trie. Once all characters are inserted, the last node is marked as the end of the word. This allows the Trie to store and recognize complete words.

      Parameters:
      word - the word to be inserted
    • search

      public boolean search(String word)
      Searches for a word in the Trie.

      This method takes a string as input and searches for it in the Trie. It traverses the Trie nodes corresponding to each character in the word. If it reaches a node that does not exist or if the final node is not marked as the end of a word, the search returns false. Otherwise, it returns true, indicating that the word is present in the Trie.

      Parameters:
      word - the word to search for
      Returns:
      true if the word is found and false otherwise