208.Implement Trie (Prefix Tree)
2015-06-30 08:48
274 查看
Implement a trie with
and
Note:
You may assume that all inputs are consist of lowercase letters
class TrieNode {
public:
// Initialize your data structure here.
char content;
bool isEnd;
int shared;
vector<TrieNode *> children;
TrieNode():content(' '),isEnd(false),shared(0) {
}
TrieNode(char ch):content(ch),isEnd(false),shared(0) {
}
TrieNode *subNode(char ch)
{
if(!children.empty())
{
for(auto child:children)
{
if(child->content == ch)
return child;
}
}
return NULL;
}
~TrieNode()
{
for(auto child:children)
{
delete child;
}
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
~Trie()
{
delete root;
}
// Inserts a word into the trie.
void insert(string word) {
if(search(word))
return;
TrieNode * cur = root;
for(auto ch:word)
{
TrieNode *child = cur->subNode(ch);
if(child)
{
cur = child;
}
else
{
TrieNode *newNode = new TrieNode(ch);
cur->children.push_back(newNode);
cur = newNode;
}
++cur->shared;
}
cur->isEnd = true;
}
// Returns if the word is in the trie.
bool search(string word) {
TrieNode * cur = root;
for(auto ch:word)
{
cur = cur->subNode(ch);
if(!cur)
return false;
}
return cur->isEnd == true;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode * cur = root;
for(auto ch:prefix)
{
cur = cur->subNode(ch);
if(!cur)
return false;
}
return true;
}
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.
Note:
You may assume that all inputs are consist of lowercase letters
a-z.
class TrieNode {
public:
// Initialize your data structure here.
char content;
bool isEnd;
int shared;
vector<TrieNode *> children;
TrieNode():content(' '),isEnd(false),shared(0) {
}
TrieNode(char ch):content(ch),isEnd(false),shared(0) {
}
TrieNode *subNode(char ch)
{
if(!children.empty())
{
for(auto child:children)
{
if(child->content == ch)
return child;
}
}
return NULL;
}
~TrieNode()
{
for(auto child:children)
{
delete child;
}
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
~Trie()
{
delete root;
}
// Inserts a word into the trie.
void insert(string word) {
if(search(word))
return;
TrieNode * cur = root;
for(auto ch:word)
{
TrieNode *child = cur->subNode(ch);
if(child)
{
cur = child;
}
else
{
TrieNode *newNode = new TrieNode(ch);
cur->children.push_back(newNode);
cur = newNode;
}
++cur->shared;
}
cur->isEnd = true;
}
// Returns if the word is in the trie.
bool search(string word) {
TrieNode * cur = root;
for(auto ch:word)
{
cur = cur->subNode(ch);
if(!cur)
return false;
}
return cur->isEnd == true;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode * cur = root;
for(auto ch:prefix)
{
cur = cur->subNode(ch);
if(!cur)
return false;
}
return true;
}
private:
TrieNode* root;
};
// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
相关文章推荐
- 解析:使用easyui的form提交表单,在IE下出现类似附件下载时提示是否保存的现象
- 强烈推荐10个纯干货的技术订阅号
- 带有checkbox的jstree封装
- JAVA中堆和栈的区别
- will_paginate
- Markdown使用记录
- 《统计学习方法》笔记(六)--决策树
- VS2010编译libtorrent
- 排序算法合集
- OR1200数据Cache介绍
- 排序算法合集 2015-06-30 08:43 155人阅读 评论(0) 收藏
- 213.House Robber II
- WindowsServer 2008 AD搭建FTP隔离用户
- 格拉西安《智慧书》中最有价值的23条法则
- 排序算法之堆排序
- 排序算法之堆排序 分类: C/C++ 数据结构与算法 2015-06-30 08:41 216人阅读 评论(1) 收藏
- 198.House Robber
- GC - (一) What Is Garbage Collection?
- 网页 js 获取DPI pxTomm
- ibatIS中的isNotNull、isEqual、isEmpty