private void TwoChildsDelete(BinaryNode node) { BinaryNode next = FindSuccessor(node); BinaryNode parent = next.Parent; if (parent == node) { parent.SetKey(next.Key); parent.SetValue(next.Value); parent.SetRightNode(next.Right); if (next.Right != null) { next.Right.SetParentNode(parent); } } else { parent.SetLeftNode(next.Right); if (next.Right != null) { next.Right.SetParentNode(parent); } node.SetKey(next.Key); node.SetValue(next.Value); } next.SetParentNode(null); next.SetLeftNode(null); next.SetRightNode(null); }
private void OneChildDelete(BinaryNode nodeToDelete) { BinaryNode child = null; if (nodeToDelete.Left != null) { child = nodeToDelete.Left; } else { child = nodeToDelete.Right; } nodeToDelete.SetKey(child.Key); nodeToDelete.SetValue(child.Value); nodeToDelete.SetLeftNode(child.Left); nodeToDelete.SetRightNode(child.Right); child.SetParentNode(null); child.SetRightNode(null); child.SetLeftNode(null); }
private void Insert(int key, object value, BinaryNode root) { BinaryNode parent = root; BinaryNode child = parent; while (child != null) { parent = child; if (child.Key == key) { throw new Exception("Tree can contains only unique values "); } if (child.Key < key) { child = child.Right; } else //else if(child.Key > key) { child = child.Left; } } BinaryNode newNode = new BinaryNode(key, value); if (parent.Key < key) { parent.SetRightNode(newNode); } else { parent.SetLeftNode(newNode); } newNode.SetParentNode(parent); }