1
votes

I am writing a c program to create threaded binary tree and then to find INORDER SUCCESSOR of a particular node. For this, i am displaying inorder sequence for the TBT constructed and then asking user to input the node to which successor is to be displayed.. I have written function to do this. But i am not getting successor for the FIRST NODE .. Last node's successor is 0 any ways its working fine.. Can any one help me fix this ? Here is the whole program :

#include<stdio.h>
#include <stdlib.h>


struct tbtnode {
       int data;
       struct tbtnode *left,*right;
       int lbit,rbit,flag;
       int child;
}*root=NULL;

typedef struct tbtnode TBT;


TBT *insuc(TBT *t);




void inorder(TBT *);

void create(TBT *);

void create(TBT *root)
{
   int x,op,flag,y;
   flag=0;
   char ch;
   TBT *curr=root;
   TBT *q,*p;
 do
   {

     printf("\nCurrent node %d \n\n 1.Left Direction.\n\n2.Right Direction",curr->data);
     printf("\nEnter ur choice :");
     scanf("%d",&op);
     switch(op)
     {
        case 1: if(curr->lbit==1)
                {
                    printf("Enter left child of %d : ",curr->data);
                    scanf("%d",&x);
                    q=(TBT *)malloc(sizeof(TBT));
                    q->data=x;
                    q->lbit=q->rbit=1;
                    q->left=curr->left;
                    q->right=curr;
                    curr->left=q;
                    curr->lbit=0;
                    q->child=0;
                    flag=1;
                }
                else
                    curr=curr->left;
                break;
        case 2: if(curr->rbit==1)
                {

                    printf("Enter right child of %d :",curr->data);
                    scanf("%d",&x);
                    q=(TBT *)malloc(sizeof(TBT));
                    q->data=x;
                    q->lbit=q->rbit=1;
                    q->left=curr;
                    q->right=curr->right;
                    curr->right=q;
                    curr->rbit=0;
                    q->child=1;
                    flag=1;
                }
                else
                    curr=curr->right;
                break;
    }
  }while(flag==0);
}



void inorder(TBT *head)
   {
    TBT *t;
    t=head->left;
    printf("\n");
    while(t->lbit==0)
    t=t->left;
      while(t!=head)
       {
     printf("  %d",t->data);
     t=insuc(t);
       }
   }

TBT *insuc(TBT *t)
   {
     if(t->rbit==0)
      {
      t=t->right;
       while(t->lbit==0)
      t=t->left;
      return(t);
      }
     else
       return(t->right);
    }


void inorder_successor(TBT *head,int x)
   {
    TBT *t;
    t=head->left;
    printf("\n");
    while(t->lbit==0)
    t=t->left;
      while(t!=head)
       {   
           t=insuc(t);
           if(t->data==x)
           { 
                t=insuc(t);
                printf("  %d",t->data);
            }
       }
   }


int main()
  {

    int op,x,n,i=0,item;
    char ch;
    TBT *head,*root,*succ;       //here head indicates dummy variable

    head=(TBT *)malloc(sizeof(TBT));
    head->left=head;
    head->right=head;
    head->lbit=1;
    head->rbit=1;

      do
      {
     printf("\n****Threaded binary tree operations****");
     printf("\n1)create\n2)inorder\n3)Successor\n4)exit");
     printf("\nEnter ur choice: ");
     scanf("%d",&op);
  switch(op)
   {
    case 1:

    printf("\nEnter Number Of Nodes :");
    scanf("%d",&n);
    printf("\nEnter root data: ");
    scanf("%d",&x);

     root=(TBT *)malloc(sizeof(TBT));
     root->data=x;
     root->lbit=root->rbit=1;
     root->child=0;
     root->left=head->left;

     head->left=root;
     head->lbit=0;
     root->right=head->right;


     for(i=0;i<n-1;i++)
      create(root);

      break;

    case 2:
    printf("\nInorder Traversal Is:\n");
      inorder(head);
      break;

    case 3: printf("Enter the node to which successor is to be found\n");
            scanf("%d",&item);
            inorder_successor(head,item);
            break;
    case 4:
      return(0);
      break;
    }
 }while(op<=4);
return 0;
}

please fix - inorder_successor() function for me.. Thank you

1
Step though the code in a debugger, line by line. If you don't find the problem, hopefully you can narrow it down and only show us the relevant code instead of everything. - Some programmer dude
unfortunately, i am using Mac, and i dont know how can i run debugger in gcc mac :( - Dodger
gdb can't be installed like gcc, on the Mac ?.. - xtof pernod
@JoachimPileborg, its going wrong in inorder_successor() .. its not giving the successor for the very first node of inorder sequence .. - Dodger

1 Answers

0
votes

You have many problems with your code. To start with you never check for NULL pointers. To continue, you have both a global and a local variable in main called root.

The last thing means that when you allocate memory for root in main, you allocate for the local variable, and the global variable will still be NULL.

There are also other things that look weird, like you assigning the left and right pointer of head to itself.

I think you need to lay it all out on paper first, both how you have it now and how you want it to be. It will help you to better visualize the tree.