Skip to content

Commit 712c51f

Browse files
committed
Add Design A Trie
1 parent 454feb7 commit 712c51f

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package common.tries
2+
3+
class DesignTrie {
4+
class Trie {
5+
val root = TrieNode()
6+
7+
// insert
8+
fun insert(word: String) {
9+
var node = root
10+
for (c in word.toCharArray()) {
11+
// For each character in the word, if it's not a child of
12+
// the current node, create a new TrieNode for that
13+
// character.
14+
val index = c - 'a'
15+
if (node.children[index] == null) {
16+
node.children[index] = TrieNode()
17+
}
18+
node = node.children[index]!!
19+
}
20+
// Mark the last node as the end of a word.
21+
node.isWord = true
22+
}
23+
24+
// search
25+
fun search(word: String): Boolean {
26+
var node = root
27+
for (c in word.toCharArray()) {
28+
// For each character in the word, if it's not a child of
29+
// the current node, the word doesn't exist in the Trie.
30+
val index = c - 'a'
31+
if (node.children[index] == null) {
32+
return false
33+
}
34+
node = node.children[index]!!
35+
}
36+
// Return whether the current node is marked as the end of the word.
37+
return node.isWord
38+
}
39+
40+
// hasPrefix
41+
fun hasPrefix(word: String): Boolean {
42+
var node = root
43+
for (c in word.toCharArray()) {
44+
val index = c - 'a'
45+
if (node.children[index] == null) {
46+
return false
47+
}
48+
node = node.children[index]!!
49+
}
50+
// Once we've traversed the nodes corresponding to each
51+
// character in the prefix, return True.
52+
return true
53+
}
54+
}
55+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package common.tries
2+
3+
data class TrieNode(
4+
var isWord: Boolean = false,
5+
var word: String? = null,
6+
val children: Array<TrieNode?> = Array(26) { null }
7+
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package common.tries
2+
3+
import common.tries.DesignTrie.Trie
4+
import org.junit.jupiter.api.Assertions.*
5+
import org.junit.jupiter.api.BeforeAll
6+
import org.junit.jupiter.api.Test
7+
8+
class DesignTrieTest {
9+
10+
@Test
11+
fun `Expected containing word`() {
12+
assertTrue { trie.search("banana") }
13+
assertTrue { trie.search("pineapple") }
14+
}
15+
16+
@Test
17+
fun `Expected not containing word`() {
18+
assertFalse { trie.search("kiwi") }
19+
}
20+
21+
@Test
22+
fun `Expected contain prefix`() {
23+
assertTrue { trie.hasPrefix("water") }
24+
assertTrue { trie.hasPrefix("black") }
25+
}
26+
27+
@Test
28+
fun `Expected not contain prefix`() {
29+
assertFalse { trie.hasPrefix("white") }
30+
}
31+
32+
companion object {
33+
34+
val fruits: List<String> = listOf(
35+
"apple",
36+
"banana",
37+
"grape",
38+
"orange",
39+
"watermelon",
40+
"strawberry",
41+
"blueberry",
42+
"mango",
43+
"pineapple",
44+
"peach",
45+
"cherry",
46+
"pear",
47+
"plum",
48+
"blackberry",
49+
"raspberry",
50+
"coconut",
51+
"papaya"
52+
)
53+
54+
private lateinit var trie: Trie
55+
56+
@JvmStatic
57+
@BeforeAll
58+
fun init() {
59+
trie = Trie()
60+
for (fruit in fruits) {
61+
trie.insert(fruit)
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)