题目:#
Implement a trie with insert, search, and startsWith methods.
Example:
1
2
3
4
5
6
7
8
| Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // returns true
trie.search("app"); // returns false
trie.startsWith("app"); // returns true
trie.insert("app");
trie.search("app"); // returns true
|
Note:
- You may assume that all inputs are consist of lowercase letters
a-z. - All inputs are guaranteed to be non-empty strings.
实现:#
TrieTree是一个多叉树,是解决字符串快速匹配问题的数据结构, 他用于存储一个字符串,每个TrieTree的节点代表一个字符串/字符串前缀,每个节点可以有多个孩子节点,指向不同子节点不同路径代表不同的字符串,子节点的字符串是由本子节点的字符加上从根节点到本节点路径上的字符组成。
本地实现上, 节点的子节点使用数组或哈希表存储,只处理26个字母,通过判断子节点指针是否为空判断该字符是否存在。 具体实现参考leetcode提供的伪代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
| class Trie {
private:
#define N 26
struct TrieNode {
TrieNode* children[N];
bool bEnd=false;
};
TrieNode *root;
public:
/** Initialize your data structure here. */
Trie() {
root = new TrieNode;
for (int i=0; i<N; i++){
root->children[i]= nullptr;
}
}
/** Inserts a word into the trie. */
void insert(string word) {
/*
1. Initialize: cur = root
2. for each char c in target string S:
3. if cur does not have a child c:
4. cur.children[c] = new Trie node
5. cur = cur.children[c]
6. cur is the node which represents the string S
*/
// Return a specific child node with char c: (root->children)[c - 'a']
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->bEnd = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
/*1. Initialize: cur = root
2. for each char c in target string S:
3. if cur does not have a child c:
4. search fails
5. cur = cur.children[c]
6. search successes
*/
TrieNode *cur = root;
for(char& c : word) {
if (cur->children[c - 'a'] == nullptr){
return false;
}
cur = cur->children[c - 'a'];
}
if (cur->bEnd)
return true;
else
return false;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
/*1. Initialize: cur = root
2. for each char c in target string S:
3. if cur does not have a child c:
4. search fails
5. cur = cur.children[c]
6. search successes
*/
TrieNode *cur = root;
for(char& c : prefix) {
if (cur->children[c - 'a'] == nullptr){
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);
*/
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
| class Trie {
private:
#define N 26
struct TrieNode {
unordered_map<char, TrieNode*> children;
bool bEnd=false;
};
TrieNode *root;
public:
/** Initialize your data structure here. */
Trie() {
root = new TrieNode;
for (int i=0; i<N; i++){
( root->children)[i]= nullptr;
}
}
/** Inserts a word into the trie. */
void insert(string word) {
/*
1. Initialize: cur = root
2. for each char c in target string S:
3. if cur does not have a child c:
4. cur.children[c] = new Trie node
5. cur = cur.children[c]
6. cur is the node which represents the string S
*/
// unordered_map char c: (root->children)[c]
TrieNode *cur = root;
for(char& c : word) {
if ((cur->children)[c] == nullptr){
(cur->children)[c] = new TrieNode();
}
cur = (cur->children)[c];
}
cur->bEnd = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
/*1. Initialize: cur = root
2. for each char c in target string S:
3. if cur does not have a child c:
4. search fails
5. cur = cur.children[c]
6. search successes
*/
TrieNode *cur = root;
for(char& c : word) {
if ((cur->children)[c] == nullptr){
return false;
}
cur = (cur->children)[c];
}
if (cur->bEnd)
return true;
else
return false;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
/*1. Initialize: cur = root
2. for each char c in target string S:
3. if cur does not have a child c:
4. search fails
5. cur = cur.children[c]
6. search successes
*/
TrieNode *cur = root;
for(char& c : prefix) {
if ((cur->children)[c] == nullptr){
return false;
}
cur = (cur->children)[c];
}
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);
*/
|
参考:#
https://leetcode.com/problems/implement-trie-prefix-tree/solution/