RedEnginePress logo
RedEnginePress
AlgorithmsLanguagesPlaygroundAbout

Trie

G
D
A
R
p
M
O
M
and 3 more contributors
class TrieNode {
  constructor(key, parent) {
    this.key = key
    this.count = 0
    this.children = Object.create(null)
    if (parent === undefined) {
      this.parent = null
    } else {
      this.parent = parent
    }
  }
}

class Trie {
  constructor() {
    // create only root with null key and parent
    this.root = new TrieNode(null, null)
  }

  // Recursively finds the occurrence of all words in a given node
  static findAllWords(root, word, output) {
    if (root === null) return
    if (root.count > 0) {
      if (typeof output === 'object') {
        output.push({ word, count: root.count })
      }
    }
    let key
    for (key in root.children) {
      word += key
      this.findAllWords(root.children[key], word, output)
      word = word.slice(0, -1)
    }
  }

  insert(word) {
    if (typeof word !== 'string') return
    if (word === '') {
      this.root.count += 1
      return
    }
    let node = this.root
    const len = word.length
    let i
    for (i = 0; i < len; i++) {
      if (node.children[word.charAt(i)] === undefined) {
        node.children[word.charAt(i)] = new TrieNode(word.charAt(i), node)
      }
      node = node.children[word.charAt(i)]
    }
    node.count += 1
  }

  findPrefix(word) {
    if (typeof word !== 'string') return null
    let node = this.root
    const len = word.length
    let i
    // After end of this loop node will be at desired prefix
    for (i = 0; i < len; i++) {
      if (node.children[word.charAt(i)] === undefined) return null // No such prefix exists
      node = node.children[word.charAt(i)]
    }
    return node
  }

  remove(word, count) {
    if (typeof word !== 'string') return
    if (typeof count !== 'number') count = 1
    else if (count <= 0) return

    // for empty string just delete count of root
    if (word === '') {
      if (this.root.count >= count) this.root.count -= count
      else this.root.count = 0
      return
    }

    let child = this.root
    const len = word.length
    let i, key
    // child: node which is to be deleted
    for (i = 0; i < len; i++) {
      key = word.charAt(i)
      if (child.children[key] === undefined) return
      child = child.children[key]
    }

    // Delete no of occurrences specified
    if (child.count >= count) child.count -= count
    else child.count = 0

    // If some occurrences are left we don't delete it or else
    // if the object forms some other objects prefix we don't delete it
    // For checking an empty object
    // https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
    if (
      child.count <= 0 &&
      Object.keys(child.children).length &&
      child.children.constructor === Object
    ) {
      child.parent.children[child.key] = undefined
    }
  }

  findAllWords(prefix) {
    const output = []
    // find the node with provided prefix
    const node = this.findPrefix(prefix)
    // No such prefix exists
    if (node === null) return output
    Trie.findAllWords(node, prefix, output)
    return output
  }

  contains(word) {
    // find the node with given prefix
    const node = this.findPrefix(word)
    // No such word exists
    return node !== null && node.count !== 0
  }

  findOccurrences(word) {
    // find the node with given prefix
    const node = this.findPrefix(word)
    // No such word exists
    if (node === null) return 0
    return node.count
  }
}

export { Trie }
About this Algorithm

A trie (also called a prefix tree) is a tree data structure that shows order, linking parents to children. It is an efficient way of storing objects that have commonalities. A good example would be in storing phone numbers, or strings in general

For the strings example, supposing we have a list of strings to store in our data store

  1. egg
  2. eat
  3. ear
  4. end

And one of the methods we are to support is a search operation for any of the words, we can approach it the basic way - select each word, and do a string comparison, matching letter to letter. The algorithm would be as follows:

## searching for ear in data store

data_store = ["egg", "eat", "ear", "end"]
to_find = "ear"

## pick each word
## do a string match letter by letter
## when you find a mismatch, move to the next string
## continue this process
## if at the end of an iteration, index has been increased to
## the length of the word to find, we have found a match

for word in data_store:
    index = 0
    while index < len(word):
        if to_find[index] != word[index]:
            break
        index += 1
    if index == len(to_find):
        print("a match has been found")

Without a doubt, this strategy will work, but the time complexity of doing this is O(num of words x len of longest word) which is quite expensive. However, if we represent the storage of numbers in a tree such that each letter appears only once in a particular level in the tree, we can achieve a much better search time. Take, for example, the tree below

        e
     /  |  \
    a   n   g
   / \  |   |
  r   t d   g

You can see from the above representation, that all the words are in the tree, starting from the letter e, which is found at the beginning of all the words, then a, n, and g coming in the next level and so on... The above representation is called a trie.

Standard Trie Operations

  1. insert(): inserts the string into the trie.
  2. search(): searches for the string within the trie.

Building a Trie

Defining a node class for the elements of the trie

To start building a trie, you first need to define a node with the revelant attributes needed for any trie.

class Node:
    def __init__(self, is_word: bool=False):
        self.is_word = is_word
        self.children = {}

Here, you can see that the class Node has three instance attributes:

  1. is_word: bool = to mark whether that node in the trie marks the completion of a word
  2. children: Dict = to hold pointers to other children nodes

Then the trie gets built by creating a node for each letter and adding it as a child to the node before it

Building the trie itself

Start by initializing an empty node

class Trie:
    def __init__(self):
        self.node = Node()

For the insert operation, fetch the starting node, then for every letter in the word, add it to the children of the letter before it. The final node has its is_word attribute marked as True because we want to be aware of where the word ends

def insert(self, word: str) -> None:
    node = self.node
    for ltr in word:
        if ltr not in node.children:
            node.children[ltr] = Node()
        node = node.children[ltr]
    node.is_word=True

In the code above, the node variable starts by holding a reference to the null node, while the ltr iterating variable starts by holding the first letter in word. This would ensure that node is one level ahead of ltr. As they are both moved forward in the iterations, node will always remain one level ahead of ltr

For the search operation, fetch the starting node, then for every letter in the word, check if it is present in the children attribute of the current node. As long as it is present, repeat for the next letter and next node. If during the search process, we find a letter that is not present, then the word does not exist in the trie. If we successfully get to the end of the iteration, then we have found what we are looking for. It is time to return a value

Take a look at the code

def search(self, word: str) -> bool:
    node = self.node
    for ltr in word:
        if ltr not in node.children:
            return False
        node = node.children[ltr]
    return node.is_word

For the return value, there are two cases:

  1. we are searching for a word -> return node.is_word because we want to be sure it is actually a word, and not a prefix
  2. we are searching for a prefix -> return True because whether it is a word or not, it is prefix that exists in the trie

Now here is the full code

class Node:
    def __init__(self, is_word: bool=False):
        self.is_word = is_word
        self.children = {}

class Trie:

    def __init__(self):
        self.node = Node()
        

    def insert(self, word: str) -> None:
        node = self.node
        for ltr in word:
            if ltr not in node.children:
                node.children[ltr] = Node()
            node = node.children[ltr]
        node.is_word=True
        

    def search(self, word: str) -> bool:
        node = self.node
        for ltr in word:
            if ltr not in node.children:
                return False
            node = node.children[ltr]
        return node.is_word

Helpful links

  1. Trie Data Structure - GeeksForGeeks

Video Playlist