-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_test.go
204 lines (173 loc) · 4.7 KB
/
tree_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package rbtree
import "testing"
import "fmt"
import "container/vector"
import "sort"
import "rand"
func NewIntTree() *RBTree {
intComp := func(i interface{}, j interface{}) int {
return i.(int) - j.(int);
}
return NewTree(intComp)
}
func MakeIntTree() *RBTree {
tree := NewIntTree()
tree.Insert(5)
tree.Insert(10)
tree.Insert(7)
tree.Insert(0)
tree.Insert(3)
tree.Insert(3)
tree.Insert(20)
tree.Insert(20)
tree.Insert(15)
tree.Insert(2)
return tree
}
func verifyElements(tree *RBTree, a []int, t *testing.T) {
c := make([]int, len(a))
copy(c, a)
sort.SortInts(c)
index := 0
tree.For(func(e interface{}) bool {
i := e.(int)
if i != c[index] {
t.Errorf("Expected %d, Actual %d", c[index], i)
return false
}
index++
return true
});
}
func verifyTreeWithRandomData(a []int, t *testing.T) {
s := NewIntTree();
for i,v := range a {
if !s.Insert(v) {
t.Errorf("%d is not inserted", v)
}
VerifyRBTreeProperties(s, t)
if s.Size() != i + 1 {
t.Errorf("Expected size %d, actual %d", i + 1, s.Size())
}
if !s.Contains(v) {
t.Errorf("Contains %d returned false", v)
}
if s.Insert(v) {
t.Errorf("%d inserted second time", v)
}
if s.Size() != i + 1 {
t.Errorf("Expected size %d, actual %d", i + 1, s.Size())
}
verifyElements(s, a[0:i+1], t)
}
for i,v := range a {
s.Remove(v)
VerifyRBTreeProperties(s, t)
if s.Size() != len(a) - (i + 1) {
t.Errorf("Expected size %d, actual %d", len(a) - (i + 1), s.Size())
}
if s.Contains(v) {
t.Errorf("Contains %d returned true", v)
}
verifyElements(s, a[i + 1:], t)
}
}
/**
From Introduction to Algorithms, 2/e
Red-Black Tree has the following properties
1. Every Node is either red or black
2. The root is black
3. Every leaf (NIL) is black
4. If a node is red, then both its children are black
5. For each node, all paths from the node to descendantat leaves
contain the same number of black nodes
*/
func VerifyRBTreeProperties(tree *RBTree, t *testing.T) {
if tree.root == NIL {
return
}
if tree.root.color != BLACK {
t.Errorf("(1) root color is not BLACK")
}
leafs := new(vector.Vector)
inorderTreeWalk(tree.root, func(n *Node) {
if n.Leaf() {
leafs.Push(n);
}
});
var numBlacks int = -1
leafs.Do(func(i interface{}) {
n := i.(*Node)
if n.Left != NIL { // NIL is always black
t.Errorf("left of leaf is not NIL")
}
if n.Right != NIL { // NIL is always black
t.Errorf("right of leaf is not NIL")
}
var blacks int = 0
for n != tree.root {
if n.color == BLACK {
blacks += 1
} else {
if n.Parent.color == RED { // property 4 failed
t.Errorf("(4) two consecutive RED nodes %d %d", n.Value.(int), n.Parent.Value.(int))
}
}
n = n.Parent
}
if numBlacks == -1 {
numBlacks = blacks
} else {
if numBlacks != blacks {
t.Errorf("(5) number of blacks differ. %d vs. %d", numBlacks, blacks)
}
}
});
}
func TestTree(t *testing.T) {
tree := MakeIntTree()
VerifyRBTreeProperties(tree, t)
tree.Foreach(func(elem interface{}) { fmt.Printf("%d, ", elem.(int))})
fmt.Printf("\n")
min := tree.First().(int)
if min != 0 {
t.Errorf("Expected 0 actual %d", min)
}
max := tree.Last().(int)
if max != 20 {
t.Errorf("Expected 20 actual %d", max)
}
size := tree.Size()
if size != 8 {
t.Errorf("Size(). Expected 8 actual %d", size)
}
has_3 := tree.Contains(3)
if !has_3 {
t.Errorf("Contains(3) returned false")
}
tree.Remove(3)
has_3 = tree.Contains(3)
if has_3 {
t.Errorf("Contains(3) returned true")
}
}
func TestEquals(t *testing.T) {
a := rand.Perm(1000)
tree1 := NewIntTree()
tree2 := NewIntTree()
for _,i := range a {
tree1.Insert(i)
tree2.Insert(i)
}
if !tree1.equals(tree2) {
t.Errorf("equals returned false")
}
tree2.Remove(tree2.Last())
if tree1.equals(tree2) {
t.Errorf("equals returned true")
}
}
func TestRandomTree(t *testing.T) {
a := rand.Perm(1000)
verifyTreeWithRandomData(a, t)
}