-
Notifications
You must be signed in to change notification settings - Fork 1
/
AVL.cs
592 lines (527 loc) · 13 KB
/
AVL.cs
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class AVL<T> : ICollection<T> where T : IComparable<T>
{
private Node<T> _root;
public AVL()
{
}
public AVL(ICollection<T> collection)
{
AddAll(collection);
}
public static AVL<T> CopyOf(ICollection<T> collection)
{
return new AVL<T>(collection);
}
public T this[int i]
{
get
{
return _getIndexFromLeft(_root, i).value;
}
private set
{
}
}
public int Count
{
get
{
if (_root == null)
{
return 0;
}
else
{
return _root.weight;
}
}
private set
{
}
}
public void Clear()
{
_root = null;
}
public bool Contains(T item)
{
return Find(item) != -1;
}
public void CopyTo(T[] array, int arrayIndex)
{
if (Count - arrayIndex > 0)
{
array = new T[Count - arrayIndex];
for (int i = arrayIndex; i < Count; i++)
{
array[i] = this[i];
}
}
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public bool Remove(T item)
{
int idx = Find(item);
if (idx >= 0)
{
Pop(idx);
return true;
}
else
{
return false;
}
}
public T Pop(int idx)
{
Node<T> node = _getIndexFromLeft(_root, idx);
_remove(node);
return node.value;
}
public IEnumerator<T> GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
public void Add(T arg)
{
Node<T> newNode = new Node<T>(arg);
if (_root == null)
{
_root = newNode;
newNode.parent = null;
newNode.height = 0;
newNode.balance = 0;
}
else
{
_insertNode(_root, newNode);
}
}
public void AddAll(ICollection<T> collection)
{
foreach (T obj in collection)
{
Add(obj);
}
}
private void _insertNode(Node<T> current, Node<T> newNode)
{
if (current.CompareTo(newNode) > 0)
{
//Go Left
if (current.left == null)
{
current.left = newNode;
newNode.parent = current;
_updateParents(current);
_balanceTree(current);
}
else
{
_insertNode(current.left, newNode);
}
}
else if (current.CompareTo(newNode) < 0)
{
// Go Right
if (current.right == null)
{
current.right = newNode;
newNode.parent = current;
_updateParents(current);
_balanceTree(current);
}
else
{
_insertNode(current.right, newNode);
}
}
}
public void Push(T arg)
{
Add(arg);
}
public int Find(T arg)
{
int result = _getIndex(_root, arg, 0);
return result;
}
public T Pop()
{
Node<T> node = _getIndexFromLeft(_root, 0);
_remove(node);
return node.value;
}
private void _remove(Node<T> node)
{
Node<T> current = node;
Node<T> parent = node.parent;
Node<T> right = node.right;
Node<T> left = node.left;
if (left == null)
{
if (parent == null)
{
// root
_root = right;
if (right != null)
{
right.parent = null;
_balanceTree(right);
}
}
else
{
if (parent.left != null && parent.left.Equals(node))
{
parent.left = right;
}
else if (parent.right != null && parent.right.Equals(node))
{
parent.right = right;
}
if (right != null)
{
right.parent = parent;
}
node = null;
_updateParents(parent);
_balanceTree(parent);
}
}
else if (right == null)
{
if (parent == null)
{
// root
_root = left;
if (left != null)
{
left.parent = null;
_balanceTree(left);
}
}
else
{
if (parent.left != null && parent.left.Equals(node))
{
parent.left = left;
}
else if (parent.right != null && parent.right.Equals(node))
{
parent.right = left;
}
if (left != null)
{
left.parent = parent;
}
node = null;
_updateParents(parent);
_balanceTree(parent);
}
}
else
{
// Both sides are not null
Node<T> successor = _getNext(node);
node.value = successor.value;
_remove(successor);
}
}
private Node<T> _getNext(Node<T> node)
{
if (node.right == null)
{
return null;
}
else
{
node = node.right;
while (node.left != null)
{
node = node.left;
}
return node;
}
}
private Node<T> _getPrevious(Node<T> node)
{
if (node.left == null)
{
return null;
}
else
{
node = node.left;
while (node.right != null)
{
node = node.right;
}
return node;
}
}
private int _getIndex(Node<T> current, T val, int index)
{
if (current.value.CompareTo(val) > 0)
{
if (current.left == null)
{
return -1;
}
else
{
return _getIndex(current.left, val, index);
}
}
else if (current.value.CompareTo(val) < 0)
{
if (current.right == null)
{
return -1;
}
else
{
int leftWeight = 0;
if (current.left != null)
{
leftWeight = current.left.weight;
}
return _getIndex(current.right, val, index + leftWeight + 1);
}
}
else
{
return index + ((current.left == null) ? 0 : current.left.weight);
}
}
private Node<T> _getIndexFromLeft(Node<T> node, int idx)
{
if (idx < 0)
{
return null;
}
if (node.left == null)
{
if (idx == 0)
{
return node;
}
else
{
return _getIndexFromLeft(node.right, idx - 1);
}
}
else
{
if (node.left.weight > idx)
{
return _getIndexFromLeft(node.left, idx);
}
else
{
int balance = idx - node.left.weight;
if (balance == 0)
{
return node;
}
else
{
return _getIndexFromLeft(node.right, balance - 1);
}
}
}
}
private void _balanceTree(Node<T> node)
{
if (node.balance > 1)
{
// Left is too tall
_rotateTreeRight(node);
}
else if (node.balance < -1)
{
// Right is too tall
_rotateTreeLeft(node);
}
if (node.parent != null)
{
_balanceTree(node.parent);
}
}
private void _rotateTreeRight(Node<T> node)
{
//Check if left node is right heavy first
//Left must not be empty
Node<T> checkNode = node.left;
if (checkNode.balance < 0)
{
_rotateLeft(checkNode);
}
_rotateRight(node);
}
private void _rotateTreeLeft(Node<T> node)
{
//check if right node is left heavy first
//Right must not be empty
Node<T> checkNode = node.right;
if (checkNode.balance > 0)
{
_rotateRight(checkNode);
}
_rotateLeft(node);
}
private void _rotateLeft(Node<T> node)
{
if (node.right != null)
{
Node<T> newNode = node.right;
Node<T> parent = node.parent;
if (parent != null)
{
//Parent-node relations
if (parent.left != null && parent.left == node)
{
parent.left = newNode;
}
else if (parent.right != null && parent.right == node)
{
parent.right = newNode;
}
newNode.parent = parent;
}
else
{
newNode.parent = null;
_root = newNode;
}
//shift-node relations
if (newNode.left != null)
{
newNode.left.parent = node;
node.right = newNode.left;
}
else
{
node.right = null;
}
//node-old node relations
node.parent = newNode;
newNode.left = node;
//update weights and heights
_updateParents(node);
}
}
private void _rotateRight(Node<T> node)
{
if (node.left != null)
{
Node<T> newNode = node.left;
Node<T> parent = node.parent;
if (parent != null)
{
//Parent-node relations
if (parent.left != null && parent.left == node)
{
parent.left = newNode;
}
else if (parent.right != null && parent.right == node)
{
parent.right = newNode;
}
newNode.parent = parent;
}
else
{
newNode.parent = null;
_root = newNode;
}
//shift-node relations
if (newNode.right != null)
{
newNode.right.parent = node;
node.left = newNode.right;
}
else
{
node.left = null;
}
//node-old node relations
node.parent = newNode;
newNode.right = node;
//update weights and heights
_updateParents(node);
}
}
private void _updateParents(Node<T> node)
{
int leftHeight = -1;
int rightHeight = -1;
int leftWeight = 0;
int rightWeight = 0;
if (node.left != null)
{
leftHeight = node.left.height;
leftWeight = node.left.weight;
}
if (node.right != null)
{
rightHeight = node.right.height;
rightWeight = node.right.weight;
}
node.weight = rightWeight + leftWeight + 1;
node.height = Mathf.Max(leftHeight, rightHeight) + 1;
node.balance = leftHeight - rightHeight;
if (node.parent != null)
{
_updateParents(node.parent);
}
}
private class Node<T> : IComparable<Node<T>> where T : IComparable<T>
{
public Node<T> parent;
public Node<T> left;
public Node<T> right;
public int height;
public int balance;
public T value;
public int weight;
public Node()
{
}
public Node(T arg)
{
value = arg;
height = 0;
balance = 0;
weight = 1;
}
public int CompareTo(Node<T> other)
{
return value.CompareTo(other.value);
}
public override bool Equals(object obj)
{
Node<T> node = obj as Node<T>;
if (object.ReferenceEquals(node, null))
{
return false;
}
return this.CompareTo(node) == 0;
}
public bool Equals(Node<T> other)
{
return value.CompareTo(other.value) == 0;
}
}
}