delete by copy only for non-children nodes?

This is a discussion on delete by copy only for non-children nodes? within the C++ Programming forums, part of the General Programming Boards category; Fixed it, got a working randomDelete() function that finds the node based on the key portion, then zaps it from ...

  1. #16
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Fixed it, got a working randomDelete() function that finds the node based on the key portion, then zaps it from within the function, no passing pointers and more pointers. My new deleteRandomSymmetrically, which has to toggle between predecessors and successors with each successive delete, however, doesn't work. I'm dead-tired (it's almost 2 a.m.), so maybe I'm just not seeing the solution now, but do you have any ideas?

    Thanks again!
    Code:
        template<class T>
    void BinSearchTree<T>::deleteRandomSymmetrically()
    {
         if(z == 1)
         {
              z++;
         }
         else if(z == 2)
         {
              z--;
         }
        int elToDelete = rand()%countEls();
        int myInt = 1;//add 1 for node since I incremented with each child access
        Queue<BinSearchNode<T>*> queue;
        BinSearchNode<T> *p = root;
        if(p != 0)
        {
             queue.enqueue(p);
             while(!queue.empty())
             {
                 p = queue.dequeue();
                 if(p->left != 0)
                         queue.enqueue(p->left);
                         myInt++;
                 if(myInt == elToDelete)
                 {
                     if(z == 1)
                         {
                              if(p->right != 0)
                                  p = p->right;
                              while(p != 0 && p->left != 0)
                                  p = p->left;
                              delete p;    
                         }
                         else if(z == 2)
                         {
                              if(p->left != 0)
                                  p = p->left;
                              while(p != 0 && p->right != 0)
                                  p = p->right;
                              delete p;
                         }
                     
                 }
                     if(p->right != 0)
                         queue.enqueue(p->right);
                         myInt++;
                 if(myInt == elToDelete)
                 {
                     if(z == 1)
                         {
                              if(p->right != 0)
                                  p = p->right;
                              while(p != 0 && p->left != 0)
                                  p = p->left;
                              delete p;    
                         }
                         else if(z == 2)
                         {
                              if(p->left != 0)
                                  p = p->left;
                              while(p != 0 && p->right != 0)
                                  p = p->right;
                              delete p;
                         }
                     
                 }
             }
         }        
    }

  2. #17
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Hmmm, interesting, it's the same situation, the nodes that are supposed to be deleted now have a node->key value of a memory address.

  3. #18
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Nevermind, I got it, I'll send you a PM with the code if you're interested (don't want to post it here, as it took me a WHILE to get it, and don't want my classmates doing a wholesale copy of my hard work). Thanks again for the help ;-)

    -Patrick

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  2. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 10:17 PM
  3. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  4. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21