Skip to content

Commit 061e3a0

Browse files
committed
solve problem Implement Trie Prefix Tree
1 parent 46a9794 commit 061e3a0

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ All solutions will be accepted!
236236
|106|[Construct Binary Tree From Inorder And Postorder Traversal](https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/)|[java/py/js](./algorithms/ConstructBinaryTreeFromInorderAndPostorderTraversal)|Medium|
237237
|105|[Construct Binary Tree From Preorder And Inorder Traversal](https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/)|[java/py/js](./algorithms/ConstructBinaryTreeFromPreorderAndInorderTraversal)|Medium|
238238
|654|[Maximum Binary Tree](https://leetcode-cn.com/problems/maximum-binary-tree/description/)|[java/py/js](./algorithms/MaximumBinaryTree)|Medium|
239+
|208|[Implement Trie Prefix Tree](https://leetcode-cn.com/problems/implement-trie-prefix-tree/description/)|[java/py/js](./algorithms/ImplementTriePrefixTree)|Medium|
239240

240241
# Database
241242
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Implement Trie Prefix Tree
2+
This problem is easy to solve
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Trie {
2+
private class TrieNode {
3+
public TrieNode[] children;
4+
public Character ch;
5+
public boolean isEnd;
6+
7+
public TrieNode(Character ch) {
8+
children = new TrieNode[26];
9+
this.ch = ch;
10+
this.isEnd = false;
11+
}
12+
}
13+
14+
TrieNode root;
15+
/** Initialize your data structure here. */
16+
public Trie() {
17+
root = new TrieNode(null);
18+
}
19+
20+
/** Inserts a word into the trie. */
21+
public void insert(String word) {
22+
TrieNode node = root;
23+
for (char ch : word.toCharArray()) {
24+
int index = (int) ch - 97;
25+
if (node.children[index] == null) {
26+
node.children[index] = new TrieNode(ch);
27+
}
28+
node = node.children[index];
29+
}
30+
node.isEnd = true;
31+
}
32+
33+
/** Returns if the word is in the trie. */
34+
public boolean search(String word) {
35+
TrieNode node = root;
36+
for (char ch : word.toCharArray()) {
37+
int index = (int) ch - 97;
38+
if (node.children[index] == null) {
39+
return false;
40+
}
41+
node = node.children[index];
42+
}
43+
return node.isEnd;
44+
}
45+
46+
/** Returns if there is any word in the trie that starts with the given prefix. */
47+
public boolean startsWith(String prefix) {
48+
TrieNode node = root;
49+
for (char ch : prefix.toCharArray()) {
50+
int index = (int) ch - 97;
51+
if (node.children[index] == null) {
52+
return false;
53+
}
54+
node = node.children[index];
55+
}
56+
return true;
57+
}
58+
}
59+
60+
/**
61+
* Your Trie object will be instantiated and called as such:
62+
* Trie obj = new Trie();
63+
* obj.insert(word);
64+
* boolean param_2 = obj.search(word);
65+
* boolean param_3 = obj.startsWith(prefix);
66+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Initialize your data structure here.
3+
*/
4+
var Trie = function() {
5+
this.root = new TrieNode(null)
6+
};
7+
8+
var TrieNode = function (char) {
9+
this.char = char
10+
this.children = new Array(26);
11+
this.children.fill(null)
12+
this.isEnd = false
13+
}
14+
15+
/**
16+
* Inserts a word into the trie.
17+
* @param {string} word
18+
* @return {void}
19+
*/
20+
Trie.prototype.insert = function(word) {
21+
let node = this.root
22+
for (let i = 0; i < word.length; i++) {
23+
let char = word[i],
24+
index = char.charCodeAt(0) - 97
25+
26+
if (!node.children[index]) {
27+
node.children[index] = new TrieNode(char)
28+
}
29+
node = node.children[index]
30+
}
31+
node.isEnd = true
32+
};
33+
34+
/**
35+
* Returns if the word is in the trie.
36+
* @param {string} word
37+
* @return {boolean}
38+
*/
39+
Trie.prototype.search = function(word) {
40+
let node = this.root
41+
for (let i = 0; i < word.length; i++) {
42+
let char = word[i],
43+
index = char.charCodeAt(0) - 97
44+
45+
if (!node.children[index]) {
46+
return false
47+
}
48+
node = node.children[index]
49+
}
50+
return node.isEnd
51+
};
52+
53+
/**
54+
* Returns if there is any word in the trie that starts with the given prefix.
55+
* @param {string} prefix
56+
* @return {boolean}
57+
*/
58+
Trie.prototype.startsWith = function(prefix) {
59+
let node = this.root
60+
for (let i = 0; i < prefix.length; i++) {
61+
let char = prefix[i],
62+
index = char.charCodeAt(0) - 97
63+
64+
if (!node.children[index]) {
65+
return false
66+
}
67+
node = node.children[index]
68+
}
69+
return true
70+
};
71+
72+
/**
73+
* Your Trie object will be instantiated and called as such:
74+
* var obj = Object.create(Trie).createNew()
75+
* obj.insert(word)
76+
* var param_2 = obj.search(word)
77+
* var param_3 = obj.startsWith(prefix)
78+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class Trie(object):
2+
3+
class TrieNode(object):
4+
def __init__(self, char):
5+
self.char = char
6+
self.children = [None] * 26
7+
self.is_end = False
8+
9+
10+
def __init__(self):
11+
"""
12+
Initialize your data structure here.
13+
"""
14+
self.root = self.TrieNode(None)
15+
16+
def insert(self, word):
17+
"""
18+
Inserts a word into the trie.
19+
:type word: str
20+
:rtype: void
21+
"""
22+
node = self.root
23+
for c in word:
24+
index = ord(c) - 97
25+
if not node.children[index]:
26+
node.children[index] = self.TrieNode(c)
27+
node = node.children[index]
28+
node.is_end = True
29+
30+
def search(self, word):
31+
"""
32+
Returns if the word is in the trie.
33+
:type word: str
34+
:rtype: bool
35+
"""
36+
node = self.root
37+
for c in word:
38+
index = ord(c) - 97
39+
if not node.children[index]:
40+
return False
41+
node = node.children[index]
42+
return node.is_end
43+
44+
def startsWith(self, prefix):
45+
"""
46+
Returns if there is any word in the trie that starts with the given prefix.
47+
:type prefix: str
48+
:rtype: bool
49+
"""
50+
node = self.root
51+
for c in prefix:
52+
index = ord(c) - 97
53+
if not node.children[index]:
54+
return False
55+
node = node.children[index]
56+
return True
57+
58+
59+
# Your Trie object will be instantiated and called as such:
60+
# obj = Trie()
61+
# obj.insert(word)
62+
# param_2 = obj.search(word)
63+
# param_3 = obj.startsWith(prefix)

0 commit comments

Comments
 (0)