what to return ?

hi
i have a general question about a function that i'm using in a binary tree.

the function has to return the value of the father of the node containing the value n;
but if n is the value in the root, what should i return?
this function should return an int

thanks
If the values in the nodes are restricted to some subset of ints (e.g. INT_MIN is never stored on the tree), then you can return one of the forbidden values to indicate this condition (similar to how many functions of std::string return the special value std::string::npos)
Otherwise, you'd have to throw an exception, or, better, redesign.
ok thanks
same for example in this case right?

for example searching the max value in a tree

int max(node *ptr)
{
if(ptr!=NULL){

//search for the max

}
else return INT_MIN
}

is it correct?





For max search, yes, you could return INT_MIN as the special value.. or you could redesign to avoid getting called on null pointers in the first place.
returning INT_MIN actually causes an error, the compiler doesn't know INT_MIN.
is it contained in any special library?
INT_MIN requires #include <climits>

std::numeric_limits<int>::min() requires #include <limits>
Topic archived. No new replies allowed.