This is the code i am using to add in the BST.And this is the preorder function.Why is it going into infinity?I am taking the input from the user about the element to add.But the output goes into infinity.
void add(){
struct node* temp=(struct node *) malloc(sizeof(struct node));
struct node* current=root;
printf("\nEnter the data:\n");
scanf("%d",&temp->data);
while(current->left!=NULL && current->right!=NULL){
parent=current;
if(temp->data > parent->data){
current=current->right;
}
else{
current=current->left;
}
}
if(temp->data > parent->data){
parent->right=temp;
parent->left=NULL;
}
else{
parent->left=temp;
parent->right=temp;
}
}
void preorder(struct node* node)
{
if (node == NULL)
return;
/* first print data of node */
printf("%d ", node->data);
/* then recur on left subtree */
preorder(node->left);
/* now recur on right subtree */
preorder(node->right);
}
The function add has undefined behavior.
For starters root initially can be equal to NULL. So you may not use this approach
struct node* current=root;
//...
while(current->left!=NULL && current->right!=NULL){
because there is an attempt to access memory using a null-pointer.
Secondly this loop
while(current->left!=NULL && current->right!=NULL){
parent=current;
does not process a node that contains only one child node not equal to NULL.
Also you are not setting to NULL data members left and right of the created node temp.
And there is a typo in these statements
parent->left=temp;
parent->right=temp;
So the function definition is invalid.
And the function should do only one thing: append a node to the tree. It should not prompt the user to enter some data.
Take into account that it is a bad idea to use a global variable for the root node.
Nevertheless here is a demonstrative program that uses your approach. I call this approach Java-approach (in C the function add can be written much simpler)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node
{
int data;
struct node *left, *right;
};
struct node *root;
_Bool add( int data )
{
struct node *new_node = malloc( sizeof( struct node ) );
_Bool success = new_node != NULL;
if ( success )
{
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
if ( root == NULL )
{
root = new_node;
}
else
{
for ( struct node *current = root; ; )
{
struct node *parent = current;
if ( data < current->data )
{
current = current->left;
if ( current == NULL )
{
parent->left = new_node;
break;
}
}
else
{
current = current->right;
if ( current == NULL )
{
parent->right = new_node;
break;
}
}
}
}
}
return success;
}
void preorder( const struct node *node )
{
if ( node != NULL )
{
printf( "%d ", node->data );
preorder( node->left );
preorder( node->right );
}
}
int main(void)
{
srand( ( unsigned int )time( NULL ) );
const int N = 10;
for ( int i = 0; i < N; i++ ) add( rand() % N );
preorder( root );
putchar( '\n' );
return 0;
}
The program output might look like
4 0 3 2 1 0 1 2 5 8
And below there is a demonstrative program that shows how the function add can be written using the C approach. In the program the global variable root is removed and substituted for a local variable root.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node
{
int data;
struct node *left, *right;
};
_Bool add( struct node **root, int data )
{
struct node *new_node = malloc( sizeof( struct node ) );
_Bool success = new_node != NULL;
if ( success )
{
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
while ( *root != NULL )
{
if ( data < ( *root )->data ) root = &( *root )->left;
else root = &( *root )->right;
}
*root = new_node;
}
return success;
}
void preorder( const struct node *node )
{
if ( node != NULL )
{
printf( "%d ", node->data );
preorder( node->left );
preorder( node->right );
}
}
int main(void)
{
struct node *root = NULL;
srand( ( unsigned int )time( NULL ) );
const int N = 10;
for ( int i = 0; i < N; i++ ) add( &root, rand() % N );
preorder( root );
putchar( '\n' );
return 0;
}
Now investigate and compare the two implementations of the function add.