-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.go
53 lines (42 loc) · 836 Bytes
/
trie.go
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
package trie
type Trie struct {
value rune
children []Trie
}
func (start *Trie) add (string string) bool{
var findOrNot bool
existed := true
//TODO throw error if space in string
for _,char:=range string + " "{
findOrNot = false
for _,node:=range start.children{
if char == node.value{
findOrNot = true
break
}
}
if findOrNot == false{
existed = false
start.children = append(start.children, Trie{char, []Trie{}})
start = &start.children[len(start.children)-1]
}
}
return existed
}
func (start *Trie) find(string string) bool{
var findOrNot bool
for _,char:=range string + ""{
findOrNot = false
for _,node:=range start.children{
if char == node.value {
start = &node
findOrNot = true
break
}
}
if findOrNot == false{
return false
}
}
return true
}