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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
CPPtr minimumvalue(CPPtr SP){	

	CPPtr min = SP;	//node of minimum value

		if(SP!= NULL){	// if there is a node, begin comparing
	//		return SP;
	//	}
	//	else{

			min = minimumvalue(SP->left);				//check for left subtree
			min = minimumvalue(SP->right);				//check for right subtree
			if((SP->left!=NULL && SP->data<SP->left->data)||SP->left==NULL){	//if the node has smaller value than its left child
				min = SP;					//update node of minimum value
			}
			else if (SP->left!=NULL){
				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
			}
			


		}
		return min;
}


now my code only does it once, the recursion seems useless even if i called it. If i call them at the end of the if (SP!=NULL ){} the function returns NULL
Topic archived. No new replies allowed.