Search code examples
crecursionstructbinary-search-treefunction-definition

finding sum of all non terminal node in bst


how can i correct this program?

giving me output as 0. please help me out. i am new to programming. i have tried with input as 10,15,9,8 its giving me output as zero or some large number. i know this is a silly question but, i couldnt find the answer



void findProductSum(struct node* root, int sum)
{

    if (root == NULL || (root->left == NULL&& root->right == NULL))
        return;


    if (root->left != NULL || root->right != NULL)
    {

        sum += root->data;
        
    }

    findProductSum(root->left,sum);
    findProductSum(root->right,sum);
    return sum;

}```

Solution

  • The function has the return type void. So for example this statement

    return sum;
    

    does not make a sense.

    This if statement

    if (root->left != NULL || root->right != NULL)
    

    is redundant. You already checked that one of the pointers is not equal to NULL in this statement

    if (root == NULL || (root->left == NULL&& root->right == NULL))
    

    Also within the function there is being changed its local variable sum (parameters are function local variables).

    As a result these calls

    findProductSum(root->left,sum);
    findProductSum(root->right,sum);
    

    do not have an effect.

    The function can be defined the following way

    long long int findProductSum( const struct node* root )
    {
        return root == NULL || ( root->left == NULL && root->right == NULL )
               ? 0ll
               : root->data + findProductSum( root->left ) + findProductSum( root->right );
    }                            
    

    And called like

    long long int sum = findProductSum( root );
    

    where root is a pointer to the root node declared in the caller.

    Then you can output the obtained value like

    printf( "%lld\n", sum );
    

    Here is a demonstrative program

    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
        int data;
        struct node *left;
        struct node *right;
    };
    
    int insert( struct node **root, int data )
    {
        struct node *new_node = malloc( sizeof( struct node ) );
        int success = new_node != NULL;
        
        if ( success )
        {
            new_node->data  = data;
            new_node->left  = NULL;
            new_node->right = NULL;
            
            while ( *root != NULL )
            {
                if ( data < ( *root )->data )
                {
                    root = &( *root )->left;
                }
                else
                {
                    root = &( *root )->right;
                }
            }
            
            *root = new_node;
        }
        
        return success;
    }
    
    long long int findProductSum( const struct node* root )
    {
        return root == NULL || ( root->left == NULL && root->right == NULL )
               ? 0ll
               : root->data + findProductSum( root->left ) + findProductSum( root->right );
    }  
    
    int main(void) 
    {
        struct node *root = NULL;
        int data[] = { 10, 15, 9, 8 };
        const size_t N = sizeof( data ) / sizeof( *data );
        
        for ( size_t i = 0; i < N; i++ ) insert( &root, data[i] );
        
        long long int sum = findProductSum( root );
        
        printf( "The sum of non-terminal nodes is equal to %lld\n", sum );
        
        return 0;
    }
    

    The program output is

    The sum of non-terminal nodes is equal to 19
    

    Nodes with values 15 and 8 are terminal nodes. So their values are not added to the result sum.