Пример #1
0
        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);
        }
Пример #2
0
        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);
        }
Пример #3
0
 public AVLTree(BinarySearchTreeNode <T> root, IComparer <T> comparer)
     : base(root, comparer)
 {
 }
Пример #4
0
 public AVLTree(BinarySearchTreeNode <T> root)
     : base(root)
 {
 }
Пример #5
0
 internal static int GetHeight(BinarySearchTreeNode <T> node)
 {
     return(node != null ? node.Height : -1);
 }
Пример #6
0
 public BinarySearchTreeNode <T> Insert(BinarySearchTreeNode <T> node, Action <T, T> visitAction)
 {
     return((BinarySearchTreeNode <T>)DoInsert(node, visitAction));
 }
Пример #7
0
 public BinarySearchTreeNode <T> Insert(BinarySearchTreeNode <T> node)
 {
     return(Insert(node, null));
 }
Пример #8
0
        public ReadOnlyCollection <BinarySearchTreeNode <T> > GetAncestors(BinarySearchTreeNode <T> node)
        {
            var ancestorList = DoGetAncestors(node).OfType <BinarySearchTreeNode <T> >().ToList();

            return(new ReadOnlyCollection <BinarySearchTreeNode <T> >(ancestorList));
        }
Пример #9
0
 public BinarySearchTreeNode <T> GetLeastCommonAncestor(BinarySearchTreeNode <T> x, BinarySearchTreeNode <T> y)
 {
     return((BinarySearchTreeNode <T>)DoGetLeastCommonAncestor(x, y));
 }