Beispiel #1
0
 private void FindNode(ref BinarySearchTreeNode <T> prev, ref BinarySearchTreeNode <T> current, T key)
 {
     while (current != null)
     {
         int comp = current.Key.CompareTo(key);
         if (comp == 0)
         {
             break;
         }
         else if (comp > 0)
         {
             prev    = current;
             current = current.Left;
         }
         else
         {
             prev    = current;
             current = current.Right;
         }
     }
 }
Beispiel #2
0
        public bool Remove(T key)
        {
            if (Root == null)
            {
                return(false);
            }
            else
            {
                BinarySearchTreeNode <T> prev    = null;
                BinarySearchTreeNode <T> current = Root;

                FindNode(ref prev, ref current, key);
                if (current == null) // if there is no deleteNode
                {
                    return(false);
                }
                RemoveNode(prev, current);

                return(true);
            }
        }
Beispiel #3
0
        public bool Search(T key)
        {
            BinarySearchTreeNode <T> node = Root;

            while (node != null)
            {
                int comp = node.Key.CompareTo(key);

                if (comp == 0) //This instance is equal to value.
                {
                    return(true);
                }
                else if (comp > 0) //This instance is less than value.
                {
                    node = node.Left;
                }
                else //This instance is greater than value.
                {
                    node = node.Right;
                }
            }

            return(false);
        }