leetcode-trie树实现

题目大意

  https://leetcode.com/problems/implement-trie-prefix-tree/

  trie树的实现

题目分析

  trie树,实现插入,搜索,复杂度都是o(len),len为单词的长度

代码

  • Java版本
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
class TrieNode {
public TrieNode[] childNodes;
public int freq;
public char nodeChar;
// Initialize your data structure here.
public TrieNode() {
childNodes = new TrieNode[26];
freq = 0;
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
if(word.length() == 0) {
return;
}
TrieNode cur = root;
for(int i = 0; i < word.length(); i++) {
int k = word.charAt(i) - 'a';
if(cur.childNodes[k] == null) {
cur.childNodes[k] = new TrieNode();
cur.childNodes[k].nodeChar = word.charAt(i);
}
cur = cur.childNodes[k];
if(i == word.length() - 1) {
cur.freq ++;
}
}
}
// Returns if the word is in the trie.
//root->'a'->'b'->'c'
public boolean search(String word) {
if(word.length() == 0) {
return false;
}
TrieNode cur = root;
for(int i = 0; i < word.length(); i++) {
int k = word.charAt(i) - 'a';
if(cur.childNodes[k] == null) {
return false;
}
cur = cur.childNodes[k];
}
if(cur.freq > 0) {
return true;
} else {
return false;
}
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
if(prefix.length() == 0) {
return false;
}
TrieNode cur = root;
for(int i = 0; i < prefix.length(); i++) {
int k = prefix.charAt(i) - 'a';
if(cur.childNodes[k] == null) {
return false;
}
cur = cur.childNodes[k];
}
return true;
}
}

- C++版本

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
class TrieNode {
public:
// Initialize your data structure here.
char nodeChar;
int freq;
TrieNode* childNodes[26];
TrieNode() {
for(int i = 0; i < 26; i++) {
childNodes[i] = NULL;
}
freq = 0;
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
int s = word.length();
if(s == 0) {
return;
}
TrieNode* cur = root;
for(int i = 0; i < s; i++) {
int k = word[i] - 'a';
if((cur->childNodes)[k] == NULL) {
(cur->childNodes)[k] = new TrieNode();
(cur->childNodes)[k]->nodeChar = word[i];
}
cur = (cur->childNodes)[k];
if(i == s - 1) {
cur->freq ++;
}
}
}
// Returns if the word is in the trie.
bool search(string word) {
int s = word.length();
if(s == 0) {
return false;
}
TrieNode* cur = root;
for(int i = 0; i < s; i++) {
int k = word[i] - 'a';
if((cur->childNodes)[k] == NULL) {
return false;
}
cur = (cur->childNodes)[k];
}
if(cur->freq > 0) {
return true;
} else {
return false;
}
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
int s = prefix.length();
if(s == 0) {
return false;
}
TrieNode* cur = root;
for(int i = 0; i < s; i++) {
int k = prefix[i] - 'a';
if((cur->childNodes)[k] == NULL) {
return false;
}
cur = (cur->childNodes)[k];
}
return true;
}
private:
TrieNode* root;
};