Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

c - finding sum of all non terminal node in bst


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
", 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
", 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...