/
Trie.kt
58 lines (51 loc) · 1.35 KB
/
Trie.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.daily.algothrim.trie
/**
* Trie树,解决字符串查询
*/
class Trie {
companion object {
@JvmStatic
fun main(args: Array<String>) {
Trie().apply {
insert("hello".toCharArray())
insert("her".toCharArray())
insert("hi".toCharArray())
insert("how".toCharArray())
insert("so".toCharArray())
insert("see".toCharArray())
println(find("see".toCharArray()))
}
}
}
private val root = TrieNode('/')
class TrieNode(val data: Char) {
val children = Array<TrieNode?>(26) { null }
var isEndingChar = false
}
/**
* O(n)所有字符串的长度之和
*/
fun insert(text: CharArray) {
var p = root
text.forEach {
val index = it - 'a'
if (p.children[index] == null) {
p.children[index] = TrieNode(it)
}
p = p.children[index]!!
}
p.isEndingChar = true
}
/**
* O(k)查询的字符串长度
*/
fun find(text: CharArray): Boolean {
var p = root
text.forEach {
val index = it - 'a'
if (p.children[index] == null) return false
p = p.children[index]!!
}
return p.isEndingChar
}
}