File tree 1 file changed +15
-5
lines changed
BinarySearchTree/BinarySearchTree
1 file changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -71,9 +71,11 @@ public void DeleteNode(ref Node<T> root, T value)
71
71
72
72
private Node < T > Delete ( ref Node < T > root )
73
73
{
74
+ var tempValue = default ( T ) ;
75
+
74
76
if ( _globalRoot == root )
75
77
{
76
- Console . WriteLine ( " Deletion of root element is not allowed" ) ;
78
+ // Deletion of root element is not allowed;
77
79
return root ;
78
80
}
79
81
if ( root . LeftNode == null && root . RightNode == null )
@@ -86,20 +88,28 @@ private Node<T> Delete(ref Node<T> root)
86
88
}
87
89
else if ( root . RightNode == null )
88
90
{
89
- root = root . LeftNode ;
91
+ root = root . LeftNode ;
90
92
}
91
93
else
92
94
{
93
- Replace ( ref root ) ;
95
+ Replace ( ref root , ref tempValue ) ;
96
+ root . Value = tempValue ;
94
97
}
95
98
return root ;
96
99
}
97
100
98
- private void Replace ( ref Node < T > root )
101
+ private static void Replace ( ref Node < T > root , ref T newValue )
99
102
{
103
+ if ( root == null ) return ;
100
104
if ( root . LeftNode == null )
105
+ {
106
+ newValue = root . Value ;
101
107
root = root . RightNode ;
102
- else { Replace ( ref root . LeftNode ) ; }
108
+ }
109
+ else
110
+ {
111
+ Replace ( ref root . LeftNode , ref newValue ) ;
112
+ }
103
113
}
104
114
}
105
115
}
You can’t perform that action at this time.
0 commit comments