Пример #1
0
        /// <summary>
        /// Searches an object in the tree
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public virtual ComparableObject Find(ComparableObject obj)
        {
            ComparableObject comparableObject;

            if (base.IsEmpty)
            {
                comparableObject = null;
            }
            else
            {
                int i = obj.CompareTo(Key);
                if (i == 0)
                {
                    comparableObject = Key;
                }
                else if (i < 0)
                {
                    comparableObject = Left.Find(obj);
                }
                else
                {
                    comparableObject = Right.Find(obj);
                }
            }
            return(comparableObject);
        }
Пример #2
0
        /// <summary>
        /// Removes an object from the tree
        /// </summary>
        /// <param name="obj"></param>
        public virtual void Withdraw(ComparableObject obj)
        {
            if (base.IsEmpty)
            {
                throw new ArgumentException("object not found");
            }
            int i = obj.CompareTo(Key);

            if (i == 0)
            {
                if (!Left.IsEmpty)
                {
                    ComparableObject comparableObject1 = Left.Max;
                    mKey = comparableObject1;
                    Left.Withdraw(comparableObject1);
                }
                else if (!Right.IsEmpty)
                {
                    ComparableObject comparableObject2 = Right.Min;
                    mKey = comparableObject2;
                    Right.Withdraw(comparableObject2);
                }
                else
                {
                    base.DetachKey();
                }
            }
            else if (i < 0)
            {
                Left.Withdraw(obj);
            }
            else
            {
                Right.Withdraw(obj);
            }
            Balance();
        }
Пример #3
0
 /// <summary>
 /// Inserts an object in the tree
 /// </summary>
 /// <param name="obj"></param>
 public virtual void Insert(ComparableObject obj)
 {
     if (base.IsEmpty)
     {
         base.AttachKey(obj);
     }
     else
     {
         int i = obj.CompareTo(Key);
         if (i == 0)
         {
             throw new ArgumentException("duplicate mKey");
         }
         if (i < 0)
         {
             Left.Insert(obj);
         }
         else
         {
             Right.Insert(obj);
         }
     }
     Balance();
 }