xSwampy
xSwampy

Reputation: 35

bst segmentation fault search

I'm working on BST assignment and I have made all the implementations; however, when I call the bool insert(int i) function, segmentation fault(core dumped) appears. Can you please find what I should change to get rid of this fault?

const treenode* find( const treenode* n, int i ){
    if(n->val==i)
        return n;
    else if(n->val<i)
        return find(n->right, i);    //if our key is less than given node it goes to the left node through recursion function
    else if(n->val>i)
        return find(n->left, i);   //the same thing here, if it is bigger - it goes right through recursion
    else
        return n;
}

treenode** find( treenode** n, int i ){
    if((*n)->val==i)
        return n;
    else if((*n)->val<=i)
        return find(&((*n)->right), i);
    else if((*n)->val>=i)
        return find(&((*n)->left), i);
    else
        return n;
}const treenode* find( const treenode* n, int i ){
    if(n== nullptr)
        return n;
    else if(n->val==i)
        return n;
    else if(n->val>i)
        return find(n->left, i);    //if our key is less than given node it goes to the left node through recursion function
    else if(n->val<i)
        return find(n->right, i);   //the same thing here, if it is bigger - it goes right through recursion
}

treenode** find( treenode** n, int i ){
    if((*n)->val==i)
        return n;
    else if((*n)->val<=i)
        return find(&((*n)->right), i);
    else if((*n)->val>=i)
        return find(&((*n)->left), i);
    else
        return n;
}

bool set::insert( int i ) {
        treenode** res=find(&tr, i);
        if(*res==nullptr) {
            *res = new treenode(i);
            return true;
        }
        return false;
}

Upvotes: 1

Views: 45

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

This function

treenode** find( treenode** n, int i ){
    if((*n)->val==i)
        return n;
    else if((*n)->val<=i)
        return find(&((*n)->right), i);
    else if((*n)->val>=i)
        return find(&((*n)->left), i);
    else
        return n;
}c

can invoke undefined behavior when the pointer *n is equal to nullptr that is when the binary search tree is empty.

The function can look the following way

treenode ** find( treenode **n, int i )
{
    if ( *n == nullptr )
    {
        return n;
    }
    else if ( i < ( *n )->val )
    {
        return find( &( *n )->left, i );
    }
    else if ( ( *n )->val < i )
    {
        return find( &( *n )->right, i );
    }
    else
    {
        return n;
    }
}

and the other function find that shall be a constant member function can look like

const treenode * find( treenode *n, int i ) const
{
    if ( n == nullptr )
    {
        return n;
    }
    else if( i < n->val )
    {
        return find( n->left, i );
    }
    else if( n->val < i )
    {
        return find( n->right, i );
    }
    else
    {
        return n;
    }
}

I suppose that the constructor used in this statement

*res = new treenode(i);

sets the data members left and right to nullptr.

Upvotes: 0

Related Questions