-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace.go
63 lines (57 loc) · 1.47 KB
/
namespace.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
package neo4j
import (
"strings"
)
// Node represents each node in the trie
type TrieNode struct {
children map[rune]*TrieNode
isTerminal bool
mapping string
}
// Trie represents a trie and has a pointer to the root node
type Trie struct {
root *TrieNode
}
// InitTrie will create a new Trie
func InitTrie() *Trie {
result := &Trie{root: &TrieNode{
children: make(map[rune]*TrieNode),
}}
return result
}
// Insert will take in a word and add it to the trie
func (t *Trie) Insert(word, mapping string) {
currentNode := t.root
for _, ch := range word {
if _, exists := currentNode.children[ch]; !exists {
newChild := &TrieNode{children: make(map[rune]*TrieNode)}
currentNode.children[ch] = newChild
currentNode = newChild
} else {
currentNode = currentNode.children[ch]
}
}
currentNode.mapping = mapping
currentNode.isTerminal = true
}
// Search will search if a word is in the trie
func (t *Trie) Search(word string) (string, string, bool) {
currentNode := t.root
prefix := strings.Builder{}
var lastPrefixMapping string
var lastPrefix string
var lastPrefixFlag bool
for _, ch := range word {
if _, ok := currentNode.children[ch]; !ok {
return lastPrefix, lastPrefixMapping, lastPrefixFlag
}
currentNode = currentNode.children[ch]
prefix.WriteRune(ch)
if currentNode.isTerminal {
lastPrefix = prefix.String()
lastPrefixMapping = currentNode.mapping
lastPrefixFlag = true
}
}
return lastPrefix, lastPrefixMapping, lastPrefixFlag
}