Exemple #1
0
 public BSTNode(int number, BSTNode left, BSTNode right, BSTNode parent)
 {
     Number = number;
     Left = left;
     Right = right;
     Parent = parent;
 }
Exemple #2
0
 public void balance()
 {
     if (getBalance() > 1)
     {
         if (getBalance() == 2)
         {
             Root = Root.rotateLeft();
         }
         else
         {
             Root.balance();
         }
     }
     if (getBalance() < -1)
     {
         if (getBalance() == -2)
         {
             Root = Root.rotateRight();
         }
         else
         {
             Root.balance();
         }
     }
 }
Exemple #3
0
 public BSTNode(int number, BSTNode parent, BST tree)
 {
     Number = number;
     Left = null;
     Right = null;
     Parent = parent;
     Tree = tree;
 }
Exemple #4
0
        public void balanceAt(BSTNode node, int balance)
        {
            if (balance == 2) //right outweighs
            {
                int rightBalance = 0;

                if(node.Right != null)
                    rightBalance = node.Right.getBalance();

                if (rightBalance == 1 || rightBalance == 0)
                {
                    //Left rotation needed
                    Root = node.rotateLeft();
                }
                else if (rightBalance == -1)
                {
                    //Right rotation needed
                    node.Right.rotateRight();

                    //Left rotation needed
                    Root = node.rotateLeft();
                }
            }
            else if (balance == -2) //left outweighs
            {
                int leftBalance = 0;

                if(node.Left != null)
                    leftBalance = node.Left.getBalance();

                if (leftBalance == 1)
                {
                    //Left rotation needed
                    node.Left.rotateLeft();

                    //Right rotation needed
                    Root = node.rotateRight();
                }
                else if (leftBalance == -1 || leftBalance == 0)
                {
                    //Right rotation needed
                    Root = node.rotateRight();
                }
            }
        }
Exemple #5
0
 public void insert(int number)
 {
     if (number < Number)
     {
         if (Left == null)
         {
             Left = new BSTNode(number, this, Tree);
         }
         else
         {
             Left.insert(number);
         }
     }
     else
     {
         if (Right == null)
         {
             Right = new BSTNode(number, this, Tree);
         }
         else
         {
             Right.insert(number);
         }
     }
 }
Exemple #6
0
 public void insert(int number)
 {
     if (Root == null)
     {
         Root = new BSTNode(number, null, this);
     }
     else
     {
         Root.insert(number);
     }
 }
Exemple #7
0
 public void delete(int number)
 {
     if (Root != null)
     {
         Root = Root.delete(number);
     }
 }