-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
131 lines (118 loc) · 2.84 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package trie
import (
"fmt"
"strconv"
)
// getDescendents gets every node descendent of n
// recursively collects all children nodes into one slice
func (node *Node) getDescendents() []*Node {
nodes := []*Node{node}
if node == nil {
return nodes
}
for _, childNode := range node.children {
nodes = append(nodes, childNode.getDescendents()...)
}
return nodes
}
// GetString gets the string in recursive fashion
func (node *Node) GetString() string {
if node == nil {
return ""
}
return string(node.Parent.GetString()) + string(node.value)
}
// validateTrie checks to see if all the children that a parent points to point back to the parent
func (node *Node) validateTrie() bool {
if node == nil {
return true
}
for _, child := range node.children {
if child.Parent != node {
return false
}
if !child.validateTrie() {
return false
}
}
return true
}
func (node *Node) incrementLeafCount(i int) {
if node == nil {
return
}
node.leaves += i
node.Parent.incrementLeafCount(i)
}
func (node *Node) decrementLeafCount(i int) {
if node == nil {
return
}
node.leaves -= i
node.Parent.decrementLeafCount(i)
}
func (node *Node) String() string {
if node == nil {
return ""
}
// build string
firstRune := node.value[0]
children := ""
for childNodeFirstRune, childNode := range node.children {
children += "\t" + string(childNodeFirstRune) + "\t->\t" + string(childNode.value) + "\n"
}
top := string(firstRune) + "\t->\t(" + string(node.value) + ")\tcount: " + strconv.Itoa(node.count) + "\tleaves: " + strconv.Itoa(node.leaves) + "\n"
return top + children
}
// DeleteDescendents deletes all descendents of node n
// and adds 'replacement' to the end of nodes value
func (node *Node) DeleteDescendents(replacement rune) int {
if node == nil {
return 0
}
numberOfChildren := len(node.children)
node.value = append(copyRunes(node.value), replacement)
node.children = nil
node.leaves = 0
node.count = 1
return numberOfChildren
}
func (node *Node) getLeaves() []*Node {
nodes := make([]*Node, 0)
if node == nil {
return nodes
}
if node.count != 0 {
nodes = append(nodes, node)
}
for _, child := range node.children {
nodes = append(nodes, child.getDescendents()...)
}
return nodes
}
// getDeepestNode returns the depth of the node and the node itself
func (node *Node) getDeepestNode(depth int) (int, *Node) {
if node == nil || len(node.children) == 0 {
return depth, node
}
nMax := node
d := depth
for _, child := range node.children {
max, nod := child.getDeepestNode(d + 1)
if max > depth {
depth = max
nMax = nod
}
}
return depth, nMax
}
func (node *Node) printNodes() string {
if node == nil {
return ""
}
str := fmt.Sprintf("%s : %d : %d", string(node.value), node.count, node.leaves)
for _, child := range node.children {
str = fmt.Sprintf("%s\n%s", str, child.printNodes())
}
return str
}