コード例 #1
0
ファイル: AVLTree.cs プロジェクト: Manevle16/UTD2018Homework
        public void setAndCheckBalanceAndHeights(AVLNode curNode)
        {
            int insertedKey = curNode.Key;

            //Cycle through all the nodes until the root is reached
            while (curNode.Parent != null)
            {
                AVLNode parent = curNode.Parent;
                curNode = curNode.Parent;


                //Set balance and height of current node's parent
                setBalanceAndHeight(parent);
                //Check for imbalance
                if (Math.Abs(parent.Balance) > 1)
                {
                    Console.Write("Imbalance occured at inserting ISBN " + insertedKey + ";");
                    //Check if left imbalance, else right imbalance
                    if (parent.Balance > 1)
                    {
                        AVLNode leftChild = parent.LeftChild;

                        //Check if left subtree heavy, else right subtree heavy
                        if (leftChild.Balance > 0)
                        {
                            Console.WriteLine("fixed in right rotation");
                            AVLNode temp = leftChild.RightChild;

                            leftChild.takeParent(parent);
                            leftChild.setRightChild(parent);
                            parent.setLeftChild(temp);

                            setBalanceAndHeight(parent);
                            setBalanceAndHeight(temp);
                            setBalanceAndHeight(leftChild);

                            if (parent == root)
                            {
                                root = leftChild;
                            }
                            curNode = leftChild;
                        }
                        else
                        {
                            Console.WriteLine("fixed in left right rotation");
                            AVLNode leftRightChild = leftChild.RightChild;
                            AVLNode temp           = leftRightChild.LeftChild;

                            leftRightChild.takeParent(leftChild);
                            leftRightChild.setLeftChild(leftChild);
                            leftRightChild.LeftChild.setRightChild(temp);

                            setBalanceAndHeight(temp);
                            setBalanceAndHeight(leftChild);

                            temp = leftRightChild.RightChild;
                            leftRightChild.takeParent(parent);
                            leftRightChild.setRightChild(parent);
                            parent.setLeftChild(temp);

                            setBalanceAndHeight(temp);
                            setBalanceAndHeight(parent);
                            setBalanceAndHeight(leftRightChild);

                            if (parent == root)
                            {
                                root = leftRightChild;
                            }

                            curNode = leftRightChild;
                        }
                    }
                    else
                    {
                        AVLNode rightChild = parent.RightChild;

                        //Check if left subtree heavy, else right subtree heavy
                        if (rightChild.Balance > 0)
                        {
                            Console.WriteLine("fixed in right left rotation");
                            AVLNode rightLeftChild = rightChild.LeftChild;
                            AVLNode temp           = rightLeftChild.RightChild;

                            rightLeftChild.takeParent(rightChild);
                            rightLeftChild.setRightChild(rightChild);
                            rightLeftChild.RightChild.setLeftChild(temp);

                            setBalanceAndHeight(temp);
                            setBalanceAndHeight(rightChild);

                            temp = rightLeftChild.LeftChild;
                            rightLeftChild.takeParent(parent);
                            rightLeftChild.setLeftChild(parent);
                            parent.setRightChild(temp);

                            setBalanceAndHeight(temp);
                            setBalanceAndHeight(parent);
                            setBalanceAndHeight(rightLeftChild);

                            if (parent == root)
                            {
                                root = rightLeftChild;
                            }

                            curNode = rightLeftChild;
                        }
                        else
                        {
                            Console.WriteLine("fixed in left rotation");
                            AVLNode temp = rightChild.LeftChild;

                            rightChild.takeParent(parent);
                            rightChild.setLeftChild(parent);
                            parent.setRightChild(temp);

                            setBalanceAndHeight(parent);
                            setBalanceAndHeight(temp);
                            setBalanceAndHeight(rightChild);

                            if (parent == root)
                            {
                                root = rightChild;
                            }

                            curNode = rightChild;
                        }
                    }
                }
            }
        }