AlgoPlus v0.1.0
Loading...
Searching...
No Matches
trie.h
1#ifndef TRIE_H
2#define TRIE_H
3
4#ifdef __cplusplus
5#include <iostream>
6#include <memory>
7#include <vector>
8#endif
9
13class trie {
14 private:
15 struct node {
16 std::shared_ptr<node> characters[26]{nullptr};
17 bool end_word{false};
18 };
19
20 std::shared_ptr<node> root;
21 size_t _size{};
22
23 public:
29 inline explicit trie(std::vector<std::string> v = {}) noexcept
30 : root(std::make_shared<node>()) {
31 if (!v.empty()) {
32 for (auto& x : v) {
33 this->insert(x);
34 }
35 }
36 }
37
42 inline explicit trie(const trie& t) : root(t.root), _size(t._size) {}
43
49 inline trie& operator=(const trie& t) {
50 root = t.root;
51 _size = t._size;
52 return *this;
53 }
54
59 inline bool empty() { return root == nullptr; }
64 inline void insert(std::string key);
65
71 inline size_t size() { return _size; }
76 inline void remove(std::string key);
82 inline bool search(std::string key);
83
84 inline friend std::ostream& operator<<(std::ostream& out, trie& t);
85
86 private:
91 bool _children(std::shared_ptr<node> root) {
92 for (int64_t i = 0; i < 26; i++) {
93 if (root->characters[i]) {
94 return true;
95 }
96 }
97 return false;
98 }
103 std::shared_ptr<node> _remove(std::shared_ptr<node> current, std::string word, int64_t index) {
104 if (word.size() == index) {
105 if (current->end_word) {
106 current->end_word = false;
107 _size--;
108 }
109 if (_children(current)) {
110 return current;
111 }
112 return nullptr;
113 }
114
115 int64_t i = word[index] - 'a';
116 if (!current->characters[i]) {
117 return nullptr;
118 }
119 current->characters[i] = _remove(current->characters[i], word, index + 1);
120 if (current->characters[i] || _children(current)) {
121 return current;
122 }
123 return nullptr;
124 }
125};
126
127void trie::insert(std::string key) {
128 std::shared_ptr<node> current = root;
129 for (auto& c : key) {
130 int64_t index = c - 'a';
131 if (!current->characters[index]) {
132 current->characters[index] = std::make_shared<node>();
133 }
134 current = current->characters[index];
135 }
136 current->end_word = true;
137 _size++;
138}
139
140void trie::remove(std::string key) {
141 root = _remove(root, key, 0);
142}
143
144bool trie::search(std::string key) {
145 std::shared_ptr<node> current = root;
146 for (auto& c : key) {
147 int64_t index = c - 'a';
148 if (!current->characters[index]) {
149 return false;
150 }
151 current = current->characters[index];
152 }
153 return current->end_word;
154}
155
156#endif
trie class
Definition trie.h:13
size_t size()
size function
Definition trie.h:71
bool search(std::string key)
search function.
Definition trie.h:144
trie & operator=(const trie &t)
operator = for trie class
Definition trie.h:49
trie(const trie &t)
Copy constructor for trie class.
Definition trie.h:42
void remove(std::string key)
remove function.
Definition trie.h:140
trie(std::vector< std::string > v={}) noexcept
Construct a new trie object.
Definition trie.h:29
bool empty()
empty function.
Definition trie.h:59
void insert(std::string key)
insert function.
Definition trie.h:127