/// <summary> /// Public method to remove new node with given value; /// </summary> /// <param name="value">The value to be removed.</param> public void Remove(T value) { _root = Remove(value, _root); }
public BinaryNode() { _element = default(T); _left = null; _right = null; }
/// <summary> /// Public method to insert new node with given value. /// </summary> /// <param name="value">The value to be inserted.</param> public void Insert(T value) { _root = Insert(value, _root); }
/// <summary> /// Public method to empty tree. /// </summary> public void MakeEmpty() { _root = null; //what about memory leak? }
public BinarySearchTree() { _root = null; }
public BinarySearchTree(BinaryNode <T> root) { Root = root; }
private void doRemove(BinaryNode <T> node) { var parent = node.Parent; bool nodeIsRoot = (parent == null); bool nodeIsRightChild = ReferenceEquals(parent?.Right, node); bool leftIsNull = (node.Left == null); bool rightIsNull = (node.Right == null); if (leftIsNull && rightIsNull) { if (!nodeIsRoot) { if (nodeIsRightChild) { parent.Right = null; } else { parent.Left = null; } } else { Root = null; } } else if (leftIsNull) { if (nodeIsRoot) { Root = node.Right; node.Right.Parent = null; } else // parent!=null { if (nodeIsRightChild) { parent.Right = node.Right; } else { parent.Left = node.Right; } } } else if (rightIsNull) { if (nodeIsRoot) { Root = node.Left; node.Left.Parent = null; } else // parent!=null { if (nodeIsRightChild) { parent.Right = node.Left; } else { parent.Left = node.Left; } } } else // both children are !=null { var newNode = DeleteMax(node.Left); if (parent == null) { Root = newNode; newNode.Parent = null; } else if (nodeIsRightChild) { parent.Right = newNode; } else { parent.Left = newNode; } newNode.Left = node.Left; newNode.Right = node.Right; if (newNode.Parent != null) { Splay(newNode.Parent); } } }
public BinaryNode(T value, BinaryNode <T> left, BinaryNode <T> right) : this(value) { Left = left; Right = right; }