-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_test.go
135 lines (113 loc) · 3.1 KB
/
node_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
package AVL_Tree
import (
"testing"
)
func TestNode(t *testing.T) {
//Dummy tree for feeding into operations (normally used for replacing for swapping the trunk)
tr := NewAVLTree()
//Manually create a basic AVL tree
var a, b, c *node
setNewLeafs := func() {
a = newNodeLeaf(nil, []byte("a"), []byte("vA"))
b = newNodeLeaf(nil, []byte("b"), []byte("vB"))
c = newNodeLeaf(nil, []byte("c"), []byte("vC"))
}
//String the three nodes together to a basic unbalanced tree
//////////////////////////////////////////
// Test Configurations
//////////////////////////////////////////
// Config 1 Config 2 Config 3 Config 4
// a a c c
// \ \ / /
// b c b a
// \ / / \
// c b a b
setConfig1 := func() {
setNewLeafs()
a.rightNode = b
b.parNode = a
b.rightNode = c
c.parNode = b
}
setConfig2 := func() {
setNewLeafs()
a.rightNode = c
c.parNode = a
c.leftNode = b
b.parNode = c
}
setConfig3 := func() {
setNewLeafs()
c.leftNode = b
b.parNode = c
b.leftNode = a
a.parNode = b
}
setConfig4 := func() {
setNewLeafs()
c.leftNode = a
a.parNode = c
a.rightNode = b
b.parNode = a
}
//Start by testing configuration 1
setConfig1()
heightBalanceTest := func(node *node, expectedHeight int, expectedBal int) {
//test height
height := node.height
if height != expectedHeight {
t.Errorf("bad height for %v, expected %v found %v ",
string(node.key[:]), expectedHeight, height)
t.Log(a.outputStructure())
}
//test balance
bal := node.getBalance()
if bal != expectedBal {
t.Errorf("bad balance for %v, expected %v found %v ",
string(node.key[:]), expectedBal, bal)
t.Log(a.outputStructure())
}
}
//Test a non-recursive height update
a.updateHeight()
heightBalanceTest(a, 1, 1) //height still 1 because b hasn't been updated
b.updateHeight()
heightBalanceTest(b, 1, 1)
a.updateHeight()
heightBalanceTest(a, 2, 2) //height/balance now 2 because b has been updated
//Test rebalance to the expected position:
// b
// / \
// a c
testStructure := func() {
heightBalanceTest(a, 0, 0)
heightBalanceTest(b, 1, 0)
heightBalanceTest(c, 0, 0)
}
//Test manual rotation
a.rotate(&tr, true)
testStructure()
//Test rotation with updateBalance
setConfig1()
c.updateHeight()
b.updateHeight()
a.updateHeight()
a.updateBalance(&tr)
testStructure()
//Test rotation with updateHeightBalanceRecursive
setConfig1()
c.updateHeightBalanceRecursive(&tr)
testStructure()
//Test some alternate configurations to see if they perform the adequate rotations.
//Note the operations is always taken from the leaf node (which is where they would be called)
// under the tree.go operations.
setConfig2()
b.updateHeightBalanceRecursive(&tr)
testStructure()
setConfig3()
a.updateHeightBalanceRecursive(&tr)
testStructure()
setConfig4()
b.updateHeightBalanceRecursive(&tr)
testStructure()
}