### BST first minimum numbers

Question : why does this code work for first minimum numbers work when i write the two conditions in one if statement but doesn't work when i
write it in two if statements the two examples are given as follows the first one works and the second doesn't the whole code is given for
easy implementation below.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````void FirstMin(Tree *temp ) { if(temp!=NULL && count <5) { FirstMin(temp->left); cout<data<Right); } } void FirstMin(Tree *temp ) { if (count <5) { temp==NULL; //return; this doesn't work too } if(temp!=NULL) { FirstMin(temp->left); cout<data<Right); } } #include #include using namespace std; struct Tree { int data; Tree *left; Tree *Right; }; Tree * root =NULL; Tree * insert(Tree *temp , int elm) { if(temp==NULL) { temp = new Tree; temp->data=elm; temp->left=NULL; temp->Right=NULL; } else { if(elm>=temp->data) temp->Right=insert(temp->Right,elm); else temp->left=insert(temp->left,elm); } return temp; } ///////////////////////////////////////////////////////////////////////////////////////// int count=1; void FirstMin(Tree *temp ) { if (count <5) { temp==NULL; //return; this doesn't work too } cout<<"Temp : "<left); cout<<"TEMP INNER :"<data<Right); } } int main() { root=insert(root,9); insert(root,11); insert(root,4); insert(root,3); insert(root,6); insert(root,22); insert(root,1); insert(root,6); insert(root,21); insert(root,7); FirstMin(root); getch(); }``````
You already posted before, not sure why you are still posting this again. To find the minimum, you simply branch left in the tree until you get to a null value then the last non-null value encountered is the minimum. The counter business does not make sense, no sense in counting unless you have a balanced BST, then counting might make sense if you want to know the height of the tree.
I am sorry i was in a hurry i did not properly imply that this code is for first 5 minimum numbers that's what the counter is for and yes i did post another thread for minimum value which is the most left leaf , but this is a different
situation where minimum value can also be at the right or left side of the tree
 ``1234567891011121314151617`` ``````int Min(int count, Tree *temp) { if (temp == nullptr || count <= 0) return count; if (temp->left != nullptr) { count = Min(count, temp->left); } if (count > 0) { count--; std::cout << temp->data << ' '; } if (temp->right != nullptr) { count = Min(count, temp->right); } return count; }``````

Demo: http://coliru.stacked-crooked.com/a/96b64e10172b87d1
Topic archived. No new replies allowed.