1+ // Package trie provides primitives for processing radix tries.
12package trie
23
34import (
@@ -7,16 +8,16 @@ import (
78
89// Trie is a radix trie implementation.
910type Trie struct {
10- child []* node
11+ child []* Node
1112 count int
1213}
1314
14- // NewTrie is used to create a new Trie .
15+ // NewTrie is used to create a new radix trie .
1516func NewTrie () * Trie {
1617 return & Trie {}
1718}
1819
19- // Count returns the number of nodes in the Trie .
20+ // Count returns the number of nodes in the trie .
2021func (t * Trie ) Count () int {
2122 return t .count
2223}
@@ -25,7 +26,9 @@ func (t *Trie) isEmpty() bool {
2526 return t .count == 0
2627}
2728
28- // Insert is used to add a new term to the Trie.
29+ // Insert is used to add a new term to the trie.
30+ // If successful, this will create one or more child
31+ // nodes.
2932func (t * Trie ) Insert (s string ) bool {
3033
3134 // remove leading & trailing whitespace
@@ -59,7 +62,7 @@ func (t *Trie) Insert(s string) bool {
5962 return true
6063}
6164
62- func (t * Trie ) insertRuneNode (parent * node , n * node , s string ) bool {
65+ func (t * Trie ) insertRuneNode (parent * Node , n * Node , s string ) bool {
6366
6467 for _ , c := range n .children {
6568 index := t .findRuneMatch (c .value , s )
@@ -95,27 +98,28 @@ func (t *Trie) insertRuneNode(parent *node, n *node, s string) bool {
9598func (t * Trie ) makeRuneNode (s string ) {
9699 rootRune := makeNode (s [:1 ])
97100 rootChild := makeNode (s [1 :])
98- rootRune .children = []* node {& rootChild }
101+ rootRune .children = []* Node {& rootChild }
99102 rootRune .childCount = 1
100- t .child = []* node {& rootRune }
103+ t .child = []* Node {& rootRune }
101104 t .count ++
102105}
103106
104- // Find is used to search for a specific term in the Trie .
105- func (t * Trie ) Find (s string ) bool {
107+ // Find is used to search for a specific term in the trie .
108+ func (t * Trie ) Find (s string ) ( bool , * Node ) {
106109
107110 // Remove leading & trailing whitespace
108111 trimmed := strings .TrimSpace (s )
109112
110113 // Sanity check (should catch empty strings too)
111114 if len (s ) < 2 {
112- return false
115+ return false , nil
113116 }
114117
115- return t .findNode (trimmed ) != nil
118+ n := t .findNode (trimmed )
119+ return n != nil , n
116120}
117121
118- func (t * Trie ) findNode (s string ) * node {
122+ func (t * Trie ) findNode (s string ) * Node {
119123
120124 for _ , c := range t .child {
121125 if c .value == s [:1 ] {
@@ -129,7 +133,7 @@ func (t *Trie) findNode(s string) *node {
129133 return nil
130134}
131135
132- func (t * Trie ) findRuneNode (n * node , s string ) * node {
136+ func (t * Trie ) findRuneNode (n * Node , s string ) * Node {
133137
134138 for _ , c := range n .children {
135139 index := t .findRuneMatch (c .value , s )
0 commit comments