Right now Im still beggining to learn the idea of pointers,basically I just want to ask a question about the code I have found, I mean I want to know what is happening right here...the comments on the code serve as my question just look at below...
#include<iostream.h>
struct node
{
int data;
node *left;
node *right;
};
node *root;
class tree
{
public :
tree()
{
root=NULL;
};
void insert();
void display_inorder(node *);
void display_inorder()
{
display_inorder(root);
};
};
void tree::insert()
{
cout<<"\nEnter 0 to exit\nEnter a number to insert\n";
node *nn=new node;
nn->left=NULL;
nn->right=NULL;
cin>>nn->data;
if(nn->data == 0)
{
cout<<"\n\t\t\tProgram will now exit....\n";
system("PAUSE");
exit(1);
}
if(root==NULL)
{
root=nn;
}
else
{
node *prev=root;
node *current=root;
while(current!=NULL)
{
prev=current;
if(nn->data >= current->data) //can someone explain me in this scene
current=current->right;
else
current=current->left;
}
if(nn->data >= prev->data) //can someone also tell me what is happening right here please
prev->right=nn;
else
prev->left=nn;
}
}//insert()
void tree::display_inorder(node *n)
{
if(n!=NULL)
{
display_inorder(n->left);
cout<<n->data<<" ";
display_inorder(n->right);
}
}
int main()
{
tree t;
int num;
while(num!=0)
{
t.insert();
cout<<"\nBinary Tree -> ";
t.display_inorder();
cout<<"\n";
}//while
exit(0);
system("PAUSE");
return 0;
}//main
the part in the if statement(there are 2 of these) under insert() a simple explanation please..Any help can do!just curious about the program :cool: