-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie_test.go
147 lines (134 loc) · 2.79 KB
/
trie_test.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package foo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test(test *testing.T) {
assert := assert.New(test)
t := Trie{&Node{
0, false, make(map[rune]*Node),
}}
assert.False(t.Remove("food"), "Can not remove")
words := []string{"a", "at", "has", "hat", "he", "me", "men", "mens", "met"}
for _, word := range words {
t.Insert(word)
}
foundWords := t.ListWords()
assert.Equal(len(words), len(foundWords), "Word list length same")
for _, word := range foundWords {
assert.True(t.Find(word))
}
t.Remove("me")
wordsRemoved := []string{"me"}
words = []string{"a", "at", "has", "hat", "he", "men", "mens", "met"}
for _, word := range words {
assert.True(t.Find(word))
}
for _, word := range wordsRemoved {
assert.False(t.Find(word))
}
t.Remove("mens")
wordsRemoved = append(wordsRemoved, "mens")
words = []string{"a", "at", "has", "hat", "he", "men", "met"}
for _, word := range words {
assert.True(t.Find(word))
}
for _, word := range wordsRemoved {
assert.False(t.Find(word))
}
t.Remove("a")
wordsRemoved = append(wordsRemoved, "a")
words = []string{"at", "has", "hat", "he", "men", "met"}
for _, word := range words {
assert.True(t.Find(word))
}
for _, word := range wordsRemoved {
assert.False(t.Find(word))
}
t.Remove("has")
wordsRemoved = append(wordsRemoved, "has")
words = []string{"at", "hat", "he", "men", "met"}
for _, word := range words {
assert.True(t.Find(word))
}
for _, word := range wordsRemoved {
assert.False(t.Find(word))
}
}
type Node struct {
Key rune
IsWord bool
Children map[rune]*Node
}
type Trie struct {
Root *Node
}
func (t *Trie) Find(word string) bool {
curr := t.Root
if curr == nil {
return false
}
for _, char := range word {
next := curr.Children[char]
if next == nil {
return false
}
curr = next
}
return curr.IsWord
}
func (t *Trie) Insert(word string) {
if len(word) == 0 {
return
}
if t.Root == nil {
t.Root = &Node{
0, false, make(map[rune]*Node),
}
}
curr := t.Root
for _, char := range word {
next := curr.Children[char]
if next == nil {
next = &Node{
char, false, make(map[rune]*Node),
}
curr.Children[char] = next
}
curr = next
}
curr.IsWord = true
}
func (t *Trie) Remove(word string) bool {
curr := t.Root
for _, char := range word {
curr = curr.Children[char]
if curr == nil {
return false
}
}
if curr.IsWord {
curr.IsWord = false
return true
}
return false
}
func (t *Trie) ListWords() []string {
var res []string
curr := t.Root
for _, v := range curr.Children {
res = append(res, help("", v)...)
}
return res
}
func help(s string, node *Node) []string {
var res []string
t := s + string(node.Key)
if node.IsWord {
res = append(res, t)
}
for _, v := range node.Children {
res = append(res, help(t, v)...)
}
return res
}