forked from GabrielMndz/DataStructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree - AVLTree.cs
628 lines (535 loc) · 19.4 KB
/
BinarySearchTree - AVLTree.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AVLTree
{
class DuplicateKeyException : Exception { }
class InvalidRotation : Exception { }
class NotSuccessorFound : Exception { }
class NotAVLTree
{
/*************by:
Gabriel Mendez Reyes -***************/
class TreeNode : IComparable
{
StringBuilder sb = new StringBuilder();
public int key;
public TreeNode left, right, parent;
public int height, depth, heightBalanceFactor; // NOTA: puedes agregar estos atributos si lo deseas
public TreeNode(int key)
{
this.key = key;
}
public int CompareTo(object obj)
{
TreeNode that = (TreeNode)obj;
return that.depth.CompareTo(this.depth);
}
// Convertir el subtree cuya raiz es este nodo a un string (para
// verificar que su codigo de conversion funciona)
override public string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append('(');
sb.Append(key);
if (left != null || right != null)
{
sb.Append('-');
sb.Append(left == null ? "()" : left.ToString());
sb.Append('-');
sb.Append(right == null ? "()" : right.ToString());
}
sb.Append(')');
return sb.ToString();
}
}
private TreeNode root;
public int size { get; private set; } // Cantidad de keys grabados en el arbol
private int getHeight(TreeNode x) // Complexity: O(1)
{
if (x == null)
return -1;
else
return x.height;
}
/**
* Agrega key al arbol
**/
public void Add(int key)
{
if (size == 0)
{
root = new TreeNode(key);
size = 1;
return;
}
TreeNode cur = root, prev = null;
while (cur != null)
{
if (key == cur.key)
throw new DuplicateKeyException();
prev = cur;
if (key < cur.key)
cur = cur.left;
else
cur = cur.right;
}
TreeNode newNode = new TreeNode(key);
newNode.parent = prev;
if (key < prev.key)
prev.left = newNode;
else
prev.right = newNode;
++size;
}
/**
* Busca y devuelve el nodo que contiene key; devuelve null si no se
* encuentra
**/
private TreeNode FindNode(int key)
{
TreeNode cur = root;
while (cur != null)
{
if (key == cur.key)
return cur;
else if (key < cur.key)
cur = cur.left;
else
cur = cur.right;
}
return null;
}
/**
* Devuelve true si key existe en el arbol
**/
public bool Exists(int key)
{
TreeNode x = FindNode(key);
return x != null;
}
/**
* Borra key del arbol; devuelve false si el key NO se encuentra en el
* arbol
**/
public bool Remove(int key)
{
// TODO: Algoritmo implementado:
// 1) Buscar el nodo X que contiene key
// 2) Si X es un leaf o tiene un solo hijo, borralo como fue
// descrito para Hibbard's Delete
// 3) Si X tiene 2 hijos
// 3a) Ubica el sucesor de X, llamemoslo Y
// 3b) Rota el nodo Y hasta que se convierta en padre de X
// 3c) En este punto X no tendra hijo derecho y lo puedes
// borrar como en el caso de que tiene un solo hijo
if (!Exists(key))
return false;
TreeNode myNode = FindNode(key);
if (myNode == null)
throw new KeyNotFoundException();
bool remove = RemoveNode(myNode);
size--;
return remove;
}
private bool RemoveNode( TreeNode myNode )
{
TreeNode myParent = myNode.parent;
if (myNode.right == null && myNode.left == null)
{
// no hijo
if (myParent == null)
{
root = null;
}
else if (myNode == myParent.left)
myParent.left = null;
else
myParent.right = null;
}
if (myNode.left != null && myNode.right == null)
{
// tiene 1 hijo (izquierdo)
TreeNode leftChild = myNode.left;
if (myParent == null)
{
root = leftChild;
}
else if (myNode == myParent.left)
{
myParent.left = leftChild;
}
else
{
myParent.right = leftChild;
}
leftChild.parent = myParent;
}
if (myNode.left == null && myNode.right != null)
{
// tiene 1 hijo (derecho)
TreeNode rightChild = myNode.right;
if (myParent == null)
{
root = rightChild;
}
else if (myNode == myParent.left)
{
myParent.left = rightChild;
}
else
{
myParent.right = rightChild;
}
rightChild.parent = myParent;
}
if (myNode.left != null && myNode.right != null)
{
// tiene 2 hijos
TreeNode Y = Successor(myNode.key);
TreeNode myParentY = Y.parent;
while (myNode.parent != Y)
{
if(myParentY.left == Y)
{
RotateRight(myParentY);
}else
RotateLeft(myParentY);
myParentY = Y.parent;
}
RemoveNode(myNode);
}
return true;
}
private TreeNode Successor(int key)
{
TreeNode cur = root, best = null;
while (cur != null)
{
if (key == cur.key)
cur = cur.right;
else if (key < cur.key)
{
if (best == null || best.key > cur.key)
best = cur;
cur = cur.left;
}
else
cur = cur.right;
}
if (best == null) // no hay sucesor
throw new NotSuccessorFound();
return best;
}
/**
* rotateLeft(P) rota el nodo P hacia la izquierda.
**/
private void RotateLeft(TreeNode P) // O(1)
{
// NOTA: Puedes alterar este metodo
if (P == null || P.right == null)
throw new InvalidRotation();
TreeNode par = P.parent;
TreeNode Q = P.right;
TreeNode B = Q.left;
if (B != null)
B.parent = P;
P.right = B;
P.parent = Q;
Q.left = P;
Q.parent = par;
if (par != null)
{
if (P == par.left)
par.left = Q;
else
par.right = Q;
}
else
root = Q;
P.height = HeightNode(P);
if (B != null)
{
B.height = HeightNode(B);
}
if (Q != null)
{
Q.height = HeightNode(Q);
}
if (par != null)
{
par.height = HeightNode(par);
}
}
/***
* rotateRight(Q) rota el nodo Q hacia la derecha.
**/
private void RotateRight(TreeNode Q) // O(1)
{
// NOTA: Puedes alterar este metodo
if (Q == null|| Q.left == null)
throw new InvalidRotation();
TreeNode par = Q.parent;
TreeNode P = Q.left;
TreeNode B = P.right;
if (B != null)
B.parent = Q;
Q.left = B;
Q.parent = P;
P.right = Q;
P.parent = par;
if (par != null)
{
if (Q == par.left)
par.left = P;
else
par.right = P;
}
else
root = P;
if (B != null)
{
B.height = HeightNode(B);
}
Q.height = HeightNode(Q);
if (P != null)
{
P.height = HeightNode(P);
}
if (par != null)
{
par.height = HeightNode(par);
}
}
/***
Transformar el Binary Search Tree a AVL Tree
**/
private bool FixAVLPropertyOfNode(TreeNode x) // O(1)
{
// TODO: FixAVLProperty en el nodo x.
// NOTA: el algoritmo descrito NO funciona si el
// height balance factor del nodo x es < -2 o > +2, en cuyo caso
// este metodo simplemente devuelve false; de lo contrario,
// corrigelo y devuelve true
if (x.heightBalanceFactor < -2 || x.heightBalanceFactor > 2)
return false;
if (x.heightBalanceFactor == 2)
{
if (x.right.heightBalanceFactor == -1) // codo
{
RotateRight(x.right);
}
RotateLeft(x);
}
if (x.heightBalanceFactor == -2)
{
if (x.left.heightBalanceFactor == 1) // codo
{
RotateLeft(x.left);
}
RotateRight(x);
}
return true;
}
/**
* Transformar el Binary Search Tree a AVL Tree
**/
public bool TransformToAVLTree()
{
// TODO: Algoritmo implementado:
// 1) Computa el depth de cada nodo en el arbol
// 2) Ordena los nodos por su depth
// Puedes consultar en Internet sobre como ordenar datos en C#
// 3) Itera los nodos de mayor a menor depth
// 3a) Calcula el height y balance factor de cada nodo
// 3b) Si un nodo requiere correccion, llama al metodo
// FixAVLPropertyOfNode
// Si en alguno punto no fue posible corregirse, devuelve false
List<TreeNode> myList = InOrderIteration(); // Complexity: O(N)
bool SePudoArreglar = true;
foreach (var cur in myList) // Complexity: O(N*H)
{
cur.depth = depth(cur.key); // 1) Computa el depth de cada nodo en el arbol
}
myList.Sort(); // Complexity: O(N * LogN) //Sort myList por el depth <- : IComparable : // 2) Ordena los nodos por su depth
for (int i = 0; i<myList.Count; i++) // Complexity: O(N) // 3) Itera los nodos de mayor a menor depth
{
TreeNode temp = myList[i];
temp.height = HeightNode(temp); //--------------------//----> 3a) Calcula el height y balance factor de cada nodo
temp.heightBalanceFactor = HeightBalanceFactor(temp); //
bool didFixed = FixAVLPropertyOfNode(temp);//----------> 3b) Si un nodo requiere correccion, llama al metodo
// FixAVLPropertyOfNode
if (!didFixed)
SePudoArreglar = false;//------------->Si en algun punto no fue posible corregirse, devuelve false
//if didFixes = false, significa que el heightBalanceFactor es menor que -2 o mayor que 2
// Se convierte false y se sigue iterando cada nodo de mayor a menor profundidad,
// hasta que no quede nodo por iterar y al final return el value que quedara en "SePudoArreglar"
// PD: Por lo tanto si se da el caso que no se pueda corregir por violentar la regla,
//----> el metodo "TransformToAVLTree" hara un return false.
if (temp.left != null)
{
temp.left.heightBalanceFactor = HeightBalanceFactor(temp.left);
}
if (temp.right != null)
{
temp.right.heightBalanceFactor = HeightBalanceFactor(temp.right);
}
}
return SePudoArreglar;
//Complexity --> O(N*H + N*LogN) */
}
private List<TreeNode> InOrderIteration() // Complexity: O(N)
{
List<TreeNode> myList = new List<TreeNode>();
RecInOrderIteration(myList, root);
return myList;
}
private void RecInOrderIteration(List<TreeNode> L, TreeNode root) // Complexity: O(N)
{
if (root == null)
return;
RecInOrderIteration(L, root.left);
L.Add(root);
RecInOrderIteration(L, root.right);
}
private int depth(int key) // Complexity: O(H)
{
int contador = 0;
TreeNode cur = root;
while (cur != null)
{
if (key == cur.key)
{
break;
}
if (key < cur.key)
{
cur = cur.left;
contador++;
}
if (key > cur.key)
{
cur = cur.right;
contador++;
}
}
return contador;
}
private void RecDepth (TreeNode cur) // Complexity: O(N)
{
if (cur == null)
return;
if (cur.parent != null)
cur.depth = cur.parent.depth + 1;
RecDepth(cur.right);
RecDepth(cur.left);
}
private int RecHeight(TreeNode cur) // Complexity: O(N)
{
if (cur == null)
return -1;
int HeightRight = RecHeight(cur.right);
int HeightLeft = RecHeight(cur.left);
cur.height = Math.Max(RecHeight(cur.right), RecHeight(cur.left)) + 1;
return cur.height;
}
private int HeightNode(TreeNode x) // Complexity: O(1)
{
return Math.Max( getHeight(x.left) , getHeight(x.right) ) + 1;
}
private int HeightBalanceFactor(TreeNode x) // Complexity: O(1)
{
return getHeight(x.right) - getHeight(x.left);
}
/**
* Convertir el arbol a un string (para debugging)
**/
override public string ToString()
{
if (root == null) return "";
return root.ToString();
}
}
class Program
{
static void Main(string[] args)
{
NotAVLTree T = new NotAVLTree();
T.Add(90);
T.Add(100);
T.Add(20);
T.Add(10);
T.Add(70);
T.Add(30);
T.Add(40);
T.Add(50);
T.Add(80);
Console.WriteLine(T);
// (90-(20-(10)-(70-(30-()-(40-()-(50)))-(80)))-(100))
T.TransformToAVLTree();
Console.WriteLine(T);
// (40-(20-(10)-(30))-(90-(70-(50)-(80))-(100)))
// NOTA: No estoy seguro si este output esta correcto :-P
T.Remove(40);
Console.WriteLine(T.Exists(40)); // False
Console.WriteLine(T);
// (50-(20-(10)-(30))-(90-(70-()-(80))-(100)))
Console.ReadKey();
/**************************************/
/* // <- QUITAR /* PARA VER OTRA PRUEBA CON OTRO BST.
T.Add(80);
T.Add(10);
T.Add(50);
T.Add(30);
T.Add(15);
T.Add(5);
T.Add(70);
T.Add(100);
T.Add(120);
T.Add(42);
T.Add(13);
T.Add(500);
T.Add(9);
Console.WriteLine(T);
//(80-(10-(5-()-(9))-(50-(30-(15-(13)-())-(42))-(70)))-(100-()-(120-()-(500))))
T.TransformToAVLTree();
Console.WriteLine(T);
//(30-(10-(5-()-(9))-(15-(13)-()))-(80-(50-(42)-(70))-(120-(100)-(500))))
T.Remove(9);
Console.WriteLine(T);
//(30-(10-(5)-(15-(13)-()))-(80-(50-(42)-(70))-(120-(100)-(500))))
T.Remove(10);
Console.WriteLine(T);
//(30-(13-(5)-(15))-(80-(50-(42)-(70))-(120-(100)-(500))))
T.Remove(30);
Console.WriteLine(T);
//(42-(13-(5)-(15))-(80-(50-()-(70))-(120-(100)-(500))))
Console.WriteLine(T.Remove(8));
/**************************************/
/**************************************/
/* // <- QUITAR /* PARA VER OTRA PRUEBA CON OTRO BST.
T.Add(100);
T.Add(10);
T.Add(1);
T.Add(50);
T.Add(80);
T.Add(25);
T.Add(150);
T.Add(130);
T.Add(200);
T.Add(20);
T.Add(15);
T.Add(75);
Console.WriteLine(T);
//(100 - (10 - (1) - (50 - (25 - (20 - (15) - ()) - ()) - (80 - (75) - ()))) - (150 - (130) - (200)))
T.TransformToAVLTree();
Console.WriteLine(T);
//(50 - (10 - (1) - (20 - (15) - (25))) - (100 - (80 - (75) - ()) - (150 - (130) - (200))))
T.Remove(100);
Console.WriteLine(T);
//(50 - (10 - (1) - (20 - (15) - (25))) - (130 - (80 - (75) - ()) - (150 - () - (200)))) */
}
}
}