Beispiel #1
0
 public virtual void Remove(int value, BST parent)
 {
     if (value < this.value)
     {
         if (left != null)
         {
             left.Remove(value, this);
         }
     }
     else if (value > this.value)
     {
         if (right != null)
         {
             right.Remove(value, this);
         }
     }
     else
     {
         if (left != null && right != null)
         {
             this.value = right.getMinValue();
             right.Remove(this.value, this);
         }
         else if (parent == null)
         {
             if (left != null)
             {
                 this.value = left.value;
                 right      = left.right;
                 left       = left.left;
             }
             if (right != null)
             {
                 this.value = right.value;
                 left       = right.left;
                 right      = right.right;
             }
             else
             {
             }
         }
         else if (parent.left == this)
         {
             parent.left = left != null ? left : right;
         }
         else if (parent.right == this)
         {
             parent.right = left != null ? left : right;
         }
     }
 }
        public BSTConstruction(int value)
        {
            var bst = new BST(value);

            bst.Insert(5);
            bst.Insert(5);
            bst.Insert(13);
            bst.Insert(2);

            Console.WriteLine(bst.Contains(5));
            bst.Remove(2, null);
        }