Skip to content

Commit 42ecf00

Browse files
committed
Update index.go
1 parent 8fd553b commit 42ecf00

File tree

1 file changed

+26
-77
lines changed

1 file changed

+26
-77
lines changed

implement-trie-prefix-tree/index.go

Lines changed: 26 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,53 @@
11
package implement_trie_prefix_tree
22

33
type Trie struct {
4-
Prefix string
5-
IsEnd bool
6-
Children map[string]*Trie
7-
words map[string]bool
4+
children map[rune]*Trie
5+
isEnd bool
6+
7+
words map[string]bool
88
}
99

1010
func Constructor() Trie {
11-
return Trie{
12-
words: make(map[string]bool),
13-
14-
Children: make(map[string]*Trie)}
11+
return Trie{children: make(map[rune]*Trie),
12+
words: make(map[string]bool)}
1513
}
1614

1715
func (t *Trie) Insert(word string) {
16+
1817
t.words[word] = true
19-
if t.Prefix == "" && !t.IsEnd {
20-
t.Prefix = word
21-
t.IsEnd = true
22-
return
23-
}
24-
if t.Prefix == word {
25-
t.IsEnd = true
26-
return
27-
}
28-
prefixLen := CommonPrefixLen(t.Prefix, word)
29-
if len(t.Prefix) > prefixLen {
30-
t.split(prefixLen)
31-
}
18+
node := t
19+
for _, ch := range word {
3220

33-
if len(word) > prefixLen {
34-
kid := t.findOrCreateKid(word[prefixLen : prefixLen+1])
35-
kid.Insert(word[prefixLen+1:])
36-
} else {
37-
t.IsEnd = true
38-
}
39-
}
40-
func (node *Trie) findOrCreateKid(char string) *Trie {
41-
kid, ok := node.Children[char]
42-
if ok {
43-
return kid
21+
if node.children[ch] == nil {
22+
var nt = Constructor()
23+
node.children[ch] = &nt
24+
}
25+
node = node.children[ch]
4426
}
45-
kidn := Constructor()
46-
47-
node.Children[char] = &kidn
48-
return &kidn
27+
node.isEnd = true
4928
}
50-
func (node *Trie) split(n int) {
5129

52-
node.Children = map[string]*Trie{}
53-
var kidn = Constructor()
54-
var ntn = &kidn
55-
ntn.Prefix = node.Prefix[n+1:]
56-
ntn.Children = node.Children
57-
ntn.IsEnd = node.IsEnd
58-
node.Children[node.Prefix[n:n+1]] = ntn
59-
node.Prefix = node.Prefix[:n]
60-
node.IsEnd = false
30+
func (t *Trie) SearchPrefix(prefix string) *Trie {
31+
node := t
32+
for _, ch := range prefix {
6133

34+
if node.children[ch] == nil {
35+
return nil
36+
}
37+
node = node.children[ch]
38+
}
39+
return node
6240
}
6341

6442
func (t *Trie) Search(word string) bool {
6543
return t.words[word]
6644
}
6745

6846
func (t *Trie) StartsWith(prefix string) bool {
69-
var has = t.words[prefix]
70-
if has {
71-
return true
72-
}
73-
// fmt.Println(t.Prefix,t.IsEnd,t.Children,t.words)
74-
// fmt.Println(prefix)
75-
if len(prefix) == 0 {
76-
return true
77-
}
78-
if t.Prefix == prefix {
79-
return true
80-
}
8147

82-
n := CommonPrefixLen(t.Prefix, prefix)
83-
if n == len(t.Prefix) {
84-
kid, found := t.Children[prefix[n:n+1]]
85-
if found {
86-
return kid.StartsWith(prefix[n+1:])
48+
if t.words[prefix] {
8749

88-
}
89-
}
90-
if n == len(prefix) {
9150
return true
9251
}
93-
return false
94-
}
95-
func CommonPrefixLen(s1, s2 string) int {
96-
n1, n2 := len(s1), len(s2)
97-
i := 0
98-
for ; i < n1 && i < n2; i++ {
99-
if s1[i] != s2[i] {
100-
break
101-
}
102-
}
103-
return i
52+
return t.SearchPrefix(prefix) != nil
10453
}

0 commit comments

Comments
 (0)