internal BinarySearchTreeNode(TEntity value, BinarySearchTreeNode <TEntity> parent) { Value = value; Parent = parent; }
public void Delete(TEntity value) { BinarySearchTreeNode <TEntity> node = FindNode(value); if (node == null) { throw new EntityNotExistsInBinarySearchTreeException(); } BinarySearchTreeNode <TEntity> parent = null; BinarySearchTreeNode <TEntity> child = null; // First case when node doesn't have children if (node.Left == null && node.Right == null) { parent = node.Parent; } //Second case when node has only one children else if (node.Left == null || node.Right == null) { parent = node.Parent; child = node.Left == null ? node.Right : node.Left; } // Third case - node has two children else { BinarySearchTreeNode <TEntity> successorNode = GetSuccessorNode(node); // Successor for node with two children always doesn't have left node // We change node value with successor value and connect successor parent with successor right child parent = successorNode.Parent; child = successorNode.Right; node.Value = successorNode.Value; } // change parent if (child != null) { child.Parent = parent; } // change child node if (parent == null) { _root = child; } else { if (parent.Value.CompareTo(node.Value) > 0) { parent.Left = child; } else { parent.Right = child; } } _size--; }