Thread: want to return a node using recursion

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    10

    want to return a node using recursion

    So the task is to find the node with minimum value of a binary tree (not binary search tree). the input is the pointer to the root of the tree. and i cannot make recursion work when i do if conditions. here is what i have
    Code:
    ​/*function 3-takses as input the pointer to the root of the tree and returns a pointer to the node with the minimum value*/
    CPPtr minimumvalue(CPPtr SP){    
    
    
        CPPtr min = NULL;    //node of minimum value
    
    
            if(SP== NULL){    // if there is a node, begin comparing
                return NULL;
            }
            else{
                if(SP->data<SP->left->data){    //if the node has smaller value than its left child
                    min = SP;                    //update node of minimum value
                }
                else min = SP->left;                //if the node has greater value than its left child 
                                                    //update node to the child
    
    
                if (min->data > SP->right->data){ //if the updated node has smaller value than right child
                    min = SP->right;                    //update node
                }
                
    
    
                    CPPtr minl = minimumvalue(SP->left);                //check for left subtree
                //    CPPtr minr = minimumvalue(SP->right);                //check for right subtree
            
                return min;
            }
    }
    no matter where i call my function i get errors like unhandled exception at some memory.
    anyone have idea on how to use recursion in this?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Before checking the value of "SP->left->data" You should check to make sure SP->left" is NOT NULL.
    The same for the right side.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Yog Sothoth!

    Please, for all that is good and decent in the world, don't `typedef' pointers. (*)

    You may see it as easier now, but imagine trying to work with several others each of whom has a different preference for how to style the names of such `typedef' pointers.

    Actually, let me just tell you: it is awful. Please don't be a part of the problem.

    *shrug*

    In any event, if the tree isn't a tree specifically organized nodes you must always visit every node (to consider every nodes value). You appear to be... never doing that.

    [Edit]
    This is simply extra; Tim is addressing your issue.
    [/Edit]

    Soma

    (*) Or bad and kinky if your that sort. (Call me!)
    Last edited by phantomotap; 03-27-2013 at 08:19 PM.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    Quote Originally Posted by stahta01 View Post
    Before checking the value of "SP->left->data" You should check to make sure SP->left" is NOT NULL.
    The same for the right side.

    Tim S.
    hi Tim, it seems there is a problem with my 1st if condition. now the code is giving NULL regardless.
    Code:
    CPPtr minimumvalue(CPPtr SP){	
    
    	CPPtr min = NULL;	//node of minimum value
    
    
    		if(SP== NULL){	// if there is a node, begin comparing
    			return NULL;
    		}
    		else{
    			if(SP->left!=NULL && SP->data<SP->left->data){	//if the node has smaller value than its left child
    				min = SP;					//update node of minimum value
    			}
    			else min = SP->left;				//if the node has greater value than its left child 
    												//update node to the child
    
    
    			if (SP->right!=NULL && min->data > SP->right->data){ //if the updated node has smaller value than right child
    				min = SP->right;					//update node
    			}
    			
    
    
    				 min = minimumvalue(SP->left);				//check for left subtree
    				 min = minimumvalue(SP->right);				//check for right subtree
    		
    			return min;
    		}
    }

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    Quote Originally Posted by phantomotap View Post
    O_o

    Yog Sothoth!

    Please, for all that is good and decent in the world, don't `typedef' pointers. (*)

    You may see it as easier now, but imagine trying to work with several others each of whom has a different preference for how to style the names of such `typedef' pointers.

    Actually, let me just tell you: it is awful. Please don't be a part of the problem.

    *shrug*

    In any event, if the tree isn't a tree specifically organized nodes you must always visit every node (to consider every nodes value). You appear to be... never doing that.

    [Edit]
    This is simply extra; Tim is addressing your issue.
    [/Edit]

    Soma

    (*) Or bad and kinky if your that sort. (Call me!)
    thanks for the advice! i will remember this when i code next time as this was my teachers idea to typedef

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Instead of

    CPPtr min = NULL; //node of minimum value
    Do
    CPPtr min = SP; //node of minimum value
    This means you always assign a value to min; your if logic did NOT always do this.

    Edit: Your code still has more logic bugs; but, you need to find then and fix them.

    Tim S.
    Last edited by stahta01; 03-27-2013 at 08:58 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    Quote Originally Posted by stahta01 View Post
    Instead of



    Do


    This means you always assign a value to min; your if logic did NOT always do this.

    Edit: Your code still has more logic bugs; but, you need to find then and fix them.

    Tim S.
    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quadtree Node -> pointers to the children node
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 06:38 PM
  2. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  3. NODE *RemoveDuplicates(NODE *p, NODE *q);
    By xnicky in forum C Programming
    Replies: 3
    Last Post: 12-03-2006, 05:30 PM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM