Esempio n. 1
0
        protected virtual 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));
                }
                else
                {
                    root = root.Left ?? root.Right;
                }
            }
            else if (comparisonResult < 0)
            {
                root.SetRight(DoDeleteRecursive(value, root.Right, ref result));
            }
            else
            {
                root.SetLeft(DoDeleteRecursive(value, root.Left, ref result));
            }
            return(root);
        }
Esempio n. 2
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);
        }