Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<malloc.h>
- struct btree
- {
- int info;
- struct btree *lchild;
- struct btree *rchild;
- };
- struct btree * createbt();
- void preorder(struct btree *);
- void postorder(struct btree *);
- void inorder(struct btree *);
- int main()
- {
- struct btree *root;
- root=NULL;
- root=createbt();
- printf("\n The preorder traversal \n");
- preorder(root);
- printf("\n The postorder traversal \n");
- postorder(root);
- printf("\n The inorder traversal \n");
- inorder(root);
- }
- struct btree * createbt()
- {
- struct btree *newnode;
- newnode=NULL;
- int data;
- printf("\n Enter the new data ");
- scanf("%d",&data);
- if(data != 999)
- {
- newnode=(struct btree *)malloc(sizeof(struct btree));
- newnode->info=data;
- newnode->lchild=NULL;
- newnode->rchild=NULL;
- printf("\n Enter the left child of %d ",data);
- newnode->lchild=createbt();
- printf("\n Enter the right child of %d",data);
- newnode->rchild=createbt();
- }
- return newnode;
- }
- void preorder(struct btree *root)
- {
- if(root!=NULL)
- {
- printf("%d\n",root->info);
- preorder(root->lchild);
- preorder(root->rchild);
- }
- }
- void postorder(struct btree *root)
- {
- if(root!=NULL)
- {
- postorder(root->lchild);
- postorder(root->rchild);
- printf("%d\n",root->info);
- }
- }
- void inorder(struct btree *root)
- {
- if(root!=NULL)
- {
- inorder(root->lchild);
- printf("%d\n",root->info);
- inorder(root->rchild);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement