Skip to content

Commit a92846c

Browse files
zhijliaalhour
authored andcommitted
Fix bug for the remove method of BinarySearchTree (aalhour#58)
1 parent c5badc2 commit a92846c

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

DataStructures/Trees/AugmentedBinarySearchTree.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected bool _remove(BSTRankedNode<T> node)
146146

147147
if (node.ChildrenCount == 2) // if both children are present
148148
{
149-
var successor = node.RightChild;
149+
var successor = _findNextLarger(node);
150150
node.Value = successor.Value;
151151
return (true && _remove(successor));
152152
}

DataStructures/Trees/BinarySearchTree.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ protected virtual void _replaceNodeInParent(BSTNode<T> node, BSTNode<T> newNode
7777
else
7878
node.Parent.RightChild = newNode;
7979
}
80+
else
81+
{
82+
Root = newNode;
83+
}
8084

8185
if (newNode != null)
8286
newNode.Parent = node.Parent;
@@ -96,7 +100,7 @@ protected virtual bool _remove(BSTNode<T> node)
96100

97101
if (node.ChildrenCount == 2) // if both children are present
98102
{
99-
var successor = node.RightChild;
103+
var successor = _findNextLarger(node);
100104
node.Value = successor.Value;
101105
return (true && _remove(successor));
102106
}

0 commit comments

Comments
 (0)