leecode_208 Implement Trie (Prefix Tree)
2016-05-28 15:03
337 查看
Implement a trie with
and
class TrieNode {
public:
// Initialize your data structure here.
char c;
unordered_map<char, TrieNode*> sub;
TrieNode() {
c='0';
}
TrieNode(char parent) {
c=parent;
}
void add_sub(char c) {
TrieNode* node=new TrieNode(c);
sub[c]=node;
}
bool has_sub(char c)
{
if (sub.find(c)!=sub.end())
return true;
else
return false;
}
TrieNode* get_sub(char c){
return sub[c];
}
bool excited(){
if (sub.find('0')!=sub.end())
return true;
else
return false;
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
TrieNode* head=root;
TrieNode* node;
for (int i=0; i<word.size();i++){
if (!head->has_sub(word[i]))
head->add_sub(word[i]);
node=head->get_sub(word[i]);
head=node;
}
head->add_sub('0');
}
// Returns if the word is in the trie.
bool search(string word) {
TrieNode* head=root;
TrieNode* node;
for (int i=0;i<word.size();i++){
if (!head->has_sub(word[i]))
return false;
node=head->get_sub(word[i]);
head=node;
//cout<<"find "<<word[i]<<endl;
}
return (*node).excited();
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode* head=root;
TrieNode* node;
for (int i=0;i<prefix.size();i++){
if (!head->has_sub(prefix[i]))
return false;
node=head->get_sub(prefix[i]);
head=node;
}
for (int i=0;i<26;i++){
if(head->has_sub('a'+i))
return true;
}
return head->excited();
}
private:
TrieNode* root;
};
// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
insert,
search,
and
startsWithmethods.
class TrieNode {
public:
// Initialize your data structure here.
char c;
unordered_map<char, TrieNode*> sub;
TrieNode() {
c='0';
}
TrieNode(char parent) {
c=parent;
}
void add_sub(char c) {
TrieNode* node=new TrieNode(c);
sub[c]=node;
}
bool has_sub(char c)
{
if (sub.find(c)!=sub.end())
return true;
else
return false;
}
TrieNode* get_sub(char c){
return sub[c];
}
bool excited(){
if (sub.find('0')!=sub.end())
return true;
else
return false;
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
TrieNode* head=root;
TrieNode* node;
for (int i=0; i<word.size();i++){
if (!head->has_sub(word[i]))
head->add_sub(word[i]);
node=head->get_sub(word[i]);
head=node;
}
head->add_sub('0');
}
// Returns if the word is in the trie.
bool search(string word) {
TrieNode* head=root;
TrieNode* node;
for (int i=0;i<word.size();i++){
if (!head->has_sub(word[i]))
return false;
node=head->get_sub(word[i]);
head=node;
//cout<<"find "<<word[i]<<endl;
}
return (*node).excited();
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode* head=root;
TrieNode* node;
for (int i=0;i<prefix.size();i++){
if (!head->has_sub(prefix[i]))
return false;
node=head->get_sub(prefix[i]);
head=node;
}
for (int i=0;i<26;i++){
if(head->has_sub('a'+i))
return true;
}
return head->excited();
}
private:
TrieNode* root;
};
// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
相关文章推荐
- ThinkPHP+AJAX
- MySQL Replication 主从配置
- Spring-事务管理
- PHP中的错误控制运算符
- Hadoop学习笔记
- [转载].NET MVC4 Razor 视图预编译
- 求教Sublime Text2 SublimeLinter插件安装问题
- spring boot + redis 实现session共享
- spring boot + redis 实现session共享
- mysql常用配置详解
- 遍历方法比较
- css中自适应的问题
- cat, more, less命令
- 一类有依赖的树形背包dp方法
- 显示操控方法和装置、用户终端及显示操控系统
- 2.2存储器
- git学习之三:分支管理
- myeclipse集成egit图文教程
- c++实验6
- Directx11学习笔记【十四】 使用最新的Effect框架和SDK