Wednesday, October 28, 2020

Trie | Part 1 Introduction

 Trie (pronounce as "Try") is a very popular data structure, usually known as Prefix Tree as well.

Applications:

1. AutoComplete

2. Spell Checker

3. IP Routing (Longest Prefix Matching)

There are several other data structures, like balanced trees and hash tables, which give us the possibility to search for a word in a dataset of strings. Then why do we need trie? Although hash table has O(1) time complexity for looking for a key, it is not efficient in the following operations :

Finding all keys with a common prefix.

Enumerating a dataset of strings in lexicographical order.

Another reason why trie outperforms hash table, is that as hash table increases in size, there are lots of hash collisions and the search time complexity could deteriorate to O(n), where n is the number of keys inserted. Trie could use less space compared to Hash Table when storing many keys with the same prefix. In this case using trie has only O(m) time complexity, where mm is the key length. Searching for a key in a balanced tree costs O(mlogn) time complexity.


Trie node structure

Trie is a rooted tree. Its nodes have the following fields:

Maximum of R links to its children, where each link corresponds to one of R character values from dataset alphabet. In this article we assume that R is 26, the number of lowercase latin letters.

Boolean field which specifies whether the node corresponds to the end of the key, or is just a key prefix.












static class TrieNode{

        TrieNode[] children=new TrieNode[26];
        boolean isEnd=false;
    }


Two of the most common operations in a trie are insertion of a key and search for a key.

Insertion of a key to a trie

We insert a key by searching into the trie. We start from the root and search a link, which corresponds to the first key character. There are two cases :

A link exists. Then we move down the tree following the link to the next child level. The algorithm continues with searching for the next key character.

A link does not exist. Then we create a new node and link it with the parent's link matching the current key character. We repeat this step until we encounter the last character of the key, then we mark the current node as an end node and the algorithm finishes.













    /** Inserts a word into the trie. */

    public void insert(String word) {

        TrieNode node=root;

        for(int i=0;i<word.length();i++){

            char ch=word.charAt(i);

            if(node.children[ch-'a']==null){

                node.children[ch-'a']=new TrieNode();

            }

            node=node.children[ch-'a'];

        }

        node.isEnd=true;

        

    }

Complexity Analysis

Time complexity : O(m), where m is the key length.

In each iteration of the algorithm, we either examine or create a node in the trie till we reach the end of the key. This takes only mm operations.


Space complexity : O(m).

In the worst case newly inserted key doesn't share a prefix with the the keys already inserted in the trie. We have to add mm new nodes, which takes us O(m)O(m) space.


Search for a key in a trie

Each key is represented in the trie as a path from the root to the internal node or leaf. We start from the root with the first key character. We examine the current node for a link corresponding to the key character. There are two cases :


A link exist. We move to the next node in the path following this link, and proceed searching for the next key character.


A link does not exist. If there are no available key characters and current node is marked as isEnd we return true. Otherwise there are possible two cases in each of them we return false :


There are key characters left, but it is impossible to follow the key path in the trie, and the key is missing.

No key characters left, but current node is not marked as isEnd. Therefore the search key is only a prefix of another key in the trie.










  /** Returns if the word is in the trie. */

    public boolean search(String word) {

        TrieNode node=root;

        for(int i=0;i<word.length();i++){

            char ch=word.charAt(i);

            if(node.children[ch-'a']==null)

                return false;

            node=node.children[ch-'a'];

        }

        return node.isEnd;

        

    }

Complexity Analysis

Time complexity : O(m)

Space complexity : O(1)

Reference : https://leetcode.com/problems/implement-trie-prefix-tree/solution/

1 comment:

  1. titanium white | Titanium Art
    Titanium trekz titanium pairing Art - Titanium Art · TITanium 2020 escape titanium Art - TITATIONAL ART · TITATIONAL ART · TITATIONAL ART · TITATIONAL titanium piercings ART · titanium trim reviews TITATIONAL ART · TITATIONAL ART · TITATIONAL ART · TITATIONAL ART · TITATIONAL ART. urban titanium metallic

    ReplyDelete