예제 #1
0
        public void InsertNode(int dataNode)
        {
            if (data > dataNode)
            {
                //InsertNode(root.left, dataNode);
                if (left == null)
                {
                    NodeAVL node = new NodeAVL(dataNode);
                    left = node;
                    return;
                }

                else
                {
                    left.InsertNode(dataNode);
                }
            }
            if (data < dataNode)
            {
                //InsertNode(root.right, dataNode);
                if (right == null)
                {
                    NodeAVL node = new NodeAVL(dataNode);
                    right = node;
                    return;
                }
                else
                {
                    right.InsertNode(dataNode);
                }
            }
        }
예제 #2
0
        void RightRotation(NodeAVL P, NodeAVL Q)
        {
            P.left  = Q.left;
            Q.left  = Q.right;
            Q.right = P.right;
            P.right = Q;
            int temp = P.data;

            P.data = Q.data;
            Q.data = temp;
        }
예제 #3
0
 int HeightTree(NodeAVL root)
 {
     if (root == null)
     {
         return(0);
     }
     else
     {
         return(1 + Math.Max(HeightTree(root.left), HeightTree(root.right)));
     }
 }
예제 #4
0
 public int CheckBalanceNode(NodeAVL node)
 {
     if (node == null)
     {
         return(0);
     }
     else
     {
         return(HeightTree(node.right) - HeightTree(node.left));
     }
 }
예제 #5
0
 // find the most left
 NodeAVL FindDeleteMostLeft(NodeAVL left)
 {
     if (left.right == null)
     {
         return(left);
     }
     else
     {
         return(FindDeleteMostLeft(left.right));
     }
 }
예제 #6
0
 int HeightTree(NodeAVL node)
 {
     if (node == null)
     {
         return(0);
     }
     else
     {
         return(1 + Math.Max(HeightTree(node.left), HeightTree(node.right)));
     }
 }
예제 #7
0
        public void CheckBalance(NodeAVL node, int newData)
        {
            if (node != null)
            {
                if (node.data == newData)
                {
                    return;
                }
                int checkPrimary = CheckBalanceNode(node);
                //int kt = node.data;
                if (checkPrimary >= 2)
                {
                    //- mat can bang right right
                    int checkSecondary = CheckBalanceNode(node.right);
                    if (checkSecondary > 0)
                    {
                        //mat can bang RR.
                        BalanceRR(node);
                        return;
                    }
                    else
                    {
                        //mat can bang RL
                        BalanceRL(node);
                        return;
                    }
                }
                if (checkPrimary <= -2)
                {
                    int checkSecondary = CheckBalanceNode(node.left);
                    if (checkSecondary > 0)
                    {
                        BalanceLR(node);
                        return;
                        //mat can bang LR
                    }
                    else
                    {
                        //mat can bang LL
                        BalanceLL(node);
                    }
                }
                //chua mat can bang

                if (newData > node.data)
                {
                    CheckBalance(node.right, newData);
                }
                else
                {
                    CheckBalance(node.left, newData);
                }
            }
        }
예제 #8
0
 public void InsertNode(int dataNode)
 {
     if (root == null)
     {
         NodeAVL temp = new NodeAVL(dataNode);
         root = temp;
     }
     else
     {
         root.InsertNode(dataNode);
         root.CheckBalance(root, dataNode);
     }
 }
