コード例 #1
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;
                        }
                    }
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// search for a member in the member collection
 /// </summary>
 /// <param name="aMember"> member to search</param>
 /// <returns></returns>
 public bool search(Member aMember)
 {
     return(Search(aMember, root));
 }