Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (this.Root is null)
- {
- return false;
- }
- var result = RemoveHelper(this.Root, item);
- if (result is null)
- {
- return false;
- }
- --this.Count;
- return true;
- BinarySearchTreeNode RemoveHelper(BinarySearchTreeNode root, T value)
- {
- if (root is null)
- {
- return null;
- }
- var comparisonResult = this.comparer.Compare(value, root.Value);
- if (comparisonResult < 0)
- {
- root.LeftNode = RemoveHelper(root.LeftNode, value);
- }
- else if (comparisonResult > 0)
- {
- root.RightNode = RemoveHelper(root.RightNode, value);
- }
- else if (root.LeftNode is null)
- {
- return root.RightNode;
- }
- else if (root.RightNode is null)
- {
- return root.LeftNode;
- }
- else
- {
- root.Value = GetMin(root.RightNode);
- root.RightNode = RemoveHelper(root.RightNode, root.Value);
- }
- return root;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement