-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_avl_tree.rb
195 lines (169 loc) · 3.77 KB
/
test_avl_tree.rb
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
##
# AvlTree Test
assert("AvlTree#insert") do
tree = AvlTree.new
tree.insert 'C', 1
1 == tree.root.value
end
assert("AvlTree right insertion") do
tree = AvlTree.new
tree.insert 'A', 1
tree.insert 'B', 2
'B' == tree.right.key
end
assert("AvlTree left insertion") do
tree = AvlTree.new
tree.insert 'B', 2
tree.insert 'A', 1
'A' == tree.left.key
end
assert('AvlTree update with insert') do
tree = AvlTree.new
tree.insert 'A', 1
tree.insert 'A', 2
2 == tree['A']
end
assert('AvlTree update with hash like access') do
tree = AvlTree.new
tree.insert 'A', 1
tree['A'] = 2
2 == tree['A']
end
assert('AvlTree hash like access') do
tree = AvlTree.new
tree.insert 'A', 1
tree.insert 'B', 2
tree.insert 'C', 3
3 == tree['C']
2 == tree['B']
1 == tree['A']
end
assert('AvlTree not found') do
tree = AvlTree.new
nil == tree['D']
end
assert('AvlTree rotate left') do
tree = AvlTree.new
tree.insert 'C', 1
tree.insert 'D', 2
tree.insert 'E', 3
'C' == tree.left.key
'E' == tree.right.key
end
assert('AvlTree rotate right') do
tree = AvlTree.new
tree.insert 'E', 1
tree.insert 'D', 2
tree.insert 'C', 3
'C' == tree.left.key
'E' == tree.right.key
end
assert('AvlTree double rotate right left') do
tree = AvlTree.new
tree.insert 5, 1
tree.insert 9, 1
tree.insert 7, 1
7 == tree.root.key
end
assert('AvlTree double rotate left right') do
tree = AvlTree.new
tree.insert 9, 1
tree.insert 5, 1
tree.insert 7, 1
7 == tree.root.key
end
assert('AvlTree multi generation right side build') do
tree = AvlTree.new
tree.insert 'C', nil
tree.insert 'D', nil
tree.insert 'E', nil
tree.insert 'F', nil
tree.insert 'G', nil
tree.insert 'H', nil
tree.insert 'I', nil
'D' == tree.left.key
'C' == tree.left.left.key
'E' == tree.left.right.key
'H' == tree.right.key
'G' == tree.right.left.key
'I' == tree.right.right.key
end
assert('AvlTree multi generation left side build') do
tree = AvlTree.new
tree.insert 'G', nil
tree.insert 'F', nil
tree.insert 'E', nil
tree.insert 'D', nil
tree.insert 'C', nil
tree.insert 'B', nil
tree.insert 'A', nil
'B' == tree.left.key
'A' == tree.left.left.key
'C' == tree.left.right.key
'F' == tree.right.key
'E' == tree.right.left.key
'G' == tree.right.right.key
end
assert('AvlTree delete root') do
tree = AvlTree.new
tree.insert 'A', 1
tree.delete 'A'
true == tree.root.is_a?(AvlTree::Node::TerminalNode)
end
assert('AvlTree delete barren child') do
tree = AvlTree.new
tree.insert 'A', 1
tree.insert 'B', 2
tree.delete 'B'
true == tree.right.is_a?(AvlTree::Node::TerminalNode)
end
assert('AvlTree delete with single child') do
tree = AvlTree.new
tree.insert 'A', 1
tree.insert 'B', 2
tree.delete 'A'
'B' == tree.root.key
end
assert('AvlTree delete barren child with rebalance') do
tree = AvlTree.new
tree.insert 'D', nil
tree.insert 'C', nil
tree.insert 'A', nil
tree.insert 'B', nil
tree.insert 'E', nil
tree.insert 'F', nil
tree.insert 'G', nil
tree.delete 'B'
'E' == tree.root.key
'C' == tree.left.key
'F' == tree.right.key
'D' == tree.left.right.key
'A' == tree.left.left.key
'G' == tree.right.right.key
end
assert('AvlTree delete using in order successor') do
tree = AvlTree.new
%w(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20).each do |item|
tree.insert item, nil
end
tree.delete '14'
'17' == tree.root.key
'11' == tree.left.key
'15' == tree.left.right.key
end
assert('AvlTree delete using in order predecessor') do
tree = AvlTree.new
(1..10).to_a.each do |item|
tree.insert item, nil
end
tree.delete 8
7 == tree.right.key
end
assert('AvlTree delete not found') do
tree = AvlTree.new
tree.insert 1, 1
tree.insert 2, 2
tree.insert 3, 3
tree.delete 'a'
2 == tree.root.key
end