@@ -10,39 +10,40 @@ namespace Advanced.Algorithms.DataStructures
1010 /// </summary>
1111 public class RedBlackTree < T > : BSTBase < T > , IEnumerable < T > where T : IComparable
1212 {
13+ //only used internally by Bentley-Ottmann sweepline algorithm for faster line swap operation.
1314 private readonly Dictionary < T , BSTNodeBase < T > > nodeLookUp ;
15+
1416 internal RedBlackTreeNode < T > Root { get ; set ; }
15- public int Count => Root == null ? 0 : Root . Count ;
1617
17- /// <param name="enableNodeLookUp">Enabling lookup will fasten deletion/insertion/exists operations
18- /// at the cost of additional space.</param>
19- /// <param name="equalityComparer">Provide custom IEquality comparer for node lookup dictionary when enabled.</param>
20- public RedBlackTree ( bool enableNodeLookUp = false , IEqualityComparer < T > equalityComparer = null )
21- {
22- if ( enableNodeLookUp )
23- {
24- nodeLookUp = new Dictionary < T , BSTNodeBase < T > > ( equalityComparer ) ;
25- }
26- }
18+ public int Count => Root == null ? 0 : Root . Count ;
2719
2820 /// <summary>
29- /// Initialize the BST with given sorted keys.
21+ /// Initialize the BST with given sorted keys optionally .
3022 /// Time complexity: O(n).
3123 /// </summary>
3224 /// <param name="collection">The sorted keys.</param>
3325 /// <param name="enableNodeLookUp">Enabling lookup will fasten deletion/insertion/exists operations
3426 /// at the cost of additional space.</param>
3527 /// <param name="equalityComparer">Provide custom IEquality comparer for node lookup dictionary when enabled.</param>
36- public RedBlackTree ( IEnumerable < T > sortedKeys , bool enableNodeLookUp = false , IEqualityComparer < T > equalityComparer = null )
37- : this ( enableNodeLookUp , equalityComparer )
28+ public RedBlackTree ( IEnumerable < T > sortedKeys = null )
3829 {
39- ValidateCollection ( sortedKeys ) ;
40- var nodes = sortedKeys . Select ( x => new RedBlackTreeNode < T > ( null , x ) ) . ToArray ( ) ;
41- Root = ( RedBlackTreeNode < T > ) ToBST ( nodes ) ;
42- assignColors ( Root ) ;
43- assignCount ( Root ) ;
30+ if ( sortedKeys != null )
31+ {
32+ ValidateCollection ( sortedKeys ) ;
33+ var nodes = sortedKeys . Select ( x => new RedBlackTreeNode < T > ( null , x ) ) . ToArray ( ) ;
34+ Root = ( RedBlackTreeNode < T > ) ToBST ( nodes ) ;
35+ assignColors ( Root ) ;
36+ assignCount ( Root ) ;
37+ }
4438 }
4539
40+ ///Special (internal only) constructor for Bentley-Ottmann sweep line algorithm for fast line swap.
41+ /// <param name="equalityComparer">Provide custom IEquality comparer for node lookup dictionary.</param>
42+ internal RedBlackTree ( IEqualityComparer < T > equalityComparer )
43+ : this ( )
44+ {
45+ nodeLookUp = new Dictionary < T , BSTNodeBase < T > > ( equalityComparer ) ;
46+ }
4647
4748 /// <summary>
4849 /// Time complexity: O(log(n))
@@ -200,7 +201,7 @@ public int Insert(T value)
200201 return ( node , insertionPosition ) ;
201202 }
202203
203- currentNode = currentNode . Right ;
204+ currentNode = currentNode . Right ;
204205 }
205206 //current node is greater than new node
206207 else if ( compareResult > 0 )
0 commit comments