16 std::shared_ptr<node> characters[26]{
nullptr};
20 std::shared_ptr<node> root;
29 inline explicit trie(std::vector<std::string> v = {})
noexcept
30 : root(std::make_shared<node>()) {
42 inline explicit trie(
const trie& t) : root(t.root), _size(t._size) {}
59 inline bool empty() {
return root ==
nullptr; }
64 inline void insert(std::string key);
71 inline size_t size() {
return _size; }
76 inline void remove(std::string key);
82 inline bool search(std::string key);
84 inline friend std::ostream& operator<<(std::ostream& out,
trie& t);
91 bool _children(std::shared_ptr<node> root) {
92 for (int64_t i = 0; i < 26; i++) {
93 if (root->characters[i]) {
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;
109 if (_children(current)) {
115 int64_t i = word[index] -
'a';
116 if (!current->characters[i]) {
119 current->characters[i] = _remove(current->characters[i], word, index + 1);
120 if (current->characters[i] || _children(current)) {