-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0208-implement-trie-prefix-tree.go
71 lines (65 loc) · 1.55 KB
/
0208-implement-trie-prefix-tree.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
// NOTE
// 1. Ensure terminal flag is set properly.
// 2. Avoid overwriting terminal flag.
// 3. Distinguish prefix and target.
// 4. Keep the relationship between Trie tree and the word.
type Trie struct {
children map[rune]*Trie
terminal bool
}
func Constructor() Trie {
return Trie{
children: make(map[rune]*Trie, 26),
}
}
func (this *Trie) Insert(word string) {
for _, ch := range word {
if this.children[ch] == nil {
this.children[ch] = &Trie{
children: make(map[rune]*Trie, 26),
// PROBLEM: Inserted Trie node leads to unset terminal flag.
// this.terminal = len(word) == 1
terminal: false,
}
}
// Ensure terminal flag is set.
// Corner Cache:
// 1. Insert(Apple)
// 2. Insert(App)
// PROBLEM: Overwrite the terminal falg.
// this.children[ch].terminal = len(word) == 1
if len(word) == 1 {
this.children[ch].terminal = true
}
this.children[ch].Insert(word[1:])
break
}
}
func (this *Trie) Search(word string) bool {
for _, ch := range word {
child := this.children[ch]
if child == nil {
return false
}
return child.Search(word[1:])
}
return this.terminal
}
func (this *Trie) StartsWith(prefix string) bool {
for _, ch := range prefix {
child := this.children[ch]
if child == nil {
return false
}
return child.StartsWith(prefix[1:])
}
// terminal(target) or non-terminal(prefix)
return true
}
/**
* Your Trie object will be instantiated and called as such:
* obj := Constructor();
* obj.Insert(word);
* param_2 := obj.Search(word);
* param_3 := obj.StartsWith(prefix);
*/