0

is it not enough to declare the struct variables as shown?? getting error that some are undeclared pls help...
insertion on a binary search tree....

#include<stdio.h>
#include<stdlib.h>
struct node
{
  int data;
  struct node *lchild,*rchild;
}*ptr,*new;
void insert(int item,struct node *root)
{
  ptr=root;
  while(ptr!=NULL)
    {
      if(item>ptr->data)
	{
	  if(ptr->rchild!=NULL)
	    insert(item,rchild);
	  else
	    {
	      new=(struct node*)malloc(sizeof(struct node));
	      ptr->data=item;
	    }
	}
      if(item<ptr->data)
	{
	  if(ptr->lchild!=NULL)
	    insert(item,lchild);
	  else
	    {
	      new=(struct node*)malloc(sizeof(struct node));
	      ptr->data=item;
	    }
	}
    }
}
void main()
{
  int ch,item;
  char y;
do
  {
  printf(" main menu \nInsert\n2.delete\n3.preorder\n4.inorder\n5.postorder\n6.hight\n7.count\n    Enter your choice  : ");
  scanf("%d",&ch);
  switch(ch)
    {
    case 1:
      printf("enter the number :");
      scanf("%d",&item);
      if(root==NULL)
	{
	  new=(struct node*)malloc(sizeof(struct node));
	  root->data=item;
	  root->rchild=NULL;
	  root->lchild=NULL;
	}
      else
	insert(item,root);
      break;
      /*    case 2:
      delete();
      break;
    case 3:
      preorder();
      break;
    case 4:
      inorder();
      break;
    case 5:
      postorder();
      break;
    case 6:
      hight();
      break;
    case 7:
      count();
      break;    */
    default:
      printf("Invalid choice  \n");
      break;
    }
  printf(" press y to continue : ");
  scanf("%s",&y);
  }while(y=='y');
}

Edited by WaltP: Added CODE Tags -- please use them.

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Narue
0

Structures are an aggregate type. You need an instance of the structure before you can access any members. Notice that rchild and lchild are referenced without any struct instance as if they were not members, You probably want something more like ptr->rchild , for example.

You also can't use a variable defined in another function. In this case you're trying to use root in main when it's defined in insert.

Might I suggest reading up on scoping rules for C?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.