Problem with pointer and recursive function

0

The code is this:

Node * search ( Node ** root, int mats ) {

    if ( ! ( * root ) ) {

        return NULL;

    } else {

        search ( & ( * root ) -> esq, mats );

        if ( ( * root ) -> person.mat == mats ) {

            return ( * root );

        }

        search ( & ( * root ) -> dir, mats );

    }

    // return NULL;

}
The problem is that the compiler points out that it is giving segmentation failure (I believe because of the return NULL of the end), but if I remove the return NULL from the end it points out that the function has no return; so if I leave it returning NULL it breaks the address I get inside else ... how to solve?

    
asked by anonymous 14.06.2017 / 02:31

3 answers

2

What about:

Node * search( Node ** root, int mats )
{
    Node * n = *root;
    Node * e = NULL;

    /* verifica o fim da arvore */
    if( !n )
        return NULL;

    /* verifica o noh atual */
    if( n->person.mat == mats )
        return n;

    /* faz pesquisa na esquerda */
    e = search( &(n->esq), mats );

    /* se encontrou ocorrencia na esquerda... */
    if(e)
        return e;

    /* faz pesquisa na direita */
    return search( &(n->dir), mats );
}

Simplifying:

Node * search( Node * root, int mats )
{
    Node * e = NULL;

    /* verifica o fim da arvore */
    if( !root )
        return NULL;

    /* verifica o noh atual */
    if( root->person.mat == mats )
        return root;

    /* faz pesquisa na esquerda */
    e = search( root->esq, mats );

    /* se encontrou ocorrencia na esquerda... */
    if(e)
        return e;

    /* faz pesquisa na direita */
    return search( root->dir, mats );
}
    
14.06.2017 / 15:40
2

The problem is that you do search(&(*root)->esq, mats); and search(&(*root)->dir, mats); and then throw away the return value of the recursion, so unless you look for the value that is in the root node, it will not do return.

You need a local variable of type Node * ; no else you mark the result of search() for this variable and check that it is null. If it is not, return it; otherwise, test the value of the current node and return it if it is correct; otherwise return the value of the second search() . This should work.

    
14.06.2017 / 15:01
1

The compiler does not do magic - if you want to return the value that the recursive call to search finds for the function that called search the first time, put this return explicitly in code. Below is the penultimate line:

Node * search ( Node ** root, int mats ) {
    if ( ! ( * root ) ) {
        return NULL;  
    }     

    if ( ( * root ) -> person.mat == mats ) {
            return ( * root );
        }
    return search ( & ( * root ) -> dir, mats );
}

Aspects changes I made are: if the root is null, the rest of the function is not executed - so it does not have to be inside a else block - this decreases the { } and indentation levels and improves readability. It also looks like you had an extra call to search before that second if .

And as a final note, the compiler never gives "segmentation fault" - the operating system gives a "segmentation fault" when it interrupts its program because it caused an illegal memory access. / p>     

14.06.2017 / 15:09