The minimum value of a binary tree question
- From: Chad <cdalten@xxxxxxxxx>
- Date: Mon, 18 Jan 2010 08:52:57 -0800 (PST)
At the following url...
http://cslibrary.stanford.edu/110/BinaryTrees.html
They have the following as a solution to finding the min value of a
binary tree...
4. minValue() Solution (C/C++)
/*
Given a non-empty binary search tree,
return the minimum data value found in that tree.
Note that the entire tree does not need to be searched.
*/
int minValue(struct node* node) {
struct node* current = node;
// loop down to find the leftmost leaf
while (current->left != NULL) {
current = current->left;
}
return(current->data);
}
Why do they do 'current->left != NULL' in the while loop instead of
something like 'current != NULL'? Ie, something like the
following....
int minValue(struct node* node) {
struct node* current = node;
// loop down to find the leftmost leaf
while (current != NULL) {
current = current->left;
}
return(current->data);
}
.
- Follow-Ups:
- Re: The minimum value of a binary tree question
- From: Daniel Pitts
- Re: The minimum value of a binary tree question
- Prev by Date: Re: Batch malloc
- Next by Date: Re: The minimum value of a binary tree question
- Previous by thread: Batch malloc
- Next by thread: Re: The minimum value of a binary tree question
- Index(es):
Relevant Pages
|