Trie Data Structure
A trie is a data structure used for efficient retrieval of data associated with keys. If key is of length n, then using trie worst case time complexity for searching the record associated with this key is O(n). Insertion of (key, record) pair also takes O(n) time in worst case.
Trie's retrieval/insertion time in worst case is better than hashTable and binary search tree both of which take worst case time of O(n) for retrieval/insertion. The trie structure though in theory has same worst case space complexity as hashTable or a binary tree, memory needed to store pointers causes it to be less space efficient during implementations.
You can see in the below picture as to how a trie data structure looks like for key, value pairs ("abc",1),("xy",2),("xyz",5),("abb",9)("xyzb",8), ("word",5).
Each node including root has 'n' number of children, where 'n' is total number of possible alphabets out of which a key would be formed. In this example, if we assume that a key would not consist of any other alphabets than characters 'a' to 'z' then value of 'n' would be 26. Each node therefore would point to 26 other nodes. Each node is basically an alphabet in the path from root node to leaf node(which stores value for that key). For example, to search the record associated with key 'abc', we start from root node then go to 0th child since first character in key 'abc' has index of 0 in alphabets. By taking 0th index, we reach node 'a', at this point we go to 1st child and reach node 'b', here we take 2nd path to child and go to node 'c'. Now to reach node 'c', in total we have visited node 'a' then node 'b' followed by node 'c', in other words key 'abc' is visited completely and hence we stop at node 'c' and return the value stored at this node.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std ;
#include<string.h>
#include<malloc.h>
#define ALPHABET 26
#define CHAR_TO_INDEX(c) ((int)c-(int)'a')
#define FREE(p) \
free(p) ; \
p=NULL ;
typedef struct TrieNode
{
struct TrieNode *children[ALPHABET] ;
bool isLeaf ;
}trie ;
trie* GetNode(void) ;
void InsertTrie(trie *,string ) ;
bool SearchTrie(trie *,string ) ;
void DeleteTrie(trie *,string ) ;
bool DeleteElementTrie(trie *,string ,int ,int ) ;
bool IsItFreeNode(trie *) ;
bool IsLeafNode(trie *) ;
int main()
{
trie *root=GetNode() ;
string str ;
int conti,choice,i,no ;
do
{
cout<<"\n 1. enter elements in the Trie DS" ;
cout<<"\n 2. search for a element in Trie DS " ;
cout<<"\n 3. Delete an element from Trie DS" ;
cout<<"\n\n pleas enter your choice" ;
cin>>choice ;
switch(choice)
{
case 1 :cout<<"\n how many elements to insert" ;
cin>>no ;
for(i=0;i<no;i++)
{
cin>>str ;
InsertTrie(root,str) ;
}
break ;
case 2 :cout<<"\n enter the element to search in trie" ;
cin>>str ;
if(SearchTrie(root,str))
cout<<"Found" ;
else
cout<<"Not Found" ;
break ;
case 3 :cout<<"\n enter the string that you need to delete from the Trie" ;
cin>>str ;
DeleteTrie(root,str) ;
break ;
default : cout<<"\n Wrong choice !! plase try again" ;
}
cout<<"\n plase enter 1 to continue " ;
cin>>conti ;
}while(conti==1) ;
return 0 ;
}
trie* GetNode(void)
{
trie *node=NULL ;
node=(trie*)malloc(sizeof(trie)) ;
node->isLeaf=false ;
for(int i=0;i<ALPHABET;i++)
node->children[i]=NULL ;
return node ;
}
void InsertTrie(trie *root,string str)
{
int level,index ;
int len=str.length() ;
trie *temp=root ;
for(level=0;level<len;level++)
{
index=CHAR_TO_INDEX(str[level]) ;
if(!temp->children[index])
temp->children[index]=GetNode() ;
temp=temp->children[index] ;
}
temp->isLeaf=true ;
}
bool SearchTrie(trie *root,string str)
{
int level,index ;
int len=str.length() ;
trie *temp=root ;
for(level=0;level<len;level++)
{
index=CHAR_TO_INDEX(str[level]) ;
if(!temp->children[index])
return false ;
temp=temp->children[index] ;
}
return (temp!=NULL&&temp->isLeaf) ;
}
void DeleteTrie(trie *root,string str)
{
int len=str.length() ;
if(len>0)
DeleteElementTrie(root,str,0,len) ;
}
bool DeleteElementTrie(trie *root,string str,int level,int len)
{
if(root)
{
if(level==len)
{
if(root->isLeaf)
{
root->isLeaf=false ;
if(IsItFreeNode(root))
return true ;
return false ;
}
}
else
{
int index=CHAR_TO_INDEX(str[level]) ;
if(DeleteElementTrie(root->children[index],str,level+1,len))
{
FREE(root->children[index]) ;
return (IsLeafNode(root)&&IsItFreeNode(root)) ;
}
}
}
return false ;
}
bool IsItFreeNode(trie *root)
{
for(int i=0;i<ALPHABET;i++)
if(root->children[i])
return false ;
return true ;
}
bool IsLeafNode(trie *root)
{
return root->isLeaf ;
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Comments
Post a Comment