> For the complete documentation index, see [llms.txt](https://liuzhenglaichn.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuzhenglaichn.gitbook.io/algorithm/data-structure/trie.md).

# Trie

as known as prefix tree.

### Implementation

```cpp
struct TrieNode {
    TrieNode *next[26] = {};
    int count = 0;
};

class Trie {
private:
    TrieNode root;
    TrieNode* searchToNode(string &prefix) {
        auto node = &root;
        for (char c : prefix) {
            if (!node->next[c - 'a']) return NULL;
            node = node->next[c - 'a'];
        }
        return node;
    }
public:
    void insert(string word) {
        auto node = &root;
        for (char c : word) {
            if (!node->next[c - 'a']) node->next[c - 'a'] = new TrieNode();
            node = node->next[c - 'a'];
        }
        node->count++;
    }

    bool search(string word) {
        auto node = searchToNode(word);
        return node && node->count;
    }

    bool startsWith(string prefix) {
        return searchToNode(prefix);
    }
};
```

## Problems

* [208. Implement Trie (Prefix Tree) (Medium)](https://leetcode.com/problems/implement-trie-prefix-tree/) **Direct Implementation**
* [212. Word Search II (Hard)](https://leetcode.com/problems/word-search-ii/)
* [745. Prefix and Suffix Search (Hard)](https://leetcode.com/problems/prefix-and-suffix-search/)
* [839. Similar String Groups (Hard)](https://leetcode.com/problems/similar-string-groups/)
* [1032. Stream of Characters (Hard)](https://leetcode.com/problems/stream-of-characters/)
