public void AddTo(AVLTreeNode <T> node, T value) { if (value.CompareTo(node.Value) < 0) { if (node.Left == null) { node.Left = new AVLTreeNode <T>(value, node, this); } else { AddTo(node.Left, value); } } else { if (node.Right == null) { node.Right = new AVLTreeNode <T>(value, node, this); } else { AddTo(node.Right, value); } } node.Balance(); }
//delete public bool Remove(T value) { AVLTreeNode <T> current; current = Find(value); if (current == null) { return(false); } AVLTreeNode <T> treeToBalance = current.Parent; Count--; if (current.Right == null) { if (current.Parent == null) { Head = current.Left; if (Head != null) { Head.Parent = null; } } else { int result = current.Parent.CompareTo(current.Value); if (result > 0) { current.Parent.Left = current.Left; } else if (result < 0) { current.Parent.Right = current.Left; } } } else if (current.Right.Left == null) { current.Right.Left = current.Left; if (current.Parent == null) { Head = current.Right; if (Head != null) { Head.Parent = null; } } else { int result = current.Parent.CompareTo(current.Value); if (result > 0) { current.Parent.Left = current.Right; } else if (result < 0) { current.Parent.Right = current.Right; } } } else { AVLTreeNode <T> leftmost = current.Right.Left; while (leftmost.Left != null) { leftmost = leftmost.Left; } leftmost.Parent.Left = leftmost.Right; leftmost.Left = current.Left; leftmost.Right = current.Right; if (current.Parent == null) { Head = leftmost; if (Head != null) { Head.Parent = null; } } else { int result = current.Parent.CompareTo(current.Value); if (result > 0) { current.Parent.Left = leftmost; } else if (result < 0) { current.Parent.Right = leftmost; } } } if (treeToBalance != null) { treeToBalance.Balance(); } else { if (Head != null) { Head.Balance(); } } return(true); }