Trie

Index

  1. Definition
  2. Time-Complexity
  3. Programmatic representation
  4. Operations
    1. Insertion
    2. Search/Auto-completion

Definition

  1. Trie comes from word reTrieval.
  2. Trie is a k-way tree DS.
  3. It is optimised for retrieval where string have common prefix which are store on common nodes.

Time Complexity

  1. It has time complexity of O(N) where N is the maximum length of string.

Programmatic representation

  1. A Node is represented as class having 2 things –
    1. children representing one of 26 characters for a node.
    2. isLastNode – representing last node of string.

class TrieNode{
//representing one of 26 characters of alphabet for a node
TrieNode children;
//representing last node of string.
boolean isLastNode;
TrieNode(){
children=new TrieNode[26];
isLastNode=false;
}
}

Trie Operation and applications:

Insertion :

class TrieNode{

TrieNode children;
boolean isLastNode;

TrieNode(){
children=new TrieNode[26];
isLastNode=false;
}

}

Class Trie{
TrieNode root;
Trie(){
root=new TrieNode();
}


public static void main(){
Trie trie=new Trie;
trie.insert("parag");
trie.insert("parameter");
trie.insert("parashoot");
trie.autocomplete("para");
}
private void insert(String word){
TrieNode current=root;
}
private void autoComplete(String prefix){
TrieNode current=root;
}
}

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment