Skip to content

Commit cdc1703

Browse files
committed
✨ [209] PREFIX TREE BY MYSELF!
1 parent a9b09b3 commit cdc1703

File tree

4 files changed

+188
-9
lines changed

4 files changed

+188
-9
lines changed

209/my_solution.js

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
class Trie {
2+
constructor() {
3+
const LENGTH = 26;
4+
this.isEndOfWord = false;
5+
this.children = new Array(LENGTH).fill(null);
6+
}
7+
8+
getIdx(char) {
9+
return char.charCodeAt() - "a".charCodeAt();
10+
}
11+
12+
/**
13+
* @param {string} word
14+
* @return {void}
15+
*/
16+
insert(word) {
17+
let root = this;
18+
for (let i = 0; i < word.length; i++) {
19+
let idx = this.getIdx(word[i]);
20+
console.log(`word:${word[i]}, idx:${idx}`)
21+
if (root.children[idx] === null) root.children[idx] = new Trie();
22+
root = root.children[idx];
23+
}
24+
root.isEndOfWord = true;
25+
// console.log(this)
26+
}
27+
28+
/**
29+
* @param {string} word
30+
* @return {boolean}
31+
*/
32+
search(word) {
33+
console.log(`Searching "${word}" ...`)
34+
const dfs = (i, node) => {
35+
// if (i === word.length) return false;
36+
console.log(`i:${i}, word:${word[i]}`)
37+
console.log(node)
38+
if (word[i] === undefined) return node.isEndOfWord;
39+
40+
let child = node.children[this.getIdx(word[i])];
41+
if (node.isEndOfWord && i === word.length) return true;
42+
if (null === child) return false;
43+
// console.log(child)
44+
45+
return dfs(i + 1, child);
46+
}
47+
48+
return dfs(0, this);
49+
}
50+
51+
/**
52+
* @param {string} prefix
53+
* @return {boolean}
54+
*/
55+
startsWith(prefix) {
56+
const dfs = (i, node) => {
57+
console.log(`i:${i}, prefix:${prefix[i]}`)
58+
if (prefix[i] === undefined) return !(null === node);
59+
60+
let child = node.children[this.getIdx(prefix[i])];
61+
if (node.isEndOfWord && i === prefix.length) return true;
62+
if (null === child) return false;
63+
64+
return dfs(i + 1, child);
65+
}
66+
67+
return dfs(0, this);
68+
}
69+
}
70+
71+
72+
73+
74+
/**
75+
* Your Trie object will be instantiated and called as such:
76+
* var obj = new Trie()
77+
* obj.insert(word)
78+
* var param_2 = obj.search(word)
79+
* var param_3 = obj.startsWith(prefix)
80+
*/
81+
82+
var obj = new Trie()
83+
obj.insert("hello")
84+
// obj.insert("a")
85+
console.log(obj.search("helloa"))
86+
// obj.insert("wor")
87+
// // console.log(obj.children[22].children[14].children[17]) // 17,3
88+
// console.log(obj.search("wor"))
89+
// console.log(obj.search("wo"))
90+
// console.log(obj.startsWith("z"))
91+
92+
// [
93+
// "Trie",
94+
// "insert",
95+
// "search", "search", "search",
96+
// "startsWith", "startsWith", "startsWith"]
97+
// [
98+
// [],
99+
// ["hello"],
100+
// ["hell"], ["helloa"], ["hello"],
101+
// ["hell"], ["helloa"], ["hello"]]
102+
// [
103+
// null,
104+
// null,
105+
// false, false, true,
106+
// true, false, true]

209/solution.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Trie {
2+
constructor() {
3+
const LENGTH = 26;
4+
this.isEndOfWord = false;
5+
this.children = new Array(LENGTH).fill(null);
6+
}
7+
8+
getIdx(char) {
9+
return char.charCodeAt() - "a".charCodeAt();
10+
}
11+
12+
/**
13+
* @param {string} word
14+
* @return {void}
15+
*/
16+
insert(word) {
17+
let root = this;
18+
for (let i = 0; i < word.length; i++) {
19+
let idx = this.getIdx(word[i]);
20+
if (root.children[idx] === null) root.children[idx] = new Trie();
21+
root = root.children[idx];
22+
}
23+
root.isEndOfWord = true;
24+
}
25+
26+
/**
27+
* @param {string} word
28+
* @return {boolean}
29+
*/
30+
search(word) {
31+
const dfs = (i, node) => {
32+
if (word[i] === undefined) return node.isEndOfWord;
33+
34+
let child = node.children[this.getIdx(word[i])];
35+
if (node.isEndOfWord && i === word.length) return true;
36+
if (null === child) return false;
37+
38+
return dfs(i + 1, child);
39+
}
40+
41+
return dfs(0, this);
42+
}
43+
44+
/**
45+
* @param {string} prefix
46+
* @return {boolean}
47+
*/
48+
startsWith(prefix) {
49+
const dfs = (i, node) => {
50+
if (prefix[i] === undefined) return !(null === node);
51+
52+
let child = node.children[this.getIdx(prefix[i])];
53+
if (node.isEndOfWord && i === prefix.length) return true;
54+
if (null === child) return false;
55+
56+
return dfs(i + 1, child);
57+
}
58+
59+
return dfs(0, this);
60+
}
61+
}

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
- [200. Number of Islands](./200/)
5757
- [206. Reverse Linked List](./206/)
5858
- [207. Course Schedule](./207/)
59+
- [208. Implement Trie (Prefix Tree)](./209/)
5960
- [211. Design Add and Search Words Data Structure](./211/)
6061
- [213. House Robber II](./213/)
6162
- [217. Contains Duplicate](./217/)
@@ -147,7 +148,7 @@ Batch create:
147148
NOTE: JS IS HERE
148149
-->
149150
```ssh
150-
chapter=211 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
151+
chapter=209 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
151152
```
152153
> then you can use `x` for quick debug.
153154

test.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,19 @@
5454
// o[2] = [1, 3, 4]
5555
// console.log(o)
5656
//
57-
let x = Array(3).fill([]);
58-
x[1].push(2)
59-
// x[1] = 2
60-
console.log(x)
57+
// let x = Array(3).fill([]);
58+
// x[1].push(2)
59+
// // x[1] = 2
60+
// console.log(x)
6161
//
62-
// const g = Array(3).fill().map(() => []);
63-
// console.log(g)
62+
n = 5;
63+
const g =
64+
// Array(n).fill([]);
65+
// Array(n).fill().map(() => []);
66+
Array.from({ length: n }, () => []);
67+
g[1].push(2)
68+
69+
console.log(g)
6470

6571

6672
// Set
@@ -75,8 +81,13 @@ console.log(x)
7581
// console.log(set.has([1, 2]))
7682
// console.log(set.has(x))
7783

78-
// Map
79-
// console.log(new Map([["key", "value"], ["rrsrsr", true]]))
84+
// // Map
85+
// let map = new Map([["key", "value"], ["rrsrsr", true]])
86+
// console.log(map)
87+
// console.log(map.values()) // "value", true
88+
// for (let value of map.values()) {
89+
// console.log(`in loop:${value}`)
90+
// }
8091

8192
// Array
8293
// let l = [];

0 commit comments

Comments
 (0)