예제 #9
0
        //---

        public bool Search(int x)
        {
            NodeAVL result = Search(root, x);

            if (result != null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #10
0
 int CheckCaseDelete(NodeAVL P)
 {
     if (P.left != null && P.right != null)
     {
         return(2);
     }
     if (P.left == null && P.right == null)
     {
         return(0);
     }
     if (P.left != null)
     {
         return(-1);
     }
     return(1);
 }
예제 #11
0
        bool SearchDelete(NodeAVL P, int x)
        {
            int numberChild;

            if (P.data == x)
            {
                numberChild = -2;
                DeleteGeneration(P, -2, numberChild);
                return(true);
            }
            if (x < P.data)
            {
                if (P.left != null)
                {
                    if (x == P.left.data)
                    {
                        numberChild = CheckCaseDelete(P.left);
                        DeleteGeneration(P, -1, numberChild);
                        return(true);
                    }
                    else
                    {
                        return(SearchDelete(P.left, x));
                    }
                }
                return(false);
            }
            else
            {
                if (P.right != null)
                {
                    if (x == P.right.data)
                    {
                        numberChild = CheckCaseDelete(P.right);
                        DeleteGeneration(P, 1, numberChild);
                        return(true);
                    }
                    else
                    {
                        return(SearchDelete(P.right, x));
                    }
                }
                return(false);
            }
        }
예제 #12
0
 int MinHeightLeaf(NodeAVL root)
 {
     if (root == null)
     {
         return(0);
     }
     else
     {
         if (root.left == null)
         {
             return(1 + MinHeightLeaf(root.right));
         }
         if (root.right == null)
         {
             return(1 + MinHeightLeaf(root.left));
         }
         return(1 + Math.Min(MinHeightLeaf(root.left), MinHeightLeaf(root.right)));
     }
 }
예제 #13
0
 NodeAVL Search(NodeAVL root, int x)
 {
     if (root != null)
     {
         if (x < root.data)
         {
             return(Search(root.left, x));
         }
         else
         {
             if (x > root.data)
             {
                 return(Search(root.right, x));
             }
             else
             {
                 return(root);
             }
         }
     }
     return(null);
 }
예제 #14
0
 public NodeAVL()
 {
     data  = 0;
     left  = null;
     right = null;
 }
예제 #15
0
 public CreateAVL()
 {
     root = null;
 }
예제 #16
0
 void BalanceLR(NodeAVL P)
 {
     LeftRotation(P.left, P.left.right);
     RightRotation(P, P.left);
 }
예제 #17
0
 void BalanceLL(NodeAVL P)
 {
     RightRotation(P, P.left);
 }
예제 #18
0
 public NodeAVL(int dataNode)
 {
     this.data  = dataNode;
     this.left  = null;
     this.right = null;
 }
예제 #19
0
 void BalanceRL(NodeAVL P)
 {
     RightRotation(P.right, P.right.left);
     LeftRotation(P, P.right);
 }
예제 #20
0
 void DeleteGeneration(NodeAVL parent, int kind, int numberChild)
 {
     //delete 0 child
     if (numberChild == 0)
     {
         if (kind == 1)
         {
             int temp = parent.right.data;
             parent.right = null;
             root.CheckBalance(root, temp);
         }
         else
         {
             int temp = parent.left.data;
             parent.left = null;
             root.CheckBalance(root, temp);
         }
         return;
     }
     // delete 1 child
     if (numberChild == 1)
     {
         if (kind == 1)
         {
             parent.right = parent.right.right;
             root.CheckBalance(root, parent.right.data);
             return;
         }
         else
         {
             parent.right = parent.right.left;
             root.CheckBalance(root, parent.right.data);
             return;
         }
     }
     if (numberChild == -1)
     {
         if (kind == 1)
         {
             parent.right = parent.right.left;
             root.CheckBalance(root, parent.right.data);
             return;
         }
         else
         {
             parent.left = parent.left.left;
             root.CheckBalance(root, parent.left.data);
             return;
         }
     }
     //--delete 2 child
     if (numberChild == -2)
     {
         NodeAVL mostLeft = FindDeleteMostLeft(parent.left);
         int     temp     = mostLeft.data;
         Delete(mostLeft.data);
         mostLeft.data = parent.data;
         parent.data   = temp;
         root.CheckBalance(root, parent.left.data);
         return;
     }
     if (kind == 1)
     {
         NodeAVL mostLeft = FindDeleteMostLeft(parent.right.left);
         int     temp     = mostLeft.data;
         Delete(mostLeft.data);
         mostLeft.data     = parent.right.data;
         parent.right.data = temp;
         root.CheckBalance(root, parent.right.left.data);
     }
     else
     {
         NodeAVL mostLeft = FindDeleteMostLeft(parent.left.left);
         int     temp     = mostLeft.data;
         Delete(mostLeft.data);
         mostLeft.data    = parent.left.data;
         parent.left.data = temp;
         root.CheckBalance(root, parent.left.left.data);
     }
 }
예제 #21
0
        //----



        //-- handle imbalance
        void BalanceRR(NodeAVL P)
        {
            LeftRotation(P, P.right);
        }
예제 #22
0
 public CreateAVL(int dataRoot)
 {
     root = new NodeAVL(dataRoot);
 }