|
|
|
@ -5,70 +5,40 @@ const (
|
|
|
|
|
AlphabetSize = 26
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
type TrieNode struct {
|
|
|
|
|
children [AlphabetSize]*TrieNode
|
|
|
|
|
isWordEnd bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Trie struct {
|
|
|
|
|
root *TrieNode
|
|
|
|
|
root *TrieNode
|
|
|
|
|
nodesCount int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New returns a pointer pointing to a new Trie
|
|
|
|
|
// New returns a pointer pointing to a new trie
|
|
|
|
|
func New() *Trie {
|
|
|
|
|
return &Trie{
|
|
|
|
|
root: &TrieNode{},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert inserts a word into the Trie
|
|
|
|
|
func (t *Trie) Insert(word string) {
|
|
|
|
|
=======
|
|
|
|
|
type trieNode struct {
|
|
|
|
|
children [AlphabetSize]*trieNode
|
|
|
|
|
isWordEnd bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type trie struct {
|
|
|
|
|
root *trieNode
|
|
|
|
|
nodesCount int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New returns a pointer pointing to a new trie
|
|
|
|
|
func New() *trie {
|
|
|
|
|
return &trie{
|
|
|
|
|
root: &trieNode{},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert inserts a word into the trie
|
|
|
|
|
func (t *trie) Insert(word string) {
|
|
|
|
|
>>>>>>> 2896b350131353759aacf42da7047972425068eb
|
|
|
|
|
func (t *Trie) Insert(word string) {
|
|
|
|
|
wordLength := len(word)
|
|
|
|
|
current := t.root
|
|
|
|
|
for i := 0; i < wordLength; i++ {
|
|
|
|
|
index := word[i] - 'a'
|
|
|
|
|
if current.children[index] == nil {
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
current.children[index] = &TrieNode{}
|
|
|
|
|
=======
|
|
|
|
|
current.children[index] = &trieNode{}
|
|
|
|
|
t.nodesCount++
|
|
|
|
|
>>>>>>> 2896b350131353759aacf42da7047972425068eb
|
|
|
|
|
}
|
|
|
|
|
current = current.children[index]
|
|
|
|
|
}
|
|
|
|
|
current.isWordEnd = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// Contains checks whether the Trie has the given word or not
|
|
|
|
|
func (t *Trie) Contains(word string) bool {
|
|
|
|
|
=======
|
|
|
|
|
// Contains checks whether the trie has the given word or not
|
|
|
|
|
func (t *trie) Contains(word string) bool {
|
|
|
|
|
>>>>>>> 2896b350131353759aacf42da7047972425068eb
|
|
|
|
|
wordLength := len(word)
|
|
|
|
|
current := t.root
|
|
|
|
|
for i := 0; i < wordLength; i++ {
|
|
|
|
@ -80,7 +50,6 @@ func (t *trie) Contains(word string) bool {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return current.isWordEnd
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (trie *TrieNode, value string, values []string) findWords(){
|
|
|
|
@ -92,16 +61,14 @@ func (trie *TrieNode, value string, values []string) findWords(){
|
|
|
|
|
findWords(trie.children[i], value+string(i+'a'), values)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
=======
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NodesCount returns number of nodes in the tree.
|
|
|
|
|
func (t *trie) NodesCount() int {
|
|
|
|
|
func (t *Trie) NodesCount() int {
|
|
|
|
|
return t.nodesCount
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Empty returns true if tree does not contain any nodes except for root
|
|
|
|
|
func (t *trie) Empty() bool {
|
|
|
|
|
func (t *Trie) Empty() bool {
|
|
|
|
|
return t.nodesCount == 0
|
|
|
|
|
>>>>>>> 2896b350131353759aacf42da7047972425068eb
|
|
|
|
|
}
|
|
|
|
|