Git Product home page Git Product logo

Comments (1)

aryamanbro avatar aryamanbro commented on June 8, 2024

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);
}

};

from codehelp-dsa-busted-series.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.