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.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
void FirstMin(Tree *temp )
{
	
	if(temp!=NULL && count <5)
	{
		FirstMin(temp->left);
		cout<<temp->data<<endl;
		count++;
		FirstMin(temp->Right);
	}
}
void FirstMin(Tree *temp )
{
	if (count <5)
	{
		temp==NULL;
		//return; this doesn't work too 
	}
	if(temp!=NULL)
	{
		FirstMin(temp->left);
		cout<<temp->data<<endl;
		count++;
		FirstMin(temp->Right);
	}
}
#include<iostream>
#include<conio.h>
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 : "<<temp<<endl;
	if(temp!=NULL)
	{
		FirstMin(temp->left);
		cout<<"TEMP INNER :"<<temp<<endl;
		cout<<temp->data<<endl;
		count++;
		FirstMin(temp->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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.