Trie | (Delete) | Practice | GeeksforGeeks

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. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie.
Example 1:
Input:
N = 8
super = "the a there answer any by bye their" 
key = "the" 
 
Your Task:
Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed.
If any other string other than String A is deleted, you will get wrong answer.
 
Constraints:
1≤ T ≤200
1≤ N, |A| ≤20
+1
sachinnnnnn2 days ago
class Solution{ public:bool isempty(trie_node_t* root){   for(int i=0;i<26;i++){       if(root->children[i]!=NULL) return false;   }   return true;}trie_node_t* delkey(trie_node_t *root,char key[],int level){   int n=strlen(key);   if(root==NULL) return NULL;   if(level==n){       root->value=0;       if(isempty(root)){           delete(root);           root=NULL;       }       return root;   }   int index=key[level]-'a';   root->children[index]=delkey(root->children[index],key,level+1);   if(isempty(root) && root->value==0){       delete(root);       root=NULL;   }   return root;} void deleteKey(trie_node_t *root, char key[]){   delkey(root,key,0);}};
0
himanshujain4572 months ago
C++Solution:
class Solution{ public: void deleteKey(trie_node_t *root, char key[]) {     int len=strlen(key);   for(int i=0;i<len;i++)   {      char ch=key[i];      if(root->children[ch-'a'])      {          root->children[ch-'a']->value=0;      }      root=root->children[ch-'a'];   }   }};
 
0
himanshujain4572 months ago
Easy Java Solution:
class Solution{   public static void deleteKey(TrieNode root,char[] key)   {        for(char ch:key)    {               if(root.subNode(ch)!=null)        {            root.subNode(ch).count=0;                    }        root=root.subNode(ch);            }    root.isEnd=false;        }}
0
geminicode2 months ago
Kindly add Python compiler.
0
vatskratos232 months ago
class Solution{
  public:
  void deleteKey(trie_node_t *root, char key[])
  {
        if(root==NULL)
        {
            return;
        }
        if(strlen(key)==0)
        {
            root->value=0;
            return;
        }
        int index=key[0]-'a';
        deleteKey(root->children[index],key+1);
        if(root->children[index]!=NULL)
        {
            int flag=0;
            for(int i=0;i<26;i++)
            {
                if(root->children[index]->children[i]!=NULL)
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0)
            {
                root->children[index]=NULL;
    
            }
        }
  }
};
0
venkatarama4573 months ago
Easy C++ Solution..............
void deleteKey(trie_node_t *root, char key[])
{
	for(int i=0; key[i]!='\0'; i++)
    	root = root->children[key[i]-'a'];
	root->value = 0;
}
0
atulyadav024 months ago
void deleteKey(trie_node_t *root, char key[]) {    int cnt=0;    while(key[cnt]!='\0')      cnt++;      int k=0;    for(int i=0;i<cnt;i++){        k=key[i]-'a';       if(root->children[k]==NULL)           return ;     root=root->children[k];            }      if(root->value>0)    root->value=0;     }
0
rohitpendse1385 months ago
class Solution {
    void solve(trie_node_t *root, string word, int index) {
        if (root == NULL) return;
        if (index == word.size()) {
            root->value = 0;
            return;
        }
        return solve(root->children[INDEX(word[index])],
                     word, index + 1);
    }

public:

    void deleteKey(trie_node_t *root, char key[]) {
        if (root == NULL) return;
        string word;
        int i = 0;
        while (key[i] != '\0') {
            word += key[i];
            i++;
        }
        solve(root, word, 0);
    }
};


0
vishal patel9 months ago
vishal patel
0
18-503 harsha yadav9 months ago
18-503 harsha yadav
https://uploads.disquscdn.c...java solution
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.