Exemple #1
0
void intrav(NODEPTR t){
    if(t!=NULL){
      intrav(t->left);
      printf("%d\n",t->key);
      intrav(t->right);
   }
}
Exemple #2
0
	   void intrav(struct node*r)  /* FUNCTION FOR RECURSIVE IN-ORDER
							TRAVERSAL  */
	   {
	    if(r!=NULL)
	    {
	     intrav(r->left);
	     printf("%s\t",r->data);
	     intrav(r->right);
	     }
	    }
Exemple #3
0
void intrav(struct node* root, struct node** prev, struct node** head)
{
	if(root!=NULL)
	{
		intrav(root->left, prev, head);
		root->left=(*prev);
		if((*prev))
			(*prev)->right=root;
		else
			*head=root;
		(*prev)=root;
		intrav(root->right, prev, head);
	}

}
Exemple #4
0
void traversal(NODEPTR p){
  int ch;
  printline('*');
  while(1){
    printf("\nEnter the choice\n1.pretraversal\n2.posttraversal\n3.inorder\n4.return");
    scanf("%d",&ch);
   
    switch(ch){
         
         case 1:
                pretrav(p);break;
         case 2:
                posttrav(p);break;
         case 3:
                intrav(p);break;  
         case 4:
               break;
         default:
                 printf("Hi i m default");
                 break;

      }/*end of switch*/
  }

}/*end of traversal*/ 
Exemple #5
0
int main()
{
	FILE *fp;
	fp=fopen("input.txt", "r");
	char line[100];
	int num[100]; int i=0;
	while(fgets(line, 100, fp)!= NULL)
	{
		num[i]=atoi(line);
		i=i+1;
	}
	//printf("%d", num[i-1]);
	fclose(fp);
	struct node* node1=malloc(sizeof(struct node*));
	/*struct node* node2=malloc(sizeof(struct node*));
	struct node* node3=malloc(sizeof(struct node*));
	struct node* node4=malloc(sizeof(struct node*));
	struct node* node5=malloc(sizeof(struct node*));*/
	node1->data=1;
	/*node2->data=2;
	node3->data=3;
	node4->data=4;
	node5->data=5;
	node4->left=node2;
	node4->right=node5;
	node5->left=NULL;
	node5->right=NULL;*/
	node1->left=NULL;
	node1->right=NULL;
	/*node3->left=NULL;
	node3->right=NULL;
	node2->left=node1;
	node2->right=node3;*/
	struct node* prev=NULL;
	struct node* head=NULL;
	intrav(node1, &prev, &head);
	printit(head);
	return 1;
}
Exemple #6
0
   void main()
   {
    int choice,c,d,flag;
    char temp='N',temp1[15];
      root=NULL;
     do
     {
      clrscr();
      printf("\n\n\n\t\t     *****  THE  CHOICE - MENU  IS  *****");
      printf("\n\n\n\t\t1> ENTER");
      printf("\n\t\t2>TRAVERSE");
      printf("\n\t\t3>DISPLAY");
      printf("\n\t\t4>EXIT");
      printf("\n\n\t\t ENTER  YOUR  CHOICE :");
      scanf("%d",&choice);
      switch(choice)
       {
	case 1:
	 root= NULL;         /* INSERTION  OF  NODE */
	 root->left=NULL;
	 root->right=NULL;
	 do
	 {
	  s= (struct node*)malloc (sizeof(struct node));
	  s->left=NULL;
	  s->right=NULL;
	  printf("\n Enter The Data:");
	  scanf("%s",&s->data);
	  cont++;
	  if(root==NULL)
	    root=s;                         //ASSIGNING VALUE TO ROOT NODE
	  else
	    insert(root,s);
	    fflush(stdin);
	    printf("\n Enter More Elements(Y/N):");
	    scanf("%c",&temp);
	    }while(temp=='y'||temp=='Y');
	  break;

	 case 2:
	   do
	   {
	    clrscr();
	   printf("\n\n\n\n\t\t ****  THE  CHOICE - MENU IS ****");
	    printf("\n\n\n\n\t\t 1.RECURSIVE TRAVERSING\n\n\t\t 2.NON-RECURSIVE TRAVERSING\n\n\t\t 3.EXIT");
	    printf("\n\n\t\tEnter Your  Choice:");
	    scanf("%d",&d);
	    switch(d)
	    {
	    case 1:
	       clrscr();
	       do
	       {
		clrscr();
		printf("\n\n\n\n\t\t  ****  THE  CHOICE - MENU  IS ****" );
		printf("\n\n\n\t\t1.PREORDER\n\t\t2.INORDER\n\t\t3.POST-ORDER\n\t\t4.Exit");
		printf("\n\n\tEnter Your Choice:");
		scanf("%d",&c);
		if(root==NULL)
		 printf("\n\n   Tree  Not Started Yet !!");
	       else
		{
		  switch(c)
		  {
		   case 1:                //CALLING OF RECURSIVE FUNCTIONS
		     pretrav(root);
		     break;
		   case 2:
		     intrav(root);
		     break;
		   case 3:
		     posttrav(root);
		     break;

		   }
	    printf("\n Press Any Key  To Continue !!!");
	    getch();
	   }
	    }while(c!=4);
	    break;

	  case 2:
	   clrscr();
	   do
	   {
		clrscr();
		printf("\n\n\n\n\t\t   **** THE  CHOICE MENU IS  ****");
		printf("\n\n\n\t\t1.PREORDER\n\t\t2.INORDER\n\t\t3.POST-ORDER\n\t\t4.Exit");
		printf("\n\n\tEnter Your Choice:");
		scanf("%d",&c);
		if(root==NULL)        /*  TREE  EMPTY  CONDITION */
		 printf("\n\n   Tree  Not Started Yet !!");
	       else
		{
		  switch(c)
		  {
		   case 1:
		     npretrav(root);   //CALLING OF  NON-RECURSIVE
		     break;            //FUNCTIONS
		   case 2:
		     nintrav(root);
		     break;
		   case 3:
		     npostrav(root);
		     break;
		   }
	    printf("\n Press Any Key  To Continue !!!");
	    getch();
	   }
	    }while(c!=4);
	    break;
	   }
	  }while(d!=3);
	    break;

	  case 3:
				  if(root==NULL)
				 printf("\n THE TREE IS EMPTY ");
			  else
			   { initgraph(&gd,&gm,"d:\\tc");
			  //CALLING FUNCTION FOR DISPLAY
				 display(root,1,getmaxx()+1,15,1);  // 640 is the getmaxx
				 getch();
				 closegraph();
			   } break;

	  }

	 }while(choice!=4);
	}