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. blog搬家啦

    本blog大概不会更新了 新blog地址:https://zykykyk.github.io/

  2. 浅谈 PHP 中的多种加密技术及代码示例

    信息加密技术的分类 单项散列加密技术(不可逆的加密) 属于摘要算法,不是一种加密算法,作用是把任意长的输入字符串变化成固定长的输出串的一种函数 MD5 string md5 ( string $str ...

  3. 在windows安装配置Git开发环境

    开始配置Git的开发环境.首先从google  code下载最新的windows的git安装包msysgit,当时我下载的是Git-1.7.4-preview20110204.exe,然后就开始安装了 ...

  4. poj 1062 昂贵的聘礼 最短路 dijkstra

    #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...

  5. 【原】使用Spring自带的JdbcTemplate。

    使用Spring自带的JdbcTemplate,可以简化对数据库的操作,用起来十分方便.通过一下几个步骤的配置,即可以使用JdbcTemplate. (1)配置好Spring的数据源,加入mysql驱 ...

  6. 最小生成树-普利姆算法eager实现

    算法描述 在普利姆算法的lazy实现中,参考:普利姆算法的lazy实现 我们现在来考虑这样一个问题: 我们将所有的边都加入了优先队列,但事实上,我们真的需要所有的边吗? 我们再回到普利姆算法的lazy ...

  7. javascript小记-闭包理解

    这几天也在看一些javascript的知识,算是对以往的一个复习,现小记一下,方便以后查询. 相信大家在研究javascript的高级特性的时候,肯定会遇到闭包的概念,自己在各种复习资料中,也发现了不 ...

  8. configure: error: libpam required but missing

    安装pam-devel:yum install pam-devel

  9. WPF中的ImageBrush常用方式

    WPF的ImageBrush是一个比较常见也比较复杂的笔刷,它继承自图块笔刷(TileBrush).使用图块画笔绘制区域涉及以下三个组成部分:内容.基本图块和输出区域.基本输出过程如下图所示: 其中, ...

  10. ARM ® and Thumb ®-2 指令系统

    指令表关键词        Rm {, <opsh>} 寄存器移位方式,将寄存器的移位结果作为操作数而Rm值保持不变       <Operand2> 灵活的使用第二个操作数. ...