Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lecture 79 :Trie & its Implementation code} is missing #521

Open
divyanshu-vashu opened this issue Jan 16, 2024 · 1 comment
Open

lecture 79 :Trie & its Implementation code} is missing #521

divyanshu-vashu opened this issue Jan 16, 2024 · 1 comment

Comments

@divyanshu-vashu
Copy link

can anyone add code for lecture 79 :Trie & its Implementation code is missing

@aryamanbro
Copy link

can anyone add code for lecture 79 :Trie & its Implementation code is missing

class TrieNode{
public:
char data;
TrieNode* children[26];
bool isterminal;
/** Initialize your data structure here. */
TrieNode(char data) {
this->data=data;
for (int i = 0; i < 26; i++) {
children[i] = NULL;
}
isterminal=false;
}

};
class Trie {

public:
TrieNode *root;
Trie(){
root= new TrieNode('\0');
}

/** Inserts a word into the trie. */
void insert(string word) {
    insertword(root,word);
}
void insertword(TrieNode* root,string word){
    if(word.length()==0){
       root->isterminal=true;
       return;
    }
    int index=word[0]-'a';
    TrieNode* child;
    if(root->children[index]!=NULL){
        child=root->children[index];
    }
    else{
        child=new TrieNode(word[0]);
        root->children[index]=child;
    }
    insertword(child,word.substr(1));
}
/** Returns if the word is in the trie. */
bool search(string word){
   return searchword(root,word);
}
bool searchword(TrieNode* root,string word){
    if(word.length()==0){
       return root->isterminal;
    }
    int index=word[0]-'a';
    TrieNode* child;
    if(root->children[index]!=NULL){
        child=root->children[index];
    }
    else{
        return false;
    }
    return searchword(child,word.substr(1));
}
bool start(TrieNode* root, string word){
    if(word.length()==0){
       return true;
    }
    int index=word[0]-'a';
    TrieNode* child;
    if(root->children[index]!=NULL){
        child=root->children[index];
    }
    else{
        return false;
    }
   return start(child,word.substr(1));
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
  return  start(root,prefix);
}

};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants