-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
54 lines (47 loc) · 1.36 KB
/
node.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
package trie
import (
//"fmt"
)
// Node is a radix trie node (which may also be a leaf).
type Node struct {
value string
children []*Node
childCount int
entry bool
}
// IsEntry may be called to determine if the current node is
// terminal for an entry. Note that this node may or may not
// also be a leaf. In the case of 'slow' and 'slowly', both
// are entries but only 'slowly' can be a leaf.
func (n *Node) IsEntry() bool {
return n.entry
}
// IsLeaf may be called to determine if the current node is a
// leaf. Note that a leaf is a terminal node but that a node
// may also be terminal for an entry but not a leaf. In the
// case of 'slow' and 'slowly', 'slow' is NOT a leaf, even
// though it is terminal for the entry 'slow'.
func (n *Node) IsLeaf() bool {
return n.childCount == 0
}
func (n *Node) makeChildNode(s string, entry bool) *Node {
//fmt.Printf("makingChildNode: %s\n", s)
child := makeNode(s, entry)
n.childCount++
if n.children == nil {
n.children = []*Node{&child}
} else {
n.children = append(n.children, &child)
}
return &child
}
func (n *Node) setChildNode(newNode *Node) bool {
//fmt.Printf("settingChildNode: %v\n", newNode)
n.childCount = 1
n.children = []*Node{newNode}
return true
}
func makeNode(s string, isEntry bool) Node {
//fmt.Printf("makingNode: %s\n", s)
return Node{value: s, childCount: 0, entry: isEntry}
}