@@ -24,20 +24,20 @@ public class BinarySearchTree<TKey>
24
24
private readonly Comparer < TKey > comparer ;
25
25
26
26
/// <summary>
27
- /// The root of the BST.
27
+ /// Gets the root of the BST.
28
28
/// </summary>
29
- private BinarySearchTreeNode < TKey > ? root ;
29
+ public BinarySearchTreeNode < TKey > ? Root { get ; private set ; }
30
30
31
31
public BinarySearchTree ( )
32
32
{
33
- root = null ;
33
+ Root = null ;
34
34
Count = 0 ;
35
35
comparer = Comparer < TKey > . Default ;
36
36
}
37
37
38
38
public BinarySearchTree ( Comparer < TKey > customComparer )
39
39
{
40
- root = null ;
40
+ Root = null ;
41
41
Count = 0 ;
42
42
comparer = customComparer ;
43
43
}
@@ -56,13 +56,13 @@ public BinarySearchTree(Comparer<TKey> customComparer)
56
56
/// </exception>
57
57
public void Add ( TKey key )
58
58
{
59
- if ( root is null )
59
+ if ( Root is null )
60
60
{
61
- root = new BinarySearchTreeNode < TKey > ( key ) ;
61
+ Root = new BinarySearchTreeNode < TKey > ( key ) ;
62
62
}
63
63
else
64
64
{
65
- Add ( root , key ) ;
65
+ Add ( Root , key ) ;
66
66
}
67
67
68
68
Count ++ ;
@@ -86,14 +86,14 @@ public void AddRange(IEnumerable<TKey> keys)
86
86
/// </summary>
87
87
/// <param name="key">The key to search for.</param>
88
88
/// <returns>The node with the specified key if it exists, otherwise a default value is returned.</returns>
89
- public BinarySearchTreeNode < TKey > ? Search ( TKey key ) => Search ( root , key ) ;
89
+ public BinarySearchTreeNode < TKey > ? Search ( TKey key ) => Search ( Root , key ) ;
90
90
91
91
/// <summary>
92
92
/// Checks if the specified key is in the BST.
93
93
/// </summary>
94
94
/// <param name="key">The key to search for.</param>
95
95
/// <returns>true if the key is in the BST, false otherwise.</returns>
96
- public bool Contains ( TKey key ) => Search ( root , key ) is not null ;
96
+ public bool Contains ( TKey key ) => Search ( Root , key ) is not null ;
97
97
98
98
/// <summary>
99
99
/// Removes a node with a key that matches <paramref name="key" />.
@@ -102,12 +102,12 @@ public void AddRange(IEnumerable<TKey> keys)
102
102
/// <returns>true if the removal was successful, false otherwise.</returns>
103
103
public bool Remove ( TKey key )
104
104
{
105
- if ( root is null )
105
+ if ( Root is null )
106
106
{
107
107
return false ;
108
108
}
109
109
110
- var result = Remove ( root , root , key ) ;
110
+ var result = Remove ( Root , Root , key ) ;
111
111
if ( result )
112
112
{
113
113
Count -- ;
@@ -122,12 +122,12 @@ public bool Remove(TKey key)
122
122
/// <returns>The node if possible, a default value otherwise.</returns>
123
123
public BinarySearchTreeNode < TKey > ? GetMin ( )
124
124
{
125
- if ( root is null )
125
+ if ( Root is null )
126
126
{
127
127
return default ;
128
128
}
129
129
130
- return GetMin ( root ) ;
130
+ return GetMin ( Root ) ;
131
131
}
132
132
133
133
/// <summary>
@@ -136,31 +136,31 @@ public bool Remove(TKey key)
136
136
/// <returns>The node if possible, a default value otherwise.</returns>
137
137
public BinarySearchTreeNode < TKey > ? GetMax ( )
138
138
{
139
- if ( root is null )
139
+ if ( Root is null )
140
140
{
141
141
return default ;
142
142
}
143
143
144
- return GetMax ( root ) ;
144
+ return GetMax ( Root ) ;
145
145
}
146
146
147
147
/// <summary>
148
148
/// Returns all the keys in the BST, sorted In-Order.
149
149
/// </summary>
150
150
/// <returns>A list of keys in the BST.</returns>
151
- public ICollection < TKey > GetKeysInOrder ( ) => GetKeysInOrder ( root ) ;
151
+ public ICollection < TKey > GetKeysInOrder ( ) => GetKeysInOrder ( Root ) ;
152
152
153
153
/// <summary>
154
154
/// Returns all the keys in the BST, sorted Pre-Order.
155
155
/// </summary>
156
156
/// <returns>A list of keys in the BST.</returns>
157
- public ICollection < TKey > GetKeysPreOrder ( ) => GetKeysPreOrder ( root ) ;
157
+ public ICollection < TKey > GetKeysPreOrder ( ) => GetKeysPreOrder ( Root ) ;
158
158
159
159
/// <summary>
160
160
/// Returns all the keys in the BST, sorted Post-Order.
161
161
/// </summary>
162
162
/// <returns>A list of keys in the BST.</returns>
163
- public ICollection < TKey > GetKeysPostOrder ( ) => GetKeysPostOrder ( root ) ;
163
+ public ICollection < TKey > GetKeysPostOrder ( ) => GetKeysPostOrder ( Root ) ;
164
164
165
165
/// <summary>
166
166
/// Recursive method to add a key to the BST.
@@ -261,7 +261,7 @@ private bool Remove(BinarySearchTreeNode<TKey>? parent, BinarySearchTreeNode<TKe
261
261
else
262
262
{
263
263
var predecessorNode = GetMax ( node . Left ) ;
264
- Remove ( root , root , predecessorNode . Key ) ;
264
+ Remove ( Root , Root , predecessorNode . Key ) ;
265
265
replacementNode = new BinarySearchTreeNode < TKey > ( predecessorNode . Key )
266
266
{
267
267
Left = node . Left ,
@@ -271,9 +271,9 @@ private bool Remove(BinarySearchTreeNode<TKey>? parent, BinarySearchTreeNode<TKe
271
271
272
272
// Replace the relevant node with a replacement found in the previous stages.
273
273
// Special case for replacing the root node.
274
- if ( node == root )
274
+ if ( node == Root )
275
275
{
276
- root = replacementNode ;
276
+ Root = replacementNode ;
277
277
}
278
278
else if ( parent . Left == node )
279
279
{
0 commit comments