Beispiel #1
0
        //Iterative Version
        public void InsertNode(int val)
        {
            AVLNode         temp        = root;
            Stack <AVLNode> parentStack = new Stack <AVLNode>();

            while (temp != null)
            {
                parentStack.Push(temp);
                if (val < temp.data)
                {
                    temp = temp.left;
                }
                else if (val > temp.data)
                {
                    temp = temp.right;
                }
                else
                {
                    return;
                }
            }
            temp = new AVLNode(val);

            if (parentStack.Count != 0)
            {
                AVLNode temp1 = parentStack.Pop();
                if (val < temp1.data)
                {
                    temp1.left = temp;
                }
                else
                {
                    temp1.right = temp;
                }
                Update(temp1);

                while (parentStack.Count != 0 && (temp1.bf != -2 && temp1.bf != 2))
                {
                    temp1 = parentStack.Pop();
                    Update(temp1);
                }

                if (parentStack.Count != 0)
                {
                    temp = parentStack.Pop();
                    if (temp.data > val)
                    {
                        temp.left = Balance(temp1);
                    }
                    else
                    {
                        temp.right = Balance(temp1);
                    }
                }
                else
                {
                    root = Balance(temp1);
                }
            }
            else
            {
                root = new AVLNode(val);
            }
        }
Beispiel #2
0
 public AVLTree(int data)
 {
     root = new AVLNode(data);
 }
Beispiel #3
0
 private AVLNode LeftRightCase(AVLNode node)
 {
     node.left = LeftRotation(node.left);
     return(RightRotation(node));
 }
Beispiel #4
0
 private AVLNode LeftLeftCase(AVLNode node)
 {
     return(RightRotation(node));
 }
Beispiel #5
0
 private AVLNode RightLeftCase(AVLNode node)
 {
     node.right = RightRotation(node.right);
     return(LeftRotation(node));
 }
Beispiel #6
0
 private AVLNode RightRightCase(AVLNode node)
 {
     return(LeftRotation(node));
 }