-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.go
60 lines (56 loc) · 1.53 KB
/
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
54
55
56
57
58
59
60
package eject
import (
"strings"
)
//Trie Tree
type Trie struct {
Part string `json:"path"`
Children map[string]*Trie `json:"children"`
IsLeaf bool `json:"isleaf"`
FullPath string `json:"fullpath"`
}
//Insert Node into Trie
func (t *Trie) Insert(path string) {
//split path
paths := strings.Split(path, "/")[1:]
for _, f := range paths {
if child, exist := t.Children[f]; exist {
t = child
} else if len(f) > 0 {
isLeaf := f[0] == '*'
nextTrie := &Trie{Part: f, Children: make(map[string]*Trie)} //if start by '*' it is leaf
t.Children[f] = nextTrie //move new trie to parent node's children
t = nextTrie //pointed it to new trie
if isLeaf {
break
}
} else {
break
}
}
t.IsLeaf = true //set leaf node to true
t.FullPath = path //set full path
}
//Search Node from Trie
func (t *Trie) Search(paths []string, length int, index int, form map[string]string) *Trie {
if index == length || len(paths[index])==0 { //the latest item of paths
return t
}
if child, exist := t.Children[paths[index]]; exist { //精确匹配
return child.Search(paths, length, index+1, form)
}
for key, trie := range t.Children { //模糊匹配
if key[0] == ':' {
form[key[1:]] = paths[index]
if length == index+1 {
return trie
}
return trie.Search(paths, length, index+1, form)
}
if key[0] == '*' {
form[key[1:]] = strings.Join(paths[index:], "/")
return trie
}
}
return nil
}