Пример #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);
 }