示例#1
0
    private bool HandleRemovingRootNode(T element)
    {
        if (this.root == null)
        {
            return(false);
        }

        if (this.root.Value.CompareTo(element) == 0)
        {
            if (this.root.RightNode == null)
            {
                this.root = this.root.LeftNode;
                return(true);
            }
            else
            {
                if (this.root.RightNode.LeftNode == null)
                {
                    this.root.RightNode.LeftNode = this.root.LeftNode;
                    this.root = this.root.RightNode;
                    return(true);
                }

                OrderedSetNode <T> replaceNode = this.root.RightNode.FindReplacingNode();

                this.root.Value = replaceNode.Value;

                return(true);
            }
        }
        else
        {
            return(this.root.Remove(element));
        }
    }
示例#2
0
    private static bool HandleRemovingNode(ref OrderedSetNode <T> node, T element)
    {
        if (node == null)
        {
            return(false);
        }

        if (node.Value.CompareTo(element) == 0)
        {
            if (node.RightNode == null)
            {
                node = node.LeftNode;
                return(true);
            }

            OrderedSetNode <T> replaceNode;
            if (node.RightNode.LeftNode == null)
            {
                node.RightNode.LeftNode = node.LeftNode;
                node = node.RightNode;
                return(true);
            }

            replaceNode = node.RightNode.FindReplacingNode();
            node.Value  = replaceNode.Value;

            return(true);
        }
        else
        {
            return(node.Remove(element));
        }
    }
示例#3
0
    public static void BalanceNode(ref OrderedSetNode <T> node, int balance)
    {
        if (balance < 0)
        {
            if (node.RightNode.Balance < 0)
            {
                OrderedSetNode <T> root = node.PerformLeftRotation();
                node = root;
            }
            else
            {
                OrderedSetNode <T> rightSubtreeRoot = node.RightNode.PerformRightRotation();
                node.RightNode = rightSubtreeRoot;

                OrderedSetNode <T> root = node.PerformLeftRotation();
                node = root;
            }
        }
        else
        {
            if (node.LeftNode.Balance < 0)
            {
                OrderedSetNode <T> leftSubtreeRoot = node.LeftNode.PerformLeftRotation();
                node.LeftNode = leftSubtreeRoot;

                OrderedSetNode <T> root = node.PerformRightRotation();
                node = root;
            }
            else
            {
                OrderedSetNode <T> root = node.PerformRightRotation();
                node = root;
            }
        }
    }
示例#4
0
    private void CheckBalance()
    {
        int balance = this.root.Balance;

        if (Math.Abs(balance) > 1)
        {
            OrderedSetNode <T> .BalanceNode(ref this.root, balance);
        }
    }
示例#5
0
    private OrderedSetNode <T> PerformRightRotation()
    {
        OrderedSetNode <T> root = this.LeftNode;

        this.LeftNode  = root.RightNode;
        root.RightNode = this;

        return(root);
    }
示例#6
0
    public void Add(T element)
    {
        if (this.root == null)
        {
            this.root = new OrderedSetNode <T>(element);
        }
        else
        {
            this.root.Add(element);
        }

        this.Count++;

        CheckBalance();
    }