[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 ...
随机推荐
- CentOS系统php5.6安装ImageMagick处理webp格式图片
1.先安装webp yum install libwebp 2.编译安装ImageMagick 之前有过yum安装的先卸载 yum remove ImageMagick 我使用的是老版本ImageMa ...
- lamp 5.6.36 bug记录
后来发现另一个问题,php文字水印中文是乱码. 用yum安装lamp环境详见:https://blog.csdn.net/u010071211/article/details/80370201 在ce ...
- 4040 EZ系列之奖金
4040 EZ系列之奖金 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题目描述 Description 由于无敌的WRN在2015年世界英俊帅气男总决选中 ...
- [POI2015]Łasuchy
[POI2015]Łasuchy 题目大意: 圆桌上摆放着\(n(n\le10^6)\)份食物,围成一圈,第\(i\)份食物所含热量为\(c_i\). 相邻两份食物之间坐着一个人,共有\(n\)个人. ...
- Codeforces Round #281 (Div. 2) A. Vasya and Football 暴力水题
A. Vasya and Football time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 关于Vue的一些小技巧
前言 用Vue开发一个网页并不难,但是也经常会遇到一些问题,其实大部分的问题都在文档中有所提及,再不然我们通过谷歌也能成功搜索到问题的答案,为了帮助小伙伴们提前踩坑,在遇到问题的时候,心里大概有个谱知 ...
- CentOS 7提示:ERROR unsupported format character '(0xffffffe7) at/域安装失败,您可以运行下列命令重启您的域:
别理会,直接装即可,这个错误不影响使用.
- [Dynamic Language] Python定时任务框架
APScheduler是一个Python定时任务框架,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务.并以daemon方式运行应用. 在APSchedu ...
- java对mongodb的and, in, or 经常使用操作
DBCollection dbcon = null; DBObject query = new BasicDBObject(); BasicDBList values = new BasicDBLis ...
- SSH深度历险(四) Maven初步学�
这几天接触这个词,非常多遍了,仅仅是浅显的体会到它在GXPT中的优点,功能之强大,又通过网络查询了资料进一步的认识学习了,和大家分享. Maven是基于项目对象模型(POM),能够通过一小段描写叙述信 ...