Trie | (Delete)
PROBLEM :
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted so, Your task is to complete the function deleteKey to delete the given string A.The String A if exists in the larger String must be deleted from Trie. And if the string is deleted successfully than 1 will be printed.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of three lines.
First line of each test case consist of a integer N, denoting the number of element in a Trie to be stored.
Second line of each test case consists of N space separated strings denoting the elements to be stored in the trie.
Third line of each test case consists of a String A to be searched in the stored elements.
Output:
Print the respective output in the respective line.
Constraints:
1<=T<=20
1<=N<=20
Example:
Input:
1
8
the a there answer any by bye their
the
Output:
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*Complete the function below
Node is as follows:
struct trie_node
{
int value; // non zero if leaf
trie_node_t *children[ALPHABET_SIZE];
};*/
#define CHAR_TO_INDEX(c) ((int)c-(int)'a')
bool IsItFree(trie_node_t *root)
{
for(int i=0;i<ALPHABET_SIZE;i++)
if(root->children[i])
return false ;
return true ;
}
bool IsLeafNode(trie_node_t *root)
{
return root->value ;
}
bool DeleteKeyTrie(trie_node_t * root,char key[],int level,int len)
{
if(root)
{
if(level==len)
{
if(root->value)
{
root->value=0 ;
if(IsItFree(root))
return true ;
return false ;
}
}
else
{
int index=CHAR_TO_INDEX(key[level]) ;
if(DeleteKeyTrie(root->children[index],key,level+1,len))
{
FREE(root->children[index]) ;
return (IsLeafNode(root)&&IsItFree(root)) ;
}
}
}
return false ;
}
void deleteKey(trie_t *pTrie, char key[])
{
int len=strlen(key) ;
if(len>0)
DeleteKeyTrie(pTrie->root,key,0,len) ;
}
--------------------------------------------------------------------------------
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted so, Your task is to complete the function deleteKey to delete the given string A.The String A if exists in the larger String must be deleted from Trie. And if the string is deleted successfully than 1 will be printed.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of three lines.
First line of each test case consist of a integer N, denoting the number of element in a Trie to be stored.
Second line of each test case consists of N space separated strings denoting the elements to be stored in the trie.
Third line of each test case consists of a String A to be searched in the stored elements.
Output:
Print the respective output in the respective line.
Constraints:
1<=T<=20
1<=N<=20
Example:
Input:
1
8
the a there answer any by bye their
the
Output:
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*Complete the function below
Node is as follows:
struct trie_node
{
int value; // non zero if leaf
trie_node_t *children[ALPHABET_SIZE];
};*/
#define CHAR_TO_INDEX(c) ((int)c-(int)'a')
bool IsItFree(trie_node_t *root)
{
for(int i=0;i<ALPHABET_SIZE;i++)
if(root->children[i])
return false ;
return true ;
}
bool IsLeafNode(trie_node_t *root)
{
return root->value ;
}
bool DeleteKeyTrie(trie_node_t * root,char key[],int level,int len)
{
if(root)
{
if(level==len)
{
if(root->value)
{
root->value=0 ;
if(IsItFree(root))
return true ;
return false ;
}
}
else
{
int index=CHAR_TO_INDEX(key[level]) ;
if(DeleteKeyTrie(root->children[index],key,level+1,len))
{
FREE(root->children[index]) ;
return (IsLeafNode(root)&&IsItFree(root)) ;
}
}
}
return false ;
}
void deleteKey(trie_t *pTrie, char key[])
{
int len=strlen(key) ;
if(len>0)
DeleteKeyTrie(pTrie->root,key,0,len) ;
}
--------------------------------------------------------------------------------
Comments
Post a Comment