Exemple #1
0
 /// <summary>
 /// Add a member to a binary serach tree
 /// </summary>
 /// <param name="aMember"> member to add </param>
 /// <param name="ptr"> a binary serach tree node </param>
 private void add(Member aMember, BTreeNode ptr)
 {
     if (aMember.CompareTo(ptr.Member) < 0)
     {
         if (ptr.LChild == null)
         {
             ptr.LChild = new BTreeNode(aMember);
         }
         else
         {
             add(aMember, ptr.LChild);
         }
     }
     else
     {
         if (ptr.RChild == null)
         {
             ptr.RChild = new BTreeNode(aMember);
         }
         else
         {
             add(aMember, ptr.RChild);
         }
     }
 }
Exemple #2
0
 // pre: ptr != null
 // post: item is inserted to the binary search tree rooted at ptr
 private void Insert(Member item, BTreeNode ptr)
 {
     if (item.CompareTo(ptr.Item) < 0)
     {
         if (ptr.LChild == null)
         {
             ptr.LChild = new BTreeNode(item);
         }
         else
         {
             Insert(item, ptr.LChild);
         }
     }
     else
     {
         if (ptr.RChild == null)
         {
             ptr.RChild = new BTreeNode(item);
         }
         else
         {
             Insert(item, ptr.RChild);
         }
     }
 }
Exemple #3
0
 private bool Search(Member item, BTreeNode r)
 {
     if (r != null)
     {
         if (item.CompareTo(r.Member) == 0)
         {
             return(true);
         }
         else
         if (item.CompareTo(r.Member) < 0)
         {
             return(Search(item, r.LChild));
         }
         else
         {
             return(Search(item, r.RChild));
         }
     }
     else
     {
         return(false);
     }
 }
Exemple #4
0
        /// <summary>
        /// Delete a member from the mamber collection
        /// </summary>
        /// <param name="aMember"> member to delete </param>
        public void delete(Member aMember)
        {
            // search for item and its parent
            BTreeNode ptr    = root;          // search reference
            BTreeNode parent = null;          // parent of ptr

            while ((ptr != null) && (aMember.CompareTo(ptr.Member) != 0))
            {
                parent = ptr;
                if (aMember.CompareTo(ptr.Member) < 0)                 // move to the left child of ptr
                {
                    ptr = ptr.LChild;
                }
                else
                {
                    ptr = ptr.RChild;
                }
            }

            if (ptr != null)             // if the search was successful
            {
                number--;
                // case 3: item has two children
                if ((ptr.LChild != null) && (ptr.RChild != null))
                {
                    // find the right-most node in left subtree of ptr
                    if (ptr.LChild.RChild == null)                     // a special case: the right subtree of ptr.LChild is empty
                    {
                        ptr.Member = ptr.LChild.Member;
                        ptr.LChild = ptr.LChild.LChild;
                    }
                    else
                    {
                        BTreeNode p  = ptr.LChild;
                        BTreeNode pp = ptr;                         // parent of p
                        while (p.RChild != null)
                        {
                            pp = p;
                            p  = p.RChild;
                        }
                        // copy the item at p to ptr
                        ptr.Member = p.Member;
                        pp.RChild  = p.LChild;
                    }
                }
                else                 // cases 1 & 2: item has no or only one child
                {
                    BTreeNode c;
                    if (ptr.LChild != null)
                    {
                        c = ptr.LChild;
                    }
                    else
                    {
                        c = ptr.RChild;
                    }

                    // remove node ptr
                    if (ptr == root)                     //need to change root
                    {
                        root = c;
                    }
                    else
                    {
                        if (ptr == parent.LChild)
                        {
                            parent.LChild = c;
                        }
                        else
                        {
                            parent.RChild = c;
                        }
                    }
                }
            }
        }