Thread: delete by copy only for non-children nodes?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Sorry, I cut the deleteRandom() function off, here it is:

    Code:
    template<class T>
    void BinSearchTree<T>::deleteRandom()
    {     
    	vector<int> myVector;
    	Stack<BinSearchNode<T>*> pushToVector;
    	BinSearchNode<T> *p = root;
    	if(p != 0)
    	{
    		pushToVector.push(p);
    	    while(!pushToVector.empty())
    	    {
    	  	    p = pushToVector.pop();
    		    myVector.push_back(p->key);
    		    if(p->right != 0)
    			    pushToVector.push(p->right);
    		    if(p->left != 0)
    			    pushToVector.push(p->left);
    	    }
    	}
    	int elToDelete = rand()%myVector.size();
    	cout << myVector[(elToDelete-1)] << " deleted" << endl;
    	int keySearch = myVector[(elToDelete-1)];
        Queue<BinSearchNode<T>*> myQueue;
        BinSearchNode<T> *q = root;
    	if(q != 0)
    	{
    		myQueue.enqueue(q);
    		while(!myQueue.empty())
            {
    			q = myQueue.dequeue();
    			if(q->key == keySearch)
    			{
                          cout << "here!" << endl;
                    deleteByCopying(q); //kaboom!
                }
                if(q->left != 0)
    				myQueue.enqueue(q->left);
    			if(q->right != 0)
    				myQueue.enqueue(q->right);
    		}
    	}
    	
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    for example, 8 16 32 14 21, and then after deleteRandom called, and 16 deleted, display() will produce 8 468834 32 14 21. What could that mean? Very interesting, whatever it is. Here's how I have it set up: in int main(), (code attached as .cpp file if it will make more sense that way)
    I suspect that has to do with this error:
    Code:
    	int elToDelete = rand()%myVector.size();
    	cout << myVector[(elToDelete-1)] << " deleted" << endl;
    	int keySearch = myVector[(elToDelete-1)];
    The '-1' shouldn't be there.
    A%X returns a number between 0 and X-1. So by doing '-1', you're getting a number between -1 and X-2, which is insuitable for array indexing.

    That doesn't fix the main problem though I'm afraid.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    The '-1' shouldn't be there.
    A%X returns a number between 0 and X-1. So by doing '-1', you're getting a number between -1 and X-2, which is insuitable for array indexing.
    Good eye, I had forgotten about the x-1 rule, or maybe overlooked it.

    That doesn't fix the main problem though I'm afraid.
    Any thoughts on what in the world is causing this error and strange output?

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, 11: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, 05:50 PM