#include<bits/stdc++.h>
using namespace std;
struct node
{
node *left;
int data;
node *right;
node(int x)
{
left=right=NULL;
data=x;
}
}*root=NULL;
void display(node *root)
{
if(root)
{
display(root->left);
cout<<root->data<<" ";
display(root->right);
}
}
void insert(int key)
{
node *r=NULL, *n=new node(key);
if(root==NULL)
{
root=n;
return;
}
node *t=root;
while(t)
{
r=t;
if(key<root->data)
t=t->left;
if(key>root->data)
t=t->right;
}
if(key>r->data)
r->right=n;
else
r->left=n;
}
int main()
{
insert(30);
insert(20);
insert(5);
insert(40);
insert(25);
insert(10);
insert(45);
display(root);
return 0;
}
Expected Output is
5 10 20 25 30 40 45
The output appearing is:
5 10 20 30 40 45
Why 25 is getting missed?