예제 #1
0
        private BinaryNode <T> RotateLeft(BinaryNode <T> binaryNode)
        {
            BinaryNode <T> right     = binaryNode.GetRight();
            BinaryNode <T> rightLeft = right.GetLeft();
            BinaryNode <T> parent    = binaryNode.GetPadre();

            right.SetPadre(parent);
            right.SetLeft(binaryNode);
            binaryNode.SetRight(rightLeft);
            binaryNode.SetPadre(right);


            if (rightLeft != null)
            {
                rightLeft.SetPadre(binaryNode);
            }

            if (binaryNode == this.root)
            {
                this.root = right;
            }
            else if (parent.GetRight() == binaryNode)
            {
                parent.SetRight(right);
            }
            else
            {
                parent.SetLeft(right);
            }

            right.Balance++;
            binaryNode.Balance = -right.Balance;

            return(right);
        }
예제 #2
0
        private BinaryNode <T> RotateRight(BinaryNode <T> binaryNode)
        {
            BinaryNode <T> left      = binaryNode.GetLeft();
            BinaryNode <T> leftRight = left.GetRight();
            BinaryNode <T> parent    = binaryNode.GetPadre();

            left.SetPadre(parent);
            left.SetRight(binaryNode);
            binaryNode.SetLeft(leftRight);
            binaryNode.SetPadre(left);


            if (leftRight != null)
            {
                leftRight.SetPadre(binaryNode);
            }

            if (binaryNode == this.root)
            {
                this.root = left;
            }
            else if (parent.GetLeft() == binaryNode)
            {
                parent.SetLeft(left);
            }
            else
            {
                parent.SetRight(left);
            }

            left.Balance--;
            binaryNode.Balance = -left.Balance;

            return(left);
        }
예제 #3
0
        private BinaryNode <T> RotateLeftRight(BinaryNode <T> binaryNode)
        {
            BinaryNode <T> left           = binaryNode.GetLeft();
            BinaryNode <T> leftRight      = left.GetRight();
            BinaryNode <T> parent         = binaryNode.GetPadre();
            BinaryNode <T> leftRightRight = leftRight.GetRight();
            BinaryNode <T> leftRightLeft  = leftRight.GetLeft();

            leftRight.SetPadre(parent);
            binaryNode.SetLeft(leftRightRight);
            left.SetRight(leftRightLeft);
            leftRight.SetLeft(left);
            leftRight.SetRight(binaryNode);
            left.SetPadre(leftRight);
            binaryNode.SetPadre(leftRight);

            if (leftRightRight != null)
            {
                leftRightRight.SetPadre(binaryNode);
            }

            if (leftRightLeft != null)
            {
                leftRightLeft.SetPadre(left);
            }

            if (binaryNode == this.root)
            {
                SetRoot(leftRight);
            }
            else if (parent.GetLeft() == binaryNode)
            {
                parent.SetLeft(leftRight);
            }
            else
            {
                parent.SetRight(leftRight);
            }

            if (leftRight.Balance == -1)
            {
                binaryNode.Balance = 0;
                left.Balance       = 1;
            }
            else if (leftRight.Balance == 0)
            {
                binaryNode.Balance = 0;
                left.Balance       = 0;
            }
            else
            {
                binaryNode.Balance = -1;
                left.Balance       = 0;
            }

            leftRight.Balance = 0;

            return(leftRight);
        }
예제 #4
0
        private BinaryNode <T> RotateRightLeft(BinaryNode <T> binaryNode)
        {
            BinaryNode <T> right          = binaryNode.GetRight();
            BinaryNode <T> rightLeft      = right.GetLeft();
            BinaryNode <T> parent         = binaryNode.GetPadre();
            BinaryNode <T> rightLeftLeft  = rightLeft.GetLeft();
            BinaryNode <T> rightLeftRight = rightLeft.GetRight();

            rightLeft.SetPadre(parent);
            binaryNode.SetRight(rightLeftLeft);
            right.SetLeft(rightLeftRight);
            rightLeft.SetRight(right);
            rightLeft.SetLeft(binaryNode);
            right.SetPadre(rightLeft);
            binaryNode.SetPadre(rightLeft);

            if (rightLeftLeft != null)
            {
                rightLeftLeft.SetPadre(binaryNode);
            }

            if (rightLeftRight != null)
            {
                rightLeftRight.SetPadre(right);
            }

            if (binaryNode == this.root)
            {
                SetRoot(rightLeft);
            }
            else if (parent.GetRight() == binaryNode)
            {
                parent.SetRight(rightLeft);
            }
            else
            {
                parent.SetLeft(rightLeft);
            }

            if (rightLeft.Balance == 1)
            {
                binaryNode.Balance = 0;
                right.Balance      = -1;
            }
            else if (rightLeft.Balance == 0)
            {
                binaryNode.Balance = 0;
                right.Balance      = 0;
            }
            else
            {
                binaryNode.Balance = 1;
                right.Balance      = 0;
            }

            rightLeft.Balance = 0;

            return(rightLeft);
        }
