-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvlTree.cs
1602 lines (1383 loc) · 54.6 KB
/
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////
//// File Name : AvlTree.cs
//// Created : 10 8 2012 22:30
//// Author : Costin S
////
/////////////////////////////////////////////////////////////////////
////---------------------------------------
//// TREE_WITH_PARENT_POINTERS:
//// Defines whether or not each node in the tree maintains a reference to its parent node.
//// Only parent pointers traversal is implemented in the code below.
//// To disable uncomment the following line
#define TREE_WITH_PARENT_POINTERS
////---------------------------------------
//// TREE_WITH_CONCAT_AND_SPLIT_OPERATIONS:
//// Defines whether the tree exposes and implements concatenate and split operations.
////
//// When concat and split operations are defined, the code defines stored both the Balance and the Height in each node which is obviously not necessary.
//// To reduce space, you can do one of two things:
//// 1. The simplest change is to store both Balance and Height in one integer. Balance field needs only 2 bits which lefts 30 bits for the Height field. A tree with a HEIGHT > 2^30 (2 to the power of 30) is very unlikely you will ever build.
//// 2. Simple enough to modify and remove Balance field. Concat and Split were added as an afterthought after the implementation was already done using a Balance field.
#define TREE_WITH_CONCAT_AND_SPLIT_OPERATIONS
namespace SelfBalancedTree
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
/// <summary>
/// Dictionary class.
/// </summary>
/// <typeparam name="T">The type of the data stored in the nodes</typeparam>
public class AVLTree<T>
{
#region Fields
private Node<T> Root;
private IComparer<T> comparer;
#endregion
#region Ctor
/// <summary>
/// Initializes a new instance of the <see cref="AVLTree<T>"/> class.
/// </summary>
public AVLTree()
{
this.comparer = GetComparer();
}
/// <summary>
/// Initializes a new instance of the <see cref="AVLTree<T>"/> class.
/// </summary>
/// <param name="elems">The elements to be added to the tree.</param>
public AVLTree(IEnumerable<T> elems, IComparer<T> comparer)
{
this.comparer = comparer;
if (elems != null)
{
foreach (var elem in elems)
{
this.Add(elem);
}
}
}
#endregion
#region Delegates
/// <summary>
/// the visitor delegate
/// </summary>
/// <typeparam name="TNode">The type of the node.</typeparam>
/// <param name="node">The node.</param>
/// <param name="level">The level.</param>
private delegate void VisitNodeHandler<TNode>(TNode node, int level);
#endregion
#region Enums
public enum SplitOperationMode
{
IncludeSplitValueToLeftSubtree,
IncludeSplitValueToRightSubtree,
DoNotIncludeSplitValue
}
#endregion
#region Properties
#if TREE_WITH_PARENT_POINTERS
/// <summary>
/// Gets the collection of values in ascending order.
/// Complexity: O(N)
/// </summary>
public IEnumerable<T> ValuesCollection
{
get
{
if (this.Root == null)
{
yield break;
}
var p = FindMin(this.Root);
while (p != null)
{
yield return p.Data;
p = Successor(p);
}
}
}
/// <summary>
/// Gets the collection of values in reverse/descending order.
/// Complexity: O(N)
/// </summary>
public IEnumerable<T> ValuesCollectionDescending
{
get
{
if (this.Root == null)
{
yield break;
}
var p = FindMax(this.Root);
while (p != null)
{
yield return p.Data;
p = Predecesor(p);
}
}
}
#endif
#endregion
#region Public Methods
/// <summary>
/// Adds the specified value argument.
/// Complexity: O(log(N))
/// </summary>
/// <param name="arg">The arg.</param>
public bool Add(T arg)
{
bool wasAdded = false;
bool wasSuccessful = false;
this.Root = this.Add(this.Root, arg, ref wasAdded, ref wasSuccessful);
return wasSuccessful;
}
/// <summary>
/// Deletes the specified value argument.
/// Complexity: O(log(N))
/// </summary>
/// <param name="arg">The arg.</param>
public bool Delete(T arg)
{
bool wasSuccessful = false;
if (this.Root != null)
{
bool wasDeleted = false;
this.Root = this.Delete(this.Root, arg, ref wasDeleted, ref wasSuccessful);
}
return wasSuccessful;
}
/// <summary>
/// Gets the min value stored in the tree.
/// Complexity: O(log(N))
/// </summary>
/// <param name="value">The location which upon return will store the min value in the tree.</param>
/// <returns>a boolean indicating success or failure</returns>
public bool GetMin(out T value)
{
if (this.Root != null)
{
var min = FindMin(this.Root);
if (min != null)
{
value = min.Data;
return true;
}
}
value = default(T);
return false;
}
/// <summary>
/// Gets the max value stored in the tree.
/// Complexity: O(log(N))
/// </summary>
/// <param name="value">The location which upon return will store the max value in the tree.</param>
/// <returns>a boolean indicating success or failure</returns>
public bool GetMax(out T value)
{
if (this.Root != null)
{
var max = FindMax(this.Root);
if (max != null)
{
value = max.Data;
return true;
}
}
value = default(T);
return false;
}
/// <summary>
/// Determines whether the tree contains the specified argument value.
/// Complexity: O(log(N))
/// </summary>
/// <param name="arg">The arg to test against.</param>
/// <returns>
/// <c>true</c> if tree contains the specified arg; otherwise, <c>false</c>.
/// </returns>
public bool Contains(T arg)
{
return this.Search(this.Root, arg) != null;
}
/// <summary>
/// Deletes the min. value in the tree.
/// Complexity: O(log(N))
/// </summary>
public bool DeleteMin()
{
if (this.Root != null)
{
bool wasDeleted = false, wasSuccessful = false;
this.Root = this.DeleteMin(this.Root, ref wasDeleted, ref wasSuccessful);
return wasSuccessful;
}
return false;
}
/// <summary>
/// Deletes the max. value in the tree.
/// Complexity: O(log(N))
/// </summary>
public bool DeleteMax()
{
if (this.Root != null)
{
bool wasDeleted = false, wasSuccessful = false;
this.Root = this.DeleteMax(this.Root, ref wasDeleted, ref wasSuccessful);
return wasSuccessful;
}
return false;
}
#if TREE_WITH_CONCAT_AND_SPLIT_OPERATIONS
/// <summary>
/// Concatenates the elements of the two trees.
/// Precondition: ALL elements of the 'other' argument AVL tree must be LARGER than all elements contained in this instance.
/// Complexity: O(log(N1) + log(N2)). See Remarks section below.
/// </summary>
/// <remarks>
/// Complexity:
/// Assuming height(node1) > height(node2), our procedure runs in height(node1) + height(node2) i.e. O(log(n1)) + O(log(n2)) due to the two calls to findMin/deleteMin (or findMax, deleteMax respectively).
/// Runs in O(height(node1)) if height(node1) == height(node2).
/// Improvements:
/// Performing find/delete in one operation gives O(height(node1)) speed.
/// Furthermore, if storing min value for each subtree, one obtains the theoretical O(height(node1) - height(node2)).
/// </remarks>
public AVLTree<T> Concat(AVLTree<T> other)
{
if (other == null)
{
return this;
}
var root = Concat(this.Root, other.Root);
if (root != null)
{
return new AVLTree<T>() { Root = root };
}
return null;
}
/// <summary>
/// Splits this AVL Tree instance into 2 AVL subtrees using the specified value as the cut/split point.
/// The value to split by must exist in the tree.
/// This function is destructive (i.e. the current AVL tree instance is not a valid anymore upon return of this function)
/// </summary>
/// <param name="value">The value to use when splitting this instance.</param>
/// <param name="mode">The mode specifying what to do with the value used for splitting. Options are not to include this value in either of the two resulting trees, to include it in the left or to include it in the right tree respectively</param>
/// <param name="splitLeftTree">[out] The left avl tree. Upon return, all values of this subtree are less than the value argument.</param>
/// <param name="splitRightTree">[out] The right avl tree. Upon return, all values of this subtree are greater than the value argument.</param>
/// <returns>a boolean indicating success or failure</returns>
public bool Split(T value, SplitOperationMode mode, out AVLTree<T> splitLeftTree, out AVLTree<T> splitRightTree)
{
splitLeftTree = null;
splitRightTree = null;
Node<T> splitLeftRoot = null, splitRightRoot = null;
bool wasFound = false;
this.Split(this.Root, value, ref splitLeftRoot, ref splitRightRoot, mode, ref wasFound);
if (wasFound)
{
splitLeftTree = new AVLTree<T>() { Root = splitLeftRoot };
splitRightTree = new AVLTree<T>() { Root = splitRightRoot };
}
return wasFound;
}
#endif
/// <summary>
/// Returns the height of the tree.
/// Complexity: O(log N).
/// </summary>
/// <returns>the avl tree height</returns>
public int GetHeightLogN()
{
return this.GetHeightLogN(this.Root);
}
/// <summary>
/// Clears this instance.
/// Complexity: O(1).
/// </summary>
public void Clear()
{
this.Root = null;
}
/// <summary>
/// Prints this instance.
/// </summary>
public void Print()
{
this.Visit((node, level) =>
{
Console.Write(new string(' ', 2 * level));
Console.WriteLine("{0, 6}", node.Data);
});
}
internal int GetCount()
{
int count = 0;
this.Visit((node, level) =>
{
count++;
});
return count;
}
#endregion
#region Private Methods
private static IComparer<T> GetComparer()
{
if (typeof(IComparable<T>).IsAssignableFrom(typeof(T)) || typeof(System.IComparable).IsAssignableFrom(typeof(T)))
{
return Comparer<T>.Default;
}
else
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The type {0} cannot be compared. It must implement IComparable<T> or IComparable interface", typeof(T).FullName));
}
}
/// <summary>
/// Gets the height of the tree in log(n) time.
/// </summary>
/// <param name="node">The node.</param>
/// <returns>The height of the tree. Runs in O(log(n)) where n is the number of nodes in the tree </returns>
private int GetHeightLogN(Node<T> node)
{
if (node == null)
{
return 0;
}
else
{
int leftHeight = this.GetHeightLogN(node.Left);
if (node.Balance == 1)
{
leftHeight++;
}
return 1 + leftHeight;
}
}
/// <summary>
/// Adds the specified data to the tree identified by the specified argument.
/// </summary>
/// <param name="elem">The elem.</param>
/// <param name="data">The data.</param>
/// <returns></returns>
private Node<T> Add(Node<T> elem, T data, ref bool wasAdded, ref bool wasSuccessful)
{
if (elem == null)
{
elem = new Node<T> { Data = data, Left = null, Right = null, Balance = 0 };
#if TREE_WITH_CONCAT_AND_SPLIT_OPERATIONS
elem.Height = 1;
#endif
wasAdded = true;
wasSuccessful = true;
}
else
{
int resultCompare = this.comparer.Compare(data, elem.Data);
if (resultCompare < 0)
{
var newLeft = Add(elem.Left, data, ref wasAdded, ref wasSuccessful);
if (elem.Left != newLeft)
{
elem.Left = newLeft;
#if TREE_WITH_PARENT_POINTERS
newLeft.Parent = elem;
#endif
}
if (wasAdded)
{
--elem.Balance;
if (elem.Balance == 0)
{
wasAdded = false;
}
else if (elem.Balance == -2)
{
int leftBalance = newLeft.Balance;
if (leftBalance == 1)
{
int elemLeftRightBalance = newLeft.Right.Balance;
elem.Left = RotateLeft(newLeft);
elem = RotateRight(elem);
elem.Balance = 0;
elem.Left.Balance = elemLeftRightBalance == 1 ? -1 : 0;
elem.Right.Balance = elemLeftRightBalance == -1 ? 1 : 0;
}
else if (leftBalance == -1)
{
elem = RotateRight(elem);
elem.Balance = 0;
elem.Right.Balance = 0;
}
wasAdded = false;
}
}
}
else if (resultCompare > 0)
{
var newRight = this.Add(elem.Right, data, ref wasAdded, ref wasSuccessful);
if (elem.Right != newRight)
{
elem.Right = newRight;
#if TREE_WITH_PARENT_POINTERS
newRight.Parent = elem;
#endif
}
if (wasAdded)
{
++elem.Balance;
if (elem.Balance == 0)
{
wasAdded = false;
}
else if (elem.Balance == 2)
{
int rightBalance = newRight.Balance;
if (rightBalance == -1)
{
int elemRightLeftBalance = newRight.Left.Balance;
elem.Right = RotateRight(newRight);
elem = RotateLeft(elem);
elem.Balance = 0;
elem.Left.Balance = elemRightLeftBalance == 1 ? -1 : 0;
elem.Right.Balance = elemRightLeftBalance == -1 ? 1 : 0;
}
else if (rightBalance == 1)
{
elem = RotateLeft(elem);
elem.Balance = 0;
elem.Left.Balance = 0;
}
wasAdded = false;
}
}
}
#if TREE_WITH_CONCAT_AND_SPLIT_OPERATIONS
elem.Height = 1 + Math.Max(
elem.Left != null ? elem.Left.Height : 0,
elem.Right != null ? elem.Right.Height : 0);
#endif
}
return elem;
}
/// <summary>
/// Deletes the specified arg. value from the tree.
/// </summary>
/// <param name="node">The node.</param>
/// <param name="arg">The arg.</param>
/// <returns></returns>
private Node<T> Delete(Node<T> node, T arg, ref bool wasDeleted, ref bool wasSuccessful)
{
int cmp = this.comparer.Compare(arg, node.Data);
Node<T> newChild = null;
if (cmp < 0)
{
if (node.Left != null)
{
newChild = this.Delete(node.Left, arg, ref wasDeleted, ref wasSuccessful);
if (node.Left != newChild)
{
node.Left = newChild;
}
if (wasDeleted)
{
node.Balance++;
}
}
}
else if (cmp == 0)
{
wasDeleted = true;
if (node.Left != null && node.Right != null)
{
var min = FindMin(node.Right);
T data = node.Data;
node.Data = min.Data;
min.Data = data;
wasDeleted = false;
newChild = this.Delete(node.Right, data, ref wasDeleted, ref wasSuccessful);
if (node.Right != newChild)
{
node.Right = newChild;
}
if (wasDeleted)
{
node.Balance--;
}
}
else if (node.Left == null)
{
wasSuccessful = true;
#if TREE_WITH_PARENT_POINTERS
if (node.Right != null)
{
node.Right.Parent = node.Parent;
}
#endif
return node.Right;
}
else
{
wasSuccessful = true;
#if TREE_WITH_PARENT_POINTERS
if (node.Left != null)
{
node.Left.Parent = node.Parent;
}
#endif
return node.Left;
}
}
else
{
if (node.Right != null)
{
newChild = this.Delete(node.Right, arg, ref wasDeleted, ref wasSuccessful);
if (node.Right != newChild)
{
node.Right = newChild;
}
if (wasDeleted)
{
node.Balance--;
}
}
}
if (wasDeleted)
{
if (node.Balance == 1 || node.Balance == -1)
{
wasDeleted = false;
}
else if (node.Balance == -2)
{
var nodeLeft = node.Left;
int leftBalance = nodeLeft.Balance;
if (leftBalance == 1)
{
int leftRightBalance = nodeLeft.Right.Balance;
node.Left = RotateLeft(nodeLeft);
node = RotateRight(node);
node.Balance = 0;
node.Left.Balance = (leftRightBalance == 1) ? -1 : 0;
node.Right.Balance = (leftRightBalance == -1) ? 1 : 0;
}
else if (leftBalance == -1)
{
node = RotateRight(node);
node.Balance = 0;
node.Right.Balance = 0;
}
else if (leftBalance == 0)
{
node = RotateRight(node);
node.Balance = 1;
node.Right.Balance = -1;
wasDeleted = false;
}
}
else if (node.Balance == 2)
{
var nodeRight = node.Right;
int rightBalance = nodeRight.Balance;
if (rightBalance == -1)
{
int rightLeftBalance = nodeRight.Left.Balance;
node.Right = RotateRight(nodeRight);
node = RotateLeft(node);
node.Balance = 0;
node.Left.Balance = (rightLeftBalance == 1) ? -1 : 0;
node.Right.Balance = (rightLeftBalance == -1) ? 1 : 0;
}
else if (rightBalance == 1)
{
node = RotateLeft(node);
node.Balance = 0;
node.Left.Balance = 0;
}
else if (rightBalance == 0)
{
node = RotateLeft(node);
node.Balance = -1;
node.Left.Balance = 1;
wasDeleted = false;
}
}
#if TREE_WITH_CONCAT_AND_SPLIT_OPERATIONS
node.Height = 1 + Math.Max(
node.Left != null ? node.Left.Height : 0,
node.Right != null ? node.Right.Height : 0);
#endif
}
return node;
}
/// <summary>
/// Finds the min.
/// </summary>
/// <param name="node">The node.</param>
/// <returns></returns>
private static Node<T> FindMin(Node<T> node)
{
while (node != null && node.Left != null)
{
node = node.Left;
}
return node;
}
/// <summary>
/// Finds the max.
/// </summary>
/// <param name="node">The node.</param>
/// <returns></returns>
private static Node<T> FindMax(Node<T> node)
{
while (node != null && node.Right != null)
{
node = node.Right;
}
return node;
}
/// <summary>
/// Searches the specified subtree for the specified data.
/// </summary>
/// <param name="subtree">The subtree.</param>
/// <param name="data">The data to search for.</param>
/// <returns>null if not found, otherwise the node instance with the specified value</returns>
private Node<T> Search(Node<T> subtree, T data)
{
if (subtree != null)
{
if (this.comparer.Compare(data, subtree.Data) < 0)
{
return this.Search(subtree.Left, data);
}
else if (this.comparer.Compare(data, subtree.Data) > 0)
{
return this.Search(subtree.Right, data);
}
else
{
return subtree;
}
}
else
{
return null;
}
}
/// <summary>
/// Deletes the min element in the tree.
/// Precondition: (node != null)
/// </summary>
/// <param name="node">The node.</param>
/// <returns></returns>
private Node<T> DeleteMin(Node<T> node, ref bool wasDeleted, ref bool wasSuccessful)
{
Debug.Assert(node != null);
if (node.Left == null)
{
wasDeleted = true;
wasSuccessful = true;
#if TREE_WITH_PARENT_POINTERS
if (node.Right != null)
{
node.Right.Parent = node.Parent;
}
#endif
return node.Right;
}
node.Left = this.DeleteMin(node.Left, ref wasDeleted, ref wasSuccessful);
if (wasDeleted)
{
node.Balance++;
}
if (wasDeleted)
{
if (node.Balance == 1 || node.Balance == -1)
{
wasDeleted = false;
}
else if (node.Balance == -2)
{
int leftBalance = node.Left.Balance;
if (leftBalance == 1)
{
int leftRightBalance = node.Left.Right.Balance;
node.Left = RotateLeft(node.Left);
node = RotateRight(node);
node.Balance = 0;
node.Left.Balance = (leftRightBalance == 1) ? -1 : 0;
node.Right.Balance = (leftRightBalance == -1) ? 1 : 0;
}
else if (leftBalance == -1)
{
node = RotateRight(node);
node.Balance = 0;
node.Right.Balance = 0;
}
else if (leftBalance == 0)
{
node = RotateRight(node);
node.Balance = 1;
node.Right.Balance = -1;
wasDeleted = false;
}
}
else if (node.Balance == 2)
{
int rightBalance = node.Right.Balance;
if (rightBalance == -1)
{
int rightLeftBalance = node.Right.Left.Balance;
node.Right = RotateRight(node.Right);
node = RotateLeft(node);
node.Balance = 0;
node.Left.Balance = (rightLeftBalance == 1) ? -1 : 0;
node.Right.Balance = (rightLeftBalance == -1) ? 1 : 0;
}
else if (rightBalance == 1)
{
node = RotateLeft(node);
node.Balance = 0;
node.Left.Balance = 0;
}
else if (rightBalance == 0)
{
node = RotateLeft(node);
node.Balance = -1;
node.Left.Balance = 1;
wasDeleted = false;
}
}
#if TREE_WITH_CONCAT_AND_SPLIT_OPERATIONS
node.Height = 1 + Math.Max(
node.Left != null ? node.Left.Height : 0,
node.Right != null ? node.Right.Height : 0);
#endif
}
return node;
}
/// <summary>
/// Deletes the max element in the tree.
/// Precondition: (node != null)
/// </summary>
/// <param name="node">The node.</param>
/// <returns></returns>
private Node<T> DeleteMax(Node<T> node, ref bool wasDeleted, ref bool wasSuccessful)
{
Debug.Assert(node != null);
if (node.Right == null)
{
wasDeleted = true;
wasSuccessful = true;
#if TREE_WITH_PARENT_POINTERS
if (node.Left != null)
{
node.Left.Parent = node.Parent;
}
#endif
return node.Left;
}
node.Right = this.DeleteMax(node.Right, ref wasDeleted, ref wasSuccessful);
if (wasDeleted)
{
node.Balance--;
}
if (wasDeleted)
{
if (node.Balance == 1 || node.Balance == -1)
{
wasDeleted = false;
}
else if (node.Balance == -2)
{
int leftBalance = node.Left.Balance;
if (leftBalance == 1)
{
int leftRightBalance = node.Left.Right.Balance;
node.Left = RotateLeft(node.Left);
node = RotateRight(node);
node.Balance = 0;
node.Left.Balance = (leftRightBalance == 1) ? -1 : 0;
node.Right.Balance = (leftRightBalance == -1) ? 1 : 0;
}
else if (leftBalance == -1)
{
node = RotateRight(node);
node.Balance = 0;
node.Right.Balance = 0;
}
else if (leftBalance == 0)
{
node = RotateRight(node);
node.Balance = 1;
node.Right.Balance = -1;
wasDeleted = false;
}
}
else if (node.Balance == 2)
{
int rightBalance = node.Right.Balance;
if (rightBalance == -1)
{
int rightLeftBalance = node.Right.Left.Balance;
node.Right = RotateRight(node.Right);
node = RotateLeft(node);
node.Balance = 0;
node.Left.Balance = (rightLeftBalance == 1) ? -1 : 0;
node.Right.Balance = (rightLeftBalance == -1) ? 1 : 0;
}
else if (rightBalance == 1)
{
node = RotateLeft(node);
node.Balance = 0;
node.Left.Balance = 0;
}
else if (rightBalance == 0)
{
node = RotateLeft(node);
node.Balance = -1;
node.Left.Balance = 1;
wasDeleted = false;
}
}
#if TREE_WITH_CONCAT_AND_SPLIT_OPERATIONS
node.Height = 1 + Math.Max(
node.Left != null ? node.Left.Height : 0,
node.Right != null ? node.Right.Height : 0);
#endif
}
return node;
}
#if TREE_WITH_PARENT_POINTERS
/// <summary>
/// Returns the predecessor of the specified node.
/// </summary>
/// <returns></returns>
private static Node<T> Predecesor(Node<T> node)
{
if (node.Left != null)
{
return FindMax(node.Left);
}
else
{
var p = node;
while (p.Parent != null && p.Parent.Left == p)
{
p = p.Parent;
}
return p.Parent;