upsidedown

trees- no output

Oct 11th, 2011
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct tree
  5. {
  6.     int info;
  7.     struct tree *left;
  8.     struct tree *right;
  9.     struct tree *baap;
  10. };
  11.  
  12. typedef struct tree *ptr;
  13. ptr root= NULL;
  14. ptr present=NULL;
  15.  
  16. ptr maketree()
  17. {
  18.     ptr p;
  19.     p=(ptr)malloc(sizeof(struct tree));
  20.     if(p==NULL)
  21.     {
  22.         printf("cannot create\n");
  23.         return NULL;
  24.     }
  25.     else
  26.         return p;
  27. }
  28.  
  29. void addleft(ptr q)
  30. {
  31.     ptr p=NULL;
  32.     p=maketree();
  33.     printf("enter data:\n");
  34.     scanf("%d",&p->info);
  35.     p->left=NULL;
  36.     p->right=NULL;
  37.     q->left=p;
  38.     p->baap=q;
  39. }
  40.  
  41. void addright(ptr q)
  42. {
  43.     ptr p;
  44.     p=maketree();
  45.     printf("enter data:\n");
  46.     scanf("%d",&p->info);
  47.     p->left=NULL;
  48.     p->right=NULL;
  49.     q->right=p;
  50.     p->baap=q;
  51. }
  52.  
  53. void display(ptr abc)
  54. {
  55.     if(abc!=NULL)
  56.     {
  57.         printf("%d ",abc->info);
  58.  
  59.         display(abc->left);
  60.         display(abc->right);
  61.     }
  62.  
  63. }
  64.  
  65.  
  66.  
  67. void main()
  68. {
  69.     int a;
  70.     printf("enter choice:\n");
  71.     printf("0 to exit\n");
  72.     printf("1.start tree (root)\n");
  73.     printf("2.make left leaf\n");
  74.     printf("3.make right leaf\n");
  75.     printf("4.goto parent\n");
  76.     printf("5.goto left\n");
  77.     printf("6.goto right\n");
  78.     printf("7.display tree\n");
  79.     scanf("%d",&a);
  80.     do
  81.     {
  82.     switch(a)
  83.     {
  84.     case 0:
  85.         break;
  86.  
  87.     case 1:
  88.         if(root==NULL)
  89.         {
  90.             root=maketree();
  91.             printf("enter data\n");
  92.             scanf("%d",&root->info);
  93.             present=root;
  94.         }
  95.         else
  96.             printf("root already exists\n");
  97.         break;
  98.  
  99.     case 2:
  100.         if(present->left!=NULL)
  101.             addleft(present);
  102.         else
  103.             printf("data already exists in this location\n");
  104.         break;
  105.  
  106.     case 3:
  107.         if(present->right!=NULL)
  108.             addright(present);
  109.         else
  110.             printf("data already exists in this location\n");
  111.         break;
  112.  
  113.     case 4:
  114.         if(present->baap!=NULL)
  115.             present=present->baap;
  116.         else
  117.             printf("present position is root\n");
  118.         break;
  119.  
  120.     case 5:
  121.         if(present->left!=NULL)
  122.             present=present->left;
  123.         else
  124.             printf("present position is a leaf\n");
  125.         break;
  126.  
  127.     case 6:
  128.         if(present->right!=NULL)
  129.             present=present->right;
  130.         else
  131.             printf("present position is a leaf\n");
  132.         break;
  133.  
  134.     case 7:
  135.         display(root);
  136.         break;
  137.          
  138.     default:
  139.         printf("invalid operation");
  140.         break;
  141.     }
  142.     printf("enter choice:\n");
  143.     scanf("%d",&a);
  144.     }while(a!=0);
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
Advertisement
Add Comment
Please, Sign In to add comment