-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerictree_test.go
180 lines (161 loc) · 4.63 KB
/
generictree_test.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"cmp"
"fmt"
"math"
"testing"
)
// To keep our test functions generic, we need to turn
// the test types into generic types as well.
type tree[Value cmp.Ordered, Data any] struct {
name string
value []Value
data []Data
}
var (
// create test data with string search values and string data.
trees = []tree[string, string]{
{
name: "empty",
value: []string{},
data: []string{},
},
{
name: "onenode",
value: []string{"0"},
data: []string{"zero"},
},
{
name: "twonodes",
value: []string{"0", "1"},
data: []string{"zero", "one"},
},
{
name: "random",
value: []string{"d", "b", "g", "g", "c", "e", "a", "h", "f", "i", "j", "l", "k"},
data: []string{"delta", "bravo", "golang", "golf", "charlie", "echo", "alpha", "hotel", "foxtrot", "india", "juliett", "lima", "kilo"},
},
{
name: "ascending",
value: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"},
data: []string{"alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india", "juliett", "kilo", "lima", "mike"},
},
{
name: "descending",
value: []string{"m", "l", "k", "j", "i", "h", "g", "f", "e", "d", "c", "b", "a"},
data: []string{"mike", "lima", "kilo", "juliett", "india", "hotel", "golf", "foxtrot", "echo", "delta", "charlie", "bravo", "alpha"},
},
{
name: "issue2",
value: []string{"3", "5", "1", "0", "2", "4", "6", "7", "8"},
data: []string{"3", "5", "1", "0", "2", "4", "6", "7", "8"},
},
{
name: "balancedfromthestart",
value: []string{"4", "2", "6", "1", "7", "3", "5"},
data: []string{"4", "2", "6", "1", "7", "3", "5"},
},
}
)
// Instantiate the types upon use
func newTree(t tree[string, string]) *Tree[string, string] {
tree := &Tree[string, string]{}
for i := 0; i < len(t.value); i++ {
tree.Insert(t.value[i], t.data[i])
}
return tree
}
// calculate the height recursively, without relying on n.height
func (n *Node[Value, Data]) recHeight() int {
if n == nil {
return 0
}
return 1 + max(n.Left.recHeight(), n.Right.recHeight())
}
func (n *Node[Value, Data]) checkHeight() (*Node[Value, Data], bool) {
if n == nil {
return nil, true
}
if n.height != n.recHeight() {
return n, false
}
if node, ok := n.Left.checkHeight(); !ok {
return node, false
}
if node, ok := n.Right.checkHeight(); !ok {
return node, false
}
return nil, true
}
// A (sub-)tree is balanced if the heights of the two child subtrees of any node differ by at most one.
func (n *Node[Value, Data]) isBalanced() bool {
return n == nil || n.Right.recHeight()-n.Left.recHeight() <= 1
}
func (n *Node[Value, Data]) checkBalances() (problem string) {
if n == nil {
return ""
}
rh, lh := n.Right.recHeight(), n.Left.recHeight()
if n.Bal() != rh-lh {
problem = fmt.Sprintf("Node %v has balance %d but right height %d and left height %d\n", n.Value, n.Bal(), rh, lh)
}
return problem + n.Right.checkBalances() + n.Left.checkBalances()
}
func (t *Tree[Value, Data]) containsAllElements(source tree[Value, Data]) (Value, bool) {
for _, v := range source.value {
_, found := t.Find(v)
if !found {
return v, false
}
}
var zero Value
return zero, true
}
func (t *Tree[Value, Data]) isSorted() bool {
var sorted func(*Node[Value, Data]) bool
sorted = func(n *Node[Value, Data]) bool {
if n == nil {
return true
}
if (n.Left != nil && n.Value < n.Left.Value) ||
(n.Right != nil && n.Value > n.Right.Value) {
return false
}
return sorted(n.Left) && sorted(n.Right)
}
return sorted(t.Root)
}
func TestTree_rebalance(t *testing.T) {
for _, tree := range trees {
t.Run(tree.name, func(t *testing.T) {
fmt.Println("Creating tree ", tree.name)
tt := newTree(tree)
tt.Dump()
h := tt.Root.recHeight()
lh, rh := 0, 0
if tt.Root != nil {
lh = tt.Root.Left.recHeight()
rh = tt.Root.Right.recHeight()
}
exh := 2.0*math.Log2(float64(len(tree.value))+1.44) - 0.328
heightImbalance := ""
if float64(h) > exh {
heightImbalance = fmt.Sprintf("Height: %d - expected: %0f\nLeft.Height(): %d, Right.Height(): %d\n", h, exh, lh, rh)
}
wrongBalanceFactors := tt.Root.checkBalances()
problem := heightImbalance + wrongBalanceFactors
if v, ok := tt.containsAllElements(tree); !ok {
problem += fmt.Sprintf("Some data in the tree is missing or wrong: %s\n", v)
}
if !tt.isSorted() {
problem += fmt.Sprintf("Tree %s is not balanced\n", tree.name)
}
if n, ok := tt.Root.checkHeight(); !ok {
problem += fmt.Sprintf("Actual height %d differs from recorded height %d in node %s\n", n.recHeight(), n.height, n.Value)
}
if len(problem) > 0 {
t.Error(problem)
}
})
}
}