Skip to content

Commit 088296f

Browse files
committed
Complete IsLeaf() method & tests, clean up GoDoc
1 parent 432d595 commit 088296f

File tree

4 files changed

+113
-109
lines changed

4 files changed

+113
-109
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ a high level - what the tests ***were***. But my experience - in practice - is t
108108
still gets very cluttered quite quickly, so that this high-level overview is not really possible.
109109

110110
I still think this approach is great - but I'd probably reserve it for lower-level components, rather
111-
than for system-wise testing.
111+
than for system testing.
112112

113113
## To Do
114114

node.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,38 @@ import (
44
//"fmt"
55
)
66

7-
type node struct {
7+
// Node is a radix trie node (which may also be a leaf).
8+
type Node struct {
89
value string
9-
children []*node
10+
children []*Node
1011
childCount int
1112
}
1213

13-
func (n *node) IsLeaf() bool {
14+
// IsLeaf may be called to determine of the current node is a leaf.
15+
func (n *Node) IsLeaf() bool {
1416
return n.childCount == 0
1517
}
1618

17-
func (n *node) makeChildNode(s string) *node {
19+
func (n *Node) makeChildNode(s string) *Node {
1820
//fmt.Printf("makingChildNode: %s\n", s)
1921
child := makeNode(s)
2022
n.childCount++
2123
if n.children == nil {
22-
n.children = []*node{&child}
24+
n.children = []*Node{&child}
2325
} else {
2426
n.children = append(n.children, &child)
2527
}
2628
return &child
2729
}
2830

29-
func (n *node) setChildNode(newNode *node) bool {
31+
func (n *Node) setChildNode(newNode *Node) bool {
3032
//fmt.Printf("settingChildNode: %v\n", newNode)
3133
n.childCount = 1
32-
n.children = []*node{newNode}
34+
n.children = []*Node{newNode}
3335
return true
3436
}
3537

36-
func makeNode(s string) node {
38+
func makeNode(s string) Node {
3739
//fmt.Printf("makingNode: %s\n", s)
38-
return node{value: s, childCount: 0}
40+
return Node{value: s, childCount: 0}
3941
}

trie.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package trie provides primitives for processing radix tries.
12
package trie
23

34
import (
@@ -7,16 +8,16 @@ import (
78

89
// Trie is a radix trie implementation.
910
type 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.
1516
func 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.
2021
func (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.
2932
func (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 {
9598
func (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

Comments
 (0)