Example #1
0
 private void Transplant(BinarySearchTreeNode <T> a, BinarySearchTreeNode <T> b)
 {
     if (a.Parent == null)
     {
         Root = b;
     }
     else if (a == a.Parent.LeftChild)
     {
         a.Parent.LeftChild = b;
     }
     else
     {
         a.Parent.RightChild = b;
     }
     if (b != null)
     {
         b.Parent = a.Parent;
     }
 }
        public bool Contains(T data)
        {
            BinarySearchTreeNode <T> current = root;
            int result;

            while (current != null)
            {
                result = comparer.Compare(current.Value, data);

                if (result == 0)
                {
                    return(true);
                }
                else if (result > 0)
                {
                    current = current.Left;
                }
                else if (result < 0)
                {
                    current = current.Right;
                }
            }
            return(false);
        }
Example #3
0
 public void Delete(BinarySearchTreeNode <T> toDelete)
 {
     if (toDelete.LeftChild == null)
     {
         Transplant(toDelete, toDelete.RightChild);
     }
     else if (toDelete.RightChild == null)
     {
         Transplant(toDelete, toDelete.LeftChild);
     }
     else
     {
         var rightMin = Minimum(toDelete.RightChild);
         if (rightMin.Parent != toDelete)
         {
             Transplant(rightMin, rightMin.RightChild);
             rightMin.RightChild        = toDelete.RightChild;
             rightMin.RightChild.Parent = rightMin;
         }
         Transplant(toDelete, rightMin);
         rightMin.LeftChild        = toDelete.LeftChild;
         rightMin.LeftChild.Parent = rightMin;
     }
 }
 public BinarySearchTree()
 {
     root = null;
 }