Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct tree
- {
- int info;
- struct tree *left;
- struct tree *right;
- struct tree *baap;
- };
- typedef struct tree *ptr;
- ptr root= NULL;
- ptr present=NULL;
- ptr maketree()
- {
- ptr p;
- p=(ptr)malloc(sizeof(struct tree));
- if(p==NULL)
- {
- printf("cannot create\n");
- return NULL;
- }
- else
- return p;
- }
- void addleft(ptr q)
- {
- ptr p=NULL;
- p=maketree();
- printf("enter data:\n");
- scanf("%d",&p->info);
- p->left=NULL;
- p->right=NULL;
- q->left=p;
- p->baap=q;
- }
- void addright(ptr q)
- {
- ptr p;
- p=maketree();
- printf("enter data:\n");
- scanf("%d",&p->info);
- p->left=NULL;
- p->right=NULL;
- q->right=p;
- p->baap=q;
- }
- void display(ptr abc)
- {
- if(abc!=NULL)
- {
- printf("%d ",abc->info);
- display(abc->left);
- display(abc->right);
- }
- }
- void main()
- {
- int a;
- printf("enter choice:\n");
- printf("0 to exit\n");
- printf("1.start tree (root)\n");
- printf("2.make left leaf\n");
- printf("3.make right leaf\n");
- printf("4.goto parent\n");
- printf("5.goto left\n");
- printf("6.goto right\n");
- printf("7.display tree\n");
- scanf("%d",&a);
- do
- {
- switch(a)
- {
- case 0:
- break;
- case 1:
- if(root==NULL)
- {
- root=maketree();
- printf("enter data\n");
- scanf("%d",&root->info);
- present=root;
- }
- else
- printf("root already exists\n");
- break;
- case 2:
- if(present->left!=NULL)
- addleft(present);
- else
- printf("data already exists in this location\n");
- break;
- case 3:
- if(present->right!=NULL)
- addright(present);
- else
- printf("data already exists in this location\n");
- break;
- case 4:
- if(present->baap!=NULL)
- present=present->baap;
- else
- printf("present position is root\n");
- break;
- case 5:
- if(present->left!=NULL)
- present=present->left;
- else
- printf("present position is a leaf\n");
- break;
- case 6:
- if(present->right!=NULL)
- present=present->right;
- else
- printf("present position is a leaf\n");
- break;
- case 7:
- display(root);
- break;
- default:
- printf("invalid operation");
- break;
- }
- printf("enter choice:\n");
- scanf("%d",&a);
- }while(a!=0);
- }
Advertisement
Add Comment
Please, Sign In to add comment