题目:208. 实现 Trie (前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。
示例:
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // 返回 true
trie.search("app"); // 返回 false
trie.startsWith("app"); // 返回 true
trie.insert("app");
trie.search("app"); // 返回 true
说明:
你可以假设所有的输入都是由小写字母 a-z 构成的。
保证所有输入均为非空字符串。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码(建立单独的前缀树节点)
# define N 26
class TrieNode {
public:
TrieNode* children[N];
bool flag;
TrieNode() {
flag = false;
memset(children, 0, sizeof(children));
}
};
class Trie {
TrieNode *root;
public:
/** Initialize your data structure here. */
Trie() {
root = new TrieNode();
}
/** Inserts a word into the trie. */
void insert(string word) {
TrieNode *cur = root;
for (char c: word) {
if (cur->children[c - 'a'] == nullptr) {
cur->children[c - 'a'] = new TrieNode();
}
cur = cur->children[c - 'a'];
}
cur->flag = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
TrieNode *cur = root;
for (auto c: word) {
if (!cur->children[c - 'a']) {
return false;
}
cur = cur->children[c - 'a'];
}
return cur->flag;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
TrieNode *cur = root;
for (auto c: prefix) {
if (!cur->children[c - 'a']) {
return false;
}
cur = cur->children[c - 'a'];
}
return true;
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/
代码(利用hashmap实现子节点存储)
class Trie {
unordered_map<char, Trie*> children;
bool flag;
Trie* searchPrefixNode(string & word) {
Trie* node = this;
for(auto& chr: word) {
if(node->children.count(chr) == 0) {
return nullptr;
}
node = node->children[chr];
}
return node;
}
public:
/** Initialize your data structure here. */
Trie() {
flag = false;
}
/** Inserts a word into the trie. */
void insert(string word) {
Trie* node = this;
for(auto& chr: word) {
if(node->children.count(chr) == 0) {
node->children[chr] = new Trie();
}
node = node->children[chr];
}
node->flag = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
Trie* node = this->searchPrefixNode(word);
return node == nullptr ? false: node->flag;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
Trie* node = this->searchPrefixNode(prefix);
return node == nullptr ? false: true;
}
};
代码(利用数组实现子节点的存储)
class Trie {
vector<Trie*> children;
bool flag;
public:
/** Initialize your data structure here. */
Trie() {
children = vector<Trie*>(26, nullptr);
flag = false;
}
/** Inserts a word into the trie. */
void insert(string word) {
Trie* node = this;
for(auto& chr: word) {
int pos = chr - 'a';
if(node->children[pos] == nullptr) {
node->children[pos] = new Trie();
}
node = node->children[pos];
}
node->flag = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
Trie* node = this->prefixNode(word);
return node == nullptr ? false: node->flag;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
Trie* node = this->prefixNode(prefix);
return node == nullptr ? false: true;
}
Trie* prefixNode(string & word) {
Trie* node = this;
for(auto& chr: word) {
int pos = chr - 'a';
if(node->children[pos] == nullptr) {
return nullptr;
}
node = node->children[pos];
}
return node;
}
};