[Algorithm] Trie data structure
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的更多相关文章
- Summary: Trie Data Structure
Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- (Data structure)Implement Trie && Add and Search Word
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
- leetcode 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 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 ...
- [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 ...
- [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. ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
随机推荐
- Linux下rz,sz与ssh的配合使用,实现文件传输
一般来说,linux服务器大多是通过ssh客户端来进行远程的登陆和管理的,使用ssh登陆linux主机以后,如何能够快速的和本地机器进行文件的交互呢,也就是上传和下载文件到服务器和本地: 与ss ...
- opencv+vs2012环境搭建教程
1. 安装OpenCV和VS. 本人电脑安装的是opencv2.4.10和vs2012 2.配置环境变量 以下以win8 64位系统为例: 计算机->属性->高级系统设置->环境变量 ...
- 关于InnoDB的一些认识
一.聚簇索引 innoDB将表中数据按主键顺序构造成一颗B+树,叶子节点存放着整张表的行记录数据(索引组织表,即叶子节点就是数据页).因为无法把数据行存在二个不同的地方,因此每张表只能有一个聚集索引( ...
- 理解linux下源码、yum和rpm安装方法的特点
1.yum可看作在线安装,只需yum install 软件名,系统就自动根据yum源配置文件中的镜像位置去下载安装包,并可以自动分析所需的软件依赖关系,自动安装所需的依赖软件包.简单方便,不易出错,不 ...
- How far away ?(DFS)
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- MultCloud – 支持数据互传的网盘管理
MultCloud https://www.multcloud.com/ 是一款在线服务,可以在一个地方管理众多网盘,支持国产百度盘, 最具有特色的地方是你可以直接在 MultCloud 里操作将 D ...
- Maven系列--setting.xml 配置详解
文件存放位置 全局配置: ${M2_HOME}/conf/settings.xml 用户配置: ${user.home}/.m2/settings.xml note:用户配置优先于全局配置.${use ...
- mysql 审计插件编写
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1864367&page=1#pid13527550 http://blog ...
- java 盒子模型
http://www.cnblogs.com/xiaohuochai/tag/javascript%E6%80%BB%E7%BB%93/
- C# 中的动态创建技术
[转载]原文出处 http://blog.csdn.net/baiyun789/article/details/6156694 第一部分 WinForm控件在窗体中动态居中创建.删除控件及对其赋值 ...