For example we have an array of words:

[car, done, try, cat, trie, do]

What is the best data structure to store the data and easy for search?

We can use Trie data structure, it is a tree, but not a binary tree. The reuslts of constructing a tree by using the example array data.

True of False means whether this letter is the last of the word.

We can code it by Javascript:

  • We are using Map data structure
  • If there is no existing node for giving letter, we create a new Node.
  • If there is a existing node, then we continue to next letter
function Node() {
return {
keys: new Map(),
isWord: false
};
} function BuildTrie() {
const root = new Node();
return {
root,
add(word, node = this.root) {
if (word.length === 0) {
node.isWord = true;
return;
}
const [head, ...rest] = word;
if (!node.keys.has(head)) {
node.keys.set(head, new Node());
} return this.add(rest, node.keys.get(head));
},
hasWord(word, node = this.root) {
if (!word) {
return false;
}
while (word.length > 1) {
if (!node.keys.has(word[0])) {
return false;
} else {
node = node.keys.get(word[0]);
word = word.substr(1);
}
}
return node.keys.has(word) && node.keys.get(word).isWord;
},
print() {
let words = [];
function search(node = this.root, string = "") {
if (node.keys.size != 0) {
for (let letter of node.keys.keys()) {
search(node.keys.get(letter), string.concat(letter));
}
if (node.isWord) {
words.push(string);
}
} else {
string.length > 0 ? words.push(string) : undefined;
}
} search(this.root, "");
return words.length > 0 ? words : null;
}
};
} const t = new BuildTrie(); t.add("cat");
t.add("cal"); console.log(t.hasWord("cat")); // true
console.log(t.hasWord("catt")); // false

  

[Algorithm] Trie data structure的更多相关文章

  1. Summary: Trie Data Structure

    Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...

  2. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  3. (Data structure)Implement Trie && Add and Search Word

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

  4. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

  5. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  6. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

  7. [leetcode trie]211. Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  8. [Algorithm] JavaScript Graph Data Structure

    A graph is a data structure comprised of a set of nodes, also known as vertices, and a set of edges. ...

  9. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

随机推荐

  1. POJ1151 Atlantis 水题 计算几何

    http://poj.org/problem?id=1151 想学一下扫描线线段树,结果写了道水题. #include<iostream> #include<cstdio> # ...

  2. 谁是最快的Go Web框架

    根据Julien Schmidt测试框架中测试到的go web框架,在加上lion,fasthttp,一共测试了下面的web框架. default http macaron go-json-rest ...

  3. bzoj 3252: 攻略 -- 长链剖分+贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MB Description 题目简述:树版[k取方格数]   众所周知,桂木桂马是攻略之神,开启攻略之神 ...

  4. wikioi 1017 乘积最大

    dp[i][j]=max(dp[i][j],dp[t][k-1]*mapn[t+1][i]); dp[i][j]代表从0-i之间有j个乘号,mapn[i][j]表示第i位到第j位的数究竟是多少 #in ...

  5. 使用TensorFlow高级别的API进行编程

    这里涉及到的高级别API主要是使用Estimator类来编写机器学习的程序,此外你还需要用到一些数据导入的知识. 为什么使用Estimator Estimator类是定义在tf.estimator.E ...

  6. C# 高级编程9 第30章MEF C#可扩展编程之MEF第一章

    MEF(Managed Extensibility Framework)是一个用于创建可扩展的轻型应用程序的库 利用该库轻松地封装代码,避免生成脆弱的硬依赖项. 通过 MEF,不仅可以在应用程序内重用 ...

  7. MySQL: 查看一次SQL的执行时间都花在哪些环节上

    select @@profiling -- 看看当前的session的profiling打开没有 set profiling = 1 -- 如果没打开,打开一下 -- 执行一些sql select c ...

  8. Git_撤销修改

    自然,你是不会犯错的.不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行: $ cat readme.txt Git is a distributed version co ...

  9. mysql数据库表迁移

    @ 把老数据库中的某个表倒出成sql文件 $mysqldump -uroot -p123456 my_db > my_db.sql (输入密码) @ 在新环境中导入 $sudo apt-get ...

  10. offset大家族(一)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...