コード例 #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));
 }