Advertisement
bojandam1

Trie

Nov 15th, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct TrieNode {
  6.     char val;
  7.     bool word_end;
  8.     TrieNode* kids[26] = {0};
  9.     TrieNode(char Val ): val(Val),word_end(false){
  10.  
  11.     }
  12.  
  13. };
  14.  
  15. class Trie {
  16.     TrieNode* root;
  17.     void add(string word) {
  18.         TrieNode* cur=root;
  19.         for (int j=0; j<word.length();j++) {
  20.             int i=word[j]-'a';
  21.             if (cur->kids[i]==nullptr) {
  22.  
  23.                 cur->kids[i] = new TrieNode(word[j]);
  24.             }
  25.             cur=cur->kids[i];
  26.             if(j+1==word.length())
  27.                 cur->word_end=true;
  28.         }
  29.     }
  30.  
  31. };
  32.  
  33.  
  34.  
  35.  
  36. int main() {
  37.  
  38.     return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement