字典树(Trie)



Trie最大的问题:空间!所以可以使用一下解决方案。


Code
#pragma once
#include
LeetCode
208. 实现 Trie (前缀树)
class Trie {
public:
/** Initialize your data structure here. */
Trie() {
root = new Node();
}
/** Inserts a word into the trie. */
void insert(string word) {
Node *cur = root;
for (int i = 0; i < word.size(); ++i) {
char c = word.at(i);
if (cur->next.find(c) == cur->next.end()) {
cur->next.insert(std::pair(c, Node()));
}
cur = &cur->next.find(c)->second;
}
if (!cur->isWord) {
cur->isWord = true;
}
}
/** Returns if the word is in the trie. */
bool search(string word) {
Node *cur = root;
for (int i = 0; i < word.size(); ++i) {
char c = word.at(i);
if (cur->next.find(c) == cur->next.end()) {
return false;
}
cur = &cur->next.find(c)->second;
}
return cur->isWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
Node *cur = root;
for (int i = 0; i < prefix.size(); ++i) {
char c = prefix.at(i);
if (cur->next.find(c) == cur->next.end()) {
return false;
}
cur = &cur->next.find(c)->second;
}
return true;
}
private:
class Node {
public:
bool isWord;
std::map next;
Node() {
isWord = false;
}
};
Node *root;
};
211. 添加与搜索单词 - 数据结构设计
class WordDictionary {
public:
/** Initialize your data structure here. */
WordDictionary() {
root = new Node();
}
/** Adds a word into the data structure. */
void addWord(string word) {
Node *cur = root;
for (int i = 0; i < word.size(); ++i) {
char c = word.at(i);
if (cur->next.find(c) == cur->next.end()) {
cur->next.insert(std::pair(c, Node()));
}
cur = &cur->next.find(c)->second;
}
cur->isWord = true;
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
bool search(string word) {
return match(root, word, 0);
}
private:
class Node {
public:
bool isWord;
std::map next;
Node() {
isWord = false;
}
};
Node *root;
bool match(Node *node, string word, int index) {
if (index == word.size()) {
return node->isWord;
}
char c = word.at(index);
if (c != '.') {
if (node->next.find(c) == node->next.end()) {
return false;
}
return match(&node->next.find(c)->second, word, index + 1);
} else {
for(std::map::iterator iterator= node->next.begin();iterator != node->next.end();iterator++) {
if(match(&node->next.find(iterator->first)->second, word, index + 1)) {
return true;
}
}
return false;
}
}
};
677. 键值映射
class MapSum {
public:
/** Initialize your data structure here. */
MapSum() {
root = new Node();
}
void insert(string key, int val) {
Node *cur = root;
for (int i = 0; i < key.size(); ++i) {
char c = key.at(i);
if (cur->next.find(c) == cur->next.end()) {
cur->next.insert(std::pair(c, Node()));
}
cur = &cur->next.find(c)->second;
}
cur->value = val;
}
int sum(string prefix) {
Node *cur = root;
for (int i = 0; i < prefix.size(); i++) {
char c = prefix.at(i);
if (cur->next.find(c) == cur->next.end()) {
return 0;
}
cur = &cur->next.find(c)->second;
}
return sum(cur);
}
private:
class Node {
public:
int value;
std::map next;
Node() {
value = 0;
}
};
Node *root;
int sum(Node *node) {
int res = node->value;
for(std::map::iterator iterator= node->next.begin();iterator != node->next.end();iterator++){
res += sum(&node->next.find(iterator->first)->second);
}
return res;
}
};