-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.go
324 lines (254 loc) · 5.33 KB
/
tree.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Package AVLTree Simple AVL Tree implementation
//
package AVLTree
import (
"fmt"
"strconv"
)
// Tree Structure
type Tree struct {
root *Node
}
// Exists to see if this is in the tree or not.
func (tree Tree) Exists(value int) bool {
if tree.root == nil || tree.root.Find(value) == nil {
return false
}
return true
}
// Insert an element in the tree
func (tree *Tree) Insert(value int) {
tree.root, _ = tree.root.Insert(value)
}
// Delete return true if successful
func (tree *Tree) Delete(value int) bool {
var change int
if tree.root == nil {
return false
}
tree.root, change = tree.root.Delete(value)
if change == 0 {
return false
}
return true
}
// Update the key with a new one
func (tree *Tree) Update(orig int, value int) {
tree.Delete(orig)
tree.Insert(value)
}
// Print the tree
func (tree Tree) Print() {
if tree.root == nil {
fmt.Printf("--- AVL Tree:\n EMPTY\n")
return
}
fmt.Printf("--- AVL Tree:\n")
tree.root.Print(1)
}
/*
AVL Tree Nodes
*/
// Node in the tree
type Node struct {
value int
balance int
left *Node
right *Node
}
// Compare two values,
func (node Node) Compare(value int) int {
return node.value - value
}
// Find a value in the tree, searching
func (node *Node) Find(value int) *Node {
if node == nil {
return nil
}
if node.Compare(value) == 0 {
return node
}
var result *Node
if node.left != nil {
result = node.left.Find(value)
}
if result == nil && node.right != nil {
result = node.right.Find(value)
}
return result
}
// RotateLeft the tree
func (node *Node) RotateLeft() *Node {
if node == nil {
return nil
}
if node.right == nil {
return node
}
var result = node.right
node.right = result.left
result.left = node
var leftBalance = node.balance
var balance = result.balance
node.balance = leftBalance - 1 - max(balance, 0)
result.balance = min(leftBalance-2, balance+leftBalance-2, balance-1)
return result
}
// RotateRight the tree
func (node *Node) RotateRight() *Node {
if node == nil {
return nil
}
if node.left == nil {
return node
}
var result = node.left
node.left = result.right
result.right = node
var rightBalance = node.balance
var balance = result.balance
node.balance = rightBalance + 1 - min(balance, 0)
result.balance = max(rightBalance+2, balance+rightBalance+2, balance+1)
return result
}
// Insert a node
func (node *Node) Insert(value int) (*Node, int) {
// Terminal Condition, create this node
if node == nil {
return &Node{value: value, balance: 0}, 1
}
var change int
// Descend to the children
diff := node.Compare(value)
switch {
case diff == 0:
// Ignore duplicates
case diff > 0:
node.left, change = node.left.Insert(value)
change *= -1
case diff < 0:
node.right, change = node.right.Insert(value)
}
node.balance += change
// Rebalance at the parents or grandparents
var insert int
if node.balance != 0 && change != 0 {
switch {
case node.balance < -1:
if node.left.balance >= 0 {
node.left = node.left.RotateLeft()
}
node = node.RotateRight()
insert = 0
case node.balance > 1:
if node.right.balance <= 0 {
node.right = node.right.RotateRight()
}
node = node.RotateLeft()
insert = 0
default:
insert = 1
}
} else if change != 0 {
insert = 0
}
return node, insert
}
// Delete a node
func (node *Node) Delete(value int) (*Node, int) {
var change int
if node == nil {
return nil, change
}
diff := node.Compare(value)
switch {
case diff > 0:
node.left, change = node.left.Delete(value)
case diff < 0:
node.right, change = node.right.Delete(value)
change *= -1
case diff == 0:
switch {
case node.left == nil:
return node.right, 1
case node.right == nil:
return node.left, 1
default:
// Pick the heavier of the two...
if -1*node.left.balance < node.right.balance {
node = node.RotateLeft()
node.left, change = node.left.Delete(value)
} else {
node = node.RotateRight()
node.right, change = node.right.Delete(value)
change *= -1
}
}
}
// Update the balance
if change != 0 {
if node.balance != change {
node.balance += change
}
switch {
case node.balance < -1:
if node.left.balance >= 0 {
node.left = node.left.RotateLeft()
}
node = node.RotateRight()
case node.balance > 1:
if node.right.balance <= 0 {
node.right = node.right.RotateRight()
}
node = node.RotateLeft()
}
}
return node, change
}
// Print the current nodes, rotated 90 degrees, in-order traversal.
func (node Node) Print(depth int) {
if node.right != nil {
node.right.Print(depth + 1)
}
padding := padding(depth)
fmt.Printf("%s[%v] Value: %v\n", padding, symbols(node.balance), node.value)
if node.left != nil {
node.left.Print(depth + 1)
}
}
// Slightly more decorative: shift 1/-1 -> +/-
func symbols(balance int) string {
switch {
case balance == 1:
return "+"
case balance == -1:
return "-"
}
return strconv.Itoa(balance)
}
// Convert depth into spaces, 4 per
func padding(size int) string {
result := ""
for i := 0; i < size; i++ {
result += " "
}
return result
}
func max(values ...int) int {
var total = values[0]
for _, value := range values {
if value > total {
total = value
}
}
return total
}
func min(values ...int) int {
var total = values[0]
for _, value := range values {
if value < total {
total = value
}
}
return total
}