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. 高并发编程之synchronized

    一.什么是线程? 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成.另外,线程 ...

  2. luoguP2231 [HNOI2002]跳蚤

    题目链接 bzoj1220: [HNOI2002]跳蚤 题解 根据裴蜀定理,不定方程的解为未知数的gcd,所以选取的n个数的gcd为1 那么n - 1个数保证没有公约数为m的约数,枚举质因数容斥 质因 ...

  3. Windows Phone background Audio 后台音频

    Windows Phone 后台音频的确不是什么新鲜的话题了,但发现目前在WP平台的音频播放应用多多少少会有一些瑕疵,所以在此给大家在此介绍下这个功能给有需要的朋友们. 首先介绍下我们的应用在后台播放 ...

  4. 装了wamp之后,80端口被占用解决办法

    1.如果装了IIS,那么把IIS停掉. 2.如果装了sqlserver,那么在cmd里面执行命令:services.msc,进入服务里面,把SQL Server Reporting Services ...

  5. fragment和fragmentactivity解析

    一.为什么要使用Fragment  1.当我们须要动态的多界面切换的时候,就须要将UI元素和Activity融合成一个模块.在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单个 ...

  6. java基础学习总结——抽象类

    一.抽象类介绍

  7. 用显微镜观察cpu芯片内部

    1. 先找到一块Intel公司的奔三(Pentium III)Coppermine芯片,主频800MHZ,生产于2000年.(我查了一下,网上的报价现在是15~30元人民币/块.) 下面是这块CPU的 ...

  8. C#程序集系列10,强名称程序集

    当一个程序集的名称,版本,文化,Public Key都做了设置,就可以把这个程序集叫做"强名称程序集".强名称程序集可以防止被仿冒或篡改.本篇首先创建一个强名称程序集,接着模拟篡改 ...

  9. VS增强插件 Supercharger破解教程

    Supercharger 破解教程:步骤:1.打开Supercharger的options; 2.点击Pricing & Registration 3.复制 license tt4e2HN4X ...

  10. jQuery元素属性attr设置多个键值或函数 删除属性removeAttr

    $("Element").attr(name) '取得第一个匹配的属性值,比如$("img").attr("src") $("El ...