您的位置:首页 > 其它

LeetCode 208. Implement Trie (Prefix Tree)

2018-02-22 18:07 656 查看
https://leetcode.com/problems/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
.

Trie,前缀树,字典树。Solution里提供了详细的数据结构和算法实现。由于用到指针数组,注意初始化指针,以及析构函数防止内存泄漏问题。

Implement Trie (Prefix Tree) - LeetCode
https://leetcode.com/problems/implement-trie-prefix-tree/solution/ https://leetcode.com/problems/implement-trie-prefix-tree/discuss/58842/Maybe-the-code-is-not-too-much-by-using-%22next26%22-C++
字典树_百度百科
https://baike.baidu.com/item/字典树/9825209?fromtitle=Trie树&fromid=517527
又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。

Trie - 维基百科,自由的百科全书
https://zh.wikipedia.org/wiki/Trie
计算机科学中,trie,又称前缀树字典树,是一种有序,用于保存关联数组,其中的键通常是字符串。与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定。一个节点的所有子孙都有相同的前缀,也就是这个节点对应的字符串,而根节点对应空字符串。一般情况下,不是所有的节点都有对应的值,只有叶子节点和部分内部节点所对应的键才有相关的值。

//
//  main.cpp
//  LeetCode
//
//  Created by Hao on 2017/3/16.
//  Copyright © 2017年 Hao. All rights reserved.
//

#include <iostream>
using namespace std;

class TrieNode {
public:
TrieNode() : isEnd(false) {
memset(links, 0, sizeof(links)); // remember for inialization
}

~TrieNode() {                        // remember to avoid memory leak
for (auto & iter : links)
delete iter;
}

bool containsKey(char ch) {
return links[ch - 'a'] != nullptr;
}

TrieNode* get(char ch) {
return links[ch - 'a'];
}

void put(char ch, TrieNode* node) {
links[ch - 'a'] = node;
}

void setEnd() {
isEnd = true;
}

bool getEnd() {
return isEnd;
}

private:
TrieNode*   links[26];
bool        isEnd;
};

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

~Trie() {                           // remember to avoid memory leak
delete root;
}

/** Inserts a word into the trie. */
void insert(string word) {
TrieNode* node = root;

for (int i = 0; i < word.size(); i ++) {
char currentChar = word.at(i);

if (! node->containsKey(currentChar)) {
node->put(currentChar, new TrieNode());
}

node = node->get(currentChar);
}

node->setEnd();
}

/** Returns if the word is in the trie. */
bool search(string word) {
TrieNode* node = searchPrefix(word);

return node != nullptr && node->getEnd();
}

/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
TrieNode* node = searchPrefix(prefix);

return node != nullptr;
}

private:
TrieNode* searchPrefix(string word) {
TrieNode* node = root;

for (int i = 0; i < word.size(); i ++) {
char currentChar = word.at(i);

if (node->containsKey(currentChar)) {
node = node->get(currentChar);
} else {
return nullptr;
}
}

return node;
}

TrieNode*   root;
};

/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* bool param_2 = obj.search(word);
* bool param_3 = obj.startsWith(prefix);
*/

int main(int argc, char* argv[])
{
Trie obj;

obj.insert("word");
bool param_2 = obj.search("word");
bool param_3 = obj.startsWith("wo");
bool param_4 = obj.startsWith("prefix");

/*
1
1
0
*/
cout << param_2 << endl;
cout << param_3 << endl;
cout << param_4 << endl;

return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: