Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Trie {
- struct Node {
- bool is_end = false;
- map<char, Node *> next;
- };
- Node* root = nullptr;
- public:
- Trie();
- void insert(string s);
- void erase(string s);
- bool check(string s);
- };
- Trie::Trie() {
- root = new Node();
- }
- void Trie::insert(std::string s) {
- Node* cur = root;
- for (auto i : s) {
- if (!cur->next[i]) {
- cur->next[i] = new Node();
- }
- cur = cur->next[i];
- }
- cur->is_end = true;
- }
- void Trie::erase(string s) {
- Node* cur = root;
- for (auto i : s) {
- if (!cur->next[i]) {
- return;
- }
- cur = cur->next[i];
- }
- cur->is_end = false;
- }
- bool Trie::check(string s) {
- Node* cur = root;
- for (auto i : s) {
- if (!cur->next[i]) {
- return false;
- }
- cur = cur->next[i];
- }
- return cur->is_end;
- }
Advertisement
Add Comment
Please, Sign In to add comment