# find min and max value of BST in C

Page 1 of 1

## 2 Replies - 5037 Views - Last Post: 04 October 2011 - 06:31 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=249778&amp;s=0b5f4d94ff773ac0454dbab6ec644041&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 assert(C)

Reputation: 1
• Posts: 71
• Joined: 20-July 11

# find min and max value of BST in C

Posted 04 October 2011 - 05:06 AM

Hey Guys,

I have made these two functions two find the min and max value of a BST. They dont seem to work I want the functions to be recursive; When I run my code it will not give the minium value.

``` int minVal(tree Ptr){
int minValue=0;
if (Ptr != NULL){
minVal(Ptr->left);
minValue=Ptr->data;
}
return minValue;
}

```

Thanks
Is This A Good Question/Topic? 0

## Replies To: find min and max value of BST in C

### #2 sepp2k

• D.I.C Lover

Reputation: 2271
• Posts: 3,486
• Joined: 21-June 11

## Re: find min and max value of BST in C

Posted 04 October 2011 - 05:47 AM

You're calling minVal recursively, but you're not doing anything with the result of the recursive call. You should return the result of recursive call if Ptr->left is not null, otherwise you should return the data of the current node.

### #3 baavgai

• Dreaming Coder

Reputation: 6339
• Posts: 13,537
• Joined: 16-October 07

## Re: find min and max value of BST in C

Posted 04 October 2011 - 06:31 AM

```int minVal(tree Ptr){
// there variable here is kind of pointless
// you could just return it
// int minValue=0;
if (Ptr != NULL){
// visit the left child
minVal(Ptr->left);
// now, completely ignore
// what might have been returned from the
// call and return the current value
return minValue;
}
}

```

So, it looks like you'll return the value of the first node you visit.

While checking for NULL down one level is classic in recursion, I'd recommend you start like:
```int minVal(tree Ptr){
if (Ptr->left == NULL) {
// you're there, now what do you do?

```