예제 #1
0
 public BSTNode delete(int number)
 {
     if (number < this.number)
     {
         if (this.left != null)
         {
             this.left = this.left.delete(number);
         }
         return this;
     }
     else if (number > this.number)
     {
         if (this.right != null)
         {
             this.right = this.right.delete(number);
         }
         return this;
     }
     else
     {
         if (this.left == null)
         {
             return this.right;
         }
         else if (this.right == null)
         {
             return this.left;
         }
         else
         {
             this.number = this.right.min();
             this.right = right.delete(this.number);
             return this;
         }
     }
 }
예제 #2
0
 /**
  * Inserts the value into the binary search tree
  */
 public void insert(int number)
 {
     if (root == null)
     {
         root = new BSTNode(number);
     }
     else
     {
         root.insert(number);
     }
 }
예제 #3
0
 /**
  * Delete a number from the tree (if it exists)
  */
 public void delete(int number)
 {
     root = root.delete(number);
 }
예제 #4
0
 public void insertAvl(int qwe)
 {
     if (root == null)
     {
         root = new BSTNode(qwe);
     }
     else
     {
         root = root.insertAvl(qwe);
     }
 }
예제 #5
0
        public BSTNode insertAvl(int number)
        {
            if (number < this.number)
            {
                // Smaller value, insert it into the left subtree
                if (this.left == null)
                {
                    this.left = new BSTNode(number);
                    this.left.parent = this;
                }
                else
                    this.left = this.left.insertAvl(number);

                if (!isAvlBalanced())
                {
                    //Linkerkind is rechts zwaarder (extra rotatie)
                    if (height(this.left.left) >= height(this.left.right))
                    {
                        return this.rotateRight();
                    }
                    else
                    {
                        this.left = this.left.rotateLeft();
                        return this.rotateRight();
                    }
                }
            }
            else
            {
                // Larger value, insert it in the right subtree
                if (this.right == null)
                {
                    this.right = new BSTNode(number);
                    this.right.parent = this;
                }
                else
                    this.right = this.right.insertAvl(number);

                if (!isAvlBalanced())
                {
                    //Het rechterkind is links zwaarder (extra rotatie)
                    if (height(this.right.right) >= height(this.right.left))
                    {
                        return this.rotateLeft();
                    }
                    else
                    {
                        this.right = this.right.rotateRight();
                        return this.rotateLeft();
                    }
                }
            }

            return this;
        }
예제 #6
0
        public BSTNode insert(int number)
        {
            if (number < this.number)
            {
                // Smaller value, insert it into the left subtree
                if (this.left == null)
                    this.left = new BSTNode(number);
                else
                    this.left = this.left.insert(number);
            }
            else
            {
                // Larger value, insert it in the right subtree
                if (this.right == null)
                    this.right = new BSTNode(number);
                else
                    this.right = this.right.insert(number);
            }

            return this;
        }
예제 #7
0
 public int height(BSTNode cur)
 {
     if (cur == null)
     {
         return -1;
     }
     if (cur.left == null && cur.right == null)
     {
         return 0;
     }
     else if (cur.left == null)
     {
         return 1 + height(cur.right);
     }
     else if (cur.right == null)
     {
         return 1 + height(cur.left);
     }
     else
     {
         return 1 + Math.Max(height(cur.left), height(cur.right));
     }
 }