# 208. Implement Trie (Prefix Tree)

Description：
Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
All inputs are guaranteed to be non-empty strings.
Difficulty：Medium
Example:

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


#### 方法1：固定分配空间

c是第一个要插入的字符,'c'-'a'=2，trie[0][2]=1;
a是第二个要插入的字符,'a'-'a'=0，trie[1][0]=2;
c是第三个要插入的字符,'b'-'a'=1，trie[2][2]=3;

class Trie {
public:
/** Initialize your data structure here. */
const int maxn = 100000;
int trie[100000][26];
bool word_end[100000];
int tot = 0;

/** Inserts a word into the trie. */
void insert(string word) {
int root = 0;
for(int i = 0; i < word.size(); i++){
int id = word[i] - 'a';
if(!trie[root][id])
trie[root][id] = ++tot;
root = trie[root][id];
}
word_end[root] = true;
}

/** Returns if the word is in the trie. */
bool search(string word) {
int root = 0;
for(int i = 0; i < word.size(); i++){
int id = word[i] - 'a';
if(!trie[root][id])
return false;
root = trie[root][id];
}
if(word_end[root] == true)
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 root = 0;
for(int i = 0; i < prefix.size(); i++){
int id = prefix[i] - 'a';
if(!trie[root][id])
return false;
root = trie[root][id];
}
return true;
}
};


#### 方法2：动态分配

class TrieNode{
public:
TrieNode * branches[26];
bool is_end;
TrieNode(bool end=false){
memset(branches, 0, sizeof(branches));
is_end = end;
}
};

class Trie {
public:
/** Initialize your data structure here. */
TrieNode* root;
Trie(){
root = new TrieNode();
}
~Trie(){
destory(root);
}

void destory(TrieNode* node){
for(int i = 0; i < 26; i++){
if(node->branches[i])
destory(node->branches[i]);
}
delete node;
}

/** Inserts a word into the trie. */
void insert(string word) {
TrieNode* node = root;
int i;
for(i = 0; i < word.size(); i++){
int id = word[i] - 'a';
if(!node->branches[id])
break;
else{
node = node->branches[id];
node->is_end = (i == word.size()-1) ? true : node->is_end;
}
}
for(; i < word.size(); i++){
int id = word[i] - 'a';
node->branches[id] = new TrieNode((i == word.size()-1) ? true : node->is_end);
node  = node->branches[id];
}
}

/** Returns if the word is in the trie. */
bool search(string word) {
TrieNode* node = root;
for(int i = 0; i < word.size(); i++){
int id = word[i] - 'a';
if(!node->branches[id])
return false;
node  = node->branches[id];
}
if(node->is_end == true)
return true;
else
return false;
}

/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
TrieNode* node = root;
for(int i = 0; i < prefix.size(); i++){
int id = prefix[i] - 'a';
if(!node->branches[id])
return false;
node  = node->branches[id];
}
return true;
}
};


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

Freeman_zxp

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
05-09 4746

07-22 330
08-14 1019