Implement Trie (Prefix Tree)

Implementing Trie alias as Prefix Tree

A prefix tree (or trie) is a special tree structure that helps store and quickly find strings, making it useful for tasks like auto-complete or spell checking.

Here’s what the PrefixTree class does:

  • PrefixTree(): Creates an empty prefix tree.
  • insert(String word): Adds a word to the prefix tree.
  • search(String word): Checks if a word exists in the tree. Returns true if the word is found, otherwise false.
  • startsWith(String prefix): Checks if any word in the tree starts with a given prefix. Returns true if such a word exists, otherwise false.
N Queen problem for Chess

Explanation:

  • PrefixTree prefixTree = new PrefixTree();
  • prefixTree.insert(“dog”);
  • prefixTree.search(“dog”); // return true
  • prefixTree.search(“do”); // return false
  • prefixTree.startsWith(“do”); // return true
  • prefixTree.insert(“do”);
  • prefixTree.search(“do”); // return true

Constraints:

  • 1 <= word.length, prefix.length <= 1000
  • word and prefix are made up of lowercase English letters.

Implementing Trie(Prefix Tree) Solution

Recommendation for Time and Space Complexity – Aim for O(n) time for each function call and O(t) space, where n is the length of the string and t is the total number of nodes in the Trie.

Hints for solving problems

Hint 1 :

A Trie is a tree-like structure where each node represents a character and has child nodes for the next characters. Each node has a flag to indicate if it’s the end of a word. The root node is the starting point, and words are stored by linking nodes based on their prefixes.

Hint 2 :

To insert a word, start from the root and iterate through each character. If a character is missing, create a new node for it. Move to the next character until the end, and then mark the last node as the end of the word.

Hint 3 :

To search for a word, follow the characters through the Trie. If a character is missing or the end-of-word flag isn’t set at the last node, return false.

There are mainly 2 approach to solve this problem-

  1. Prefix Tree(Array) Method
  2. Prefix Tree(Hash Map) Method

1. Prefix Tree(Array) Method

This approach uses an array to represent child nodes of each Trie node, typically of size 26 for lowercase English letters.

  • Each node maintains an array to store references to its child nodes and a flag to mark the end of a word.
  • This method is space-efficient for fixed character sets but less flexible for larger or dynamic character ranges.

–  Time complexity: O(n) for each function call.

–  Space complexity: O(t)

where n is the length of the string and t is the total number of TrieNodes created in the Trie.

Code

2. Prefix Tree(Hash Map) Method

This approach uses a hash map in each Trie node to store child nodes, where keys represent characters and values point to the corresponding child nodes. It is more flexible and memory-efficient for dynamic or sparse character sets, as it only stores nodes for existing characters.

  • Time complexity: O(n) for each function call.
  • Space complexity: O(t)

where n is the length of the string and t is the total number of TrieNodes created in the Trie.

Code

More Articles