I made this program to create a binary search tree but it is not working properly ......could smbdy tell me whts the prob

/* Creating a BInary Tree */

#include<conio.h>
#include<stdio.h>

struct node
{
    struct node *lt;
    struct node *rt;
    int data;
}*temp;

void inorder(struct node* root)
{
    while(root != NULL)
    {
        inorder(root->lt);
        printf(" %d",root->data);
        inorder(root->rt);
    }
}

void main()
{
    struct node *root,*New;
    char ch;
//  int num;
    clrscr();
    root= NULL;
    do
    {
        New = (struct node*)malloc(sizeof(struct node*));
        New->lt = NULL;
        New->rt = NULL;

        printf("\nEnter the node:");
        scanf("%d",&New->data);

        if(root == NULL)
            root = New;
        else
        {
            temp = root;
            while(1)
            {
                if(New->data < temp->data)
                {
                    if(temp->lt == NULL)
                    {
                        temp->lt = New;
                        break;
                    }
                    else
                        temp = temp->lt;
                }
                else
                {
                    if(temp->rt == NULL)
                    {
                        temp->rt = New;
                        break;
                    }
                    else
                        temp = temp->rt;
                }
            }
        }

        //free(New);

        printf("DO u want to continue(y/n):");
        flushall();
        scanf("%c",&ch);

    }while(ch == 'y' || ch == 'Y' );

    inorder(root);
    getch();
}

I made this program to create a binary search tree but it is not working properly ......could smbdy tell me whts the prob

Can you be more specific? What in particular is not working?

Ya Actually the root smhow doesnot point to the head or root node when i accept the second node .... and thus i m not able to print the tree

The problem is with:
New = (struct node*)malloc(sizeof(struct node*));
//Here you are allocating 4 bytes of memory, sizeof(pointer) is always 4 bytes.

so better use:
New = (struct node*)malloc(sizeof(struct node));
//Here you are allocating memory required for the node

ya u r right ..... thanks v ..... but now somehow the tree is not getting printed properly..if i put node 10 , 5 , 15 it is printing 5 infinitely when i run the program ...wats wrong thr???

void inorder(struct node* root)
{
   while(root != NULL)
   {
      inorder(root->lt);
      printf(" %d",root->data);
      inorder(root->rt);
   }
}

The problem is with the above code: why are you using while loop????

void inorder(struct node* root)
{
   if(root != NULL)
   {
      inorder(root->lt);
      printf(" %d",root->data);
      inorder(root->rt);
   }
}

Instead use if statement. That should solve your problem

void inorder(struct node* root)
{
   while(root != NULL)
   {
      inorder(root->lt);
      printf(" %d",root->data);
      inorder(root->rt);
   }
}

The problem is with the above code: why are you using while loop????

void inorder(struct node* root)
{
   if(root != NULL)
   {
      inorder(root->lt);
      printf(" %d",root->data);
      inorder(root->rt);
   }
}

Instead use if statement. That should solve your problem

Ya thts rightt Thnks man !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.