Thread: Won't Return pointer, i can't find why...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    63

    Won't Return pointer, i can't find why...

    howdy boyz n gals:

    Ok here is my dilemea, i'm using my FIND function to INSERT a given data value after another given data value in a tree. Say I want 3 to be the right child of 8. So i have a find function that will find 8 in the tree, but when i compile it won't work. core dump each time. It says that it can't convert. I don't know what this means. I am pretty sure i'm returning a pointer to the node containing 8. here is my code for both the find and the insert functions.

    Code:
    //my public class function declarations:
    template <class Comparable>
            void BinarySearchTree<Comparable>::insert( const Comparable & x )
            {
                insert( x, root );
            }
    //the private version
            template <class Comparable>
            void BinarySearchTree<Comparable>::
            insert( const Comparable & x, BinaryNode<Comparable> * & t ) const
            {
                if( t == NULL )
                    t = new BinaryNode<Comparable>( x, NULL, NULL );
                else if( x < t->element )
                    insert( x, t->left );
                else if( t->element < x )
                    insert( x, t->right );
                else
                    ;  // Duplicate; do nothing
            }
    
    //private find function returning pointer to T
        template <class Comparable>
            BinaryNode<Comparable> *
            BinarySearchTree<Comparable>::
            find( const Comparable & x, BinaryNode<Comparable> *t ) const
            {
                if( t == NULL )
                    return NULL;
                else if( x < t->element )
                    return find( x, t->left );
                else if( t->element < x )
                    return find( x, t->right );
                else
                    return t;    // Match
            }
    
    //public find function
        */
            template <class Comparable>
            const Comparable & BinarySearchTree<Comparable>::
                                     find( const Comparable & x ) const
            {
                return  find( x, root ) ;
            }
    //THESE ARE MY ACTUAL CLASS DECLARATIONS
    
    public:
     const Comparable & find( const Comparable & x ) const;
                void insert( const Comparable & x);
    
    private:
    	 
                void insert( const Comparable & x, BinaryNode<Comparable> * & t ) const;        		BinaryNode<Comparable> * find( const Comparable & x, BinaryNode<Comparable> *t ) const;
    thanks i fried my brain all nite and couldn't figure this out...
    SS3X

  2. #2
    Xcal82
    Guest

    I thought of this...

    I thought of this but what happens when you have a tree containing the same number more than once? That could happen, it won't be a BST but that could happen. I'm trying to add a pointer to the previous node.

  3. #3
    Unregistered
    Guest
    forget about that.. just tryn to get the first case done! lol forget duplicates for now. .anyway got the test in the mornin so no more programmin tonite..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM