protected override BinarySearchTreeNode <T> DoDeleteRecursive(T value, BinarySearchTreeNode <T> root, ref bool result) { if (root == null) { result = false; return(root); } int comparisonResult = Compare(root.Value, value); if (comparisonResult == 0) { if (root.IsFull) { BinarySearchTreeNode <T> .ExchangeValues(root, DoGetMaximum(root.Left)); root.SetLeft(DoDeleteRecursive(value, root.Left, ref result)); root = EnsureBalanced(root, x => GetHeight(x.Right.Left) > GetHeight(x.Right.Right), RotateRL, RotateRR); } else { root = root.Left ?? root.Right; } } else if (comparisonResult < 0) { root.SetRight(DoDeleteRecursive(value, root.Right, ref result)); root = EnsureBalanced(root, x => GetHeight(x.Left.Left) > GetHeight(x.Left.Right), RotateLL, RotateLR); } else { root.SetLeft(DoDeleteRecursive(value, root.Left, ref result)); root = EnsureBalanced(root, x => GetHeight(x.Right.Left) > GetHeight(x.Right.Right), RotateRL, RotateRR); } if (root != null) { root.SetHeight(Math.Max(GetHeight(root.Left), GetHeight(root.Right)) + 1); } return(root); }
static int GetAVLTreeHeight(BinarySearchTreeNode <T> root) { if (root == null) { return(-1); } int lHeight = GetAVLTreeHeight(root.Left); if (lHeight == int.MinValue) { return(int.MinValue); } int rHeight = GetAVLTreeHeight(root.Right); if (rHeight == int.MinValue) { return(int.MinValue); } if (Math.Abs(lHeight - rHeight) > 1) { return(int.MinValue); } return(Math.Max(lHeight, rHeight) + 1); }
public AVLTree(BinarySearchTreeNode <T> root, IComparer <T> comparer) : base(root, comparer) { }
public AVLTree(BinarySearchTreeNode <T> root) : base(root) { }
internal static int GetHeight(BinarySearchTreeNode <T> node) { return(node != null ? node.Height : -1); }
public BinarySearchTreeNode <T> Insert(BinarySearchTreeNode <T> node, Action <T, T> visitAction) { return((BinarySearchTreeNode <T>)DoInsert(node, visitAction)); }
public BinarySearchTreeNode <T> Insert(BinarySearchTreeNode <T> node) { return(Insert(node, null)); }
public ReadOnlyCollection <BinarySearchTreeNode <T> > GetAncestors(BinarySearchTreeNode <T> node) { var ancestorList = DoGetAncestors(node).OfType <BinarySearchTreeNode <T> >().ToList(); return(new ReadOnlyCollection <BinarySearchTreeNode <T> >(ancestorList)); }
public BinarySearchTreeNode <T> GetLeastCommonAncestor(BinarySearchTreeNode <T> x, BinarySearchTreeNode <T> y) { return((BinarySearchTreeNode <T>)DoGetLeastCommonAncestor(x, y)); }