예제 #5
0
        private bool Eliminate2(BinaryNode <T> node)
        {
            // left
            BinaryNode <T> TempLeft = node.GetPadre().GetLeft();
            // right
            BinaryNode <T> TempRight = node.GetPadre().GetRight();

            BinaryNode <T> actual = node.GetLeft() != null?node.GetLeft() : node.GetRight();

            if (TempLeft == node)
            {
                node.GetPadre().SetLeft(actual);

                actual.SetPadre(node.GetPadre());
                node.SetRight(null);
                node.SetLeft(null);
                count--;
                return(true);
            }

            if (TempRight == node)
            {
                node.GetPadre().SetRight(actual);

                /* Eliminando todas las referencias hacia el nodo */
                actual.SetPadre(node.GetPadre());
                node.SetRight(null);
                node.SetLeft(null);
                count--;
                return(true);
            }

            return(false);
        }
예제 #6
0
        private bool Delete2(BinaryNode <T> binaryNode)
        {
            BinaryNode <T> TempLeft  = binaryNode.GetPadre().GetLeft();
            BinaryNode <T> TempRight = binaryNode.GetPadre().GetRight();

            BinaryNode <T> current = binaryNode.GetLeft() != null?binaryNode.GetLeft() : binaryNode.GetRight();

            if (TempLeft == binaryNode)
            {
                binaryNode.GetPadre().SetLeft(current);

                current.SetPadre(binaryNode.GetPadre());
                binaryNode.SetRight(null);
                binaryNode.SetLeft(null);
                count--;
                return(true);
            }

            if (TempRight == binaryNode)
            {
                binaryNode.GetPadre().SetRight(current);

                current.SetPadre(binaryNode.GetPadre());
                binaryNode.SetRight(null);
                binaryNode.SetLeft(null);
                count--;
                return(true);
            }

            return(false);
        }
예제 #7
0
        private bool Eliminate1(BinaryNode <T> node)
        {
            //left
            BinaryNode <T> TempLeft = node.GetPadre().GetLeft();

            //right
            BinaryNode <T> TempRight = node.GetPadre().GetRight();

            if (TempLeft == node)
            {
                node.GetPadre().SetLeft(null);
                count--;
                return(true);
            }

            if (TempRight == node)
            {
                node.GetPadre().SetRight(null);
                count--;
                return(true);
            }

            return(false);
        }
예제 #8
0
        private bool Delete1(BinaryNode <T> binaryNode)
        {
            //left son
            BinaryNode <T> TempLeft = binaryNode.GetPadre().GetLeft();

            //right son
            BinaryNode <T> TempRight = binaryNode.GetPadre().GetRight();

            if (TempLeft == binaryNode)
            {
                binaryNode.GetPadre().SetLeft(null);
                count--;
                return(true);
            }

            if (TempRight == binaryNode)
            {
                binaryNode.GetPadre().SetRight(null);
                count--;
                return(true);
            }

            return(false);
        }
예제 #9
0
        private void InsertBalance(BinaryNode <T> binaryNode, int balance)
        {
            while (binaryNode != null)
            {
                balance = (binaryNode.Balance += balance);

                if (balance == 0)
                {
                    return;
                }
                else if (balance == 2)
                {
                    if (binaryNode.GetLeft().Balance == 1)
                    {
                        RotateRight(binaryNode);
                    }
                    else
                    {
                        RotateLeftRight(binaryNode);
                    }

                    return;
                }
                else if (balance == -2)
                {
                    if (binaryNode.GetRight().Balance == -1)
                    {
                        RotateLeft(binaryNode);
                    }
                    else
                    {
                        RotateRightLeft(binaryNode);
                    }

                    return;
                }

                BinaryNode <T> parent = binaryNode.GetPadre();

                if (parent != null)
                {
                    balance = parent.GetLeft() == binaryNode ? 1 : -1;
                }

                binaryNode = parent;
            }
        }