@@ -10,39 +10,40 @@ namespace Advanced.Algorithms.DataStructures
10
10
/// </summary>
11
11
public class RedBlackTree < T > : BSTBase < T > , IEnumerable < T > where T : IComparable
12
12
{
13
+ //only used internally by Bentley-Ottmann sweepline algorithm for faster line swap operation.
13
14
private readonly Dictionary < T , BSTNodeBase < T > > nodeLookUp ;
15
+
14
16
internal RedBlackTreeNode < T > Root { get ; set ; }
15
- public int Count => Root == null ? 0 : Root . Count ;
16
17
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 ;
27
19
28
20
/// <summary>
29
- /// Initialize the BST with given sorted keys.
21
+ /// Initialize the BST with given sorted keys optionally .
30
22
/// Time complexity: O(n).
31
23
/// </summary>
32
24
/// <param name="collection">The sorted keys.</param>
33
25
/// <param name="enableNodeLookUp">Enabling lookup will fasten deletion/insertion/exists operations
34
26
/// at the cost of additional space.</param>
35
27
/// <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 )
38
29
{
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
+ }
44
38
}
45
39
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
+ }
46
47
47
48
/// <summary>
48
49
/// Time complexity: O(log(n))
@@ -200,7 +201,7 @@ public int Insert(T value)
200
201
return ( node , insertionPosition ) ;
201
202
}
202
203
203
- currentNode = currentNode . Right ;
204
+ currentNode = currentNode . Right ;
204
205
}
205
206
//current node is greater than new node
206
207
else if ( compareResult > 0 )
0 commit comments