Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node N;
- struct node
- {
- int data;
- N *left ,*right;
- };
- N *create()
- {
- int item;
- N *new_node;
- new_node=(N*)malloc(sizeof(N));
- printf("Enter the data:\n");
- scanf("%d",&item);
- if(item==-1)
- return 0;
- new_node->data=item;
- printf("Enter the left data of %d:\n",item);
- new_node->left=create();
- printf("Enter the right data of %d:\n",item);
- new_node->right=create();
- return new_node;
- }
- void pre_order(N *root)
- {
- if(root==-1)
- {
- return ;
- }
- printf("%d\t",root->data);
- pre_order(root->left);
- pre_order(root->right);
- }
- void in_order(N *root)
- {
- if(root==-1)
- {
- return ;
- }
- in_order(root->left);
- printf("%d\t",root->data);
- in_order(root->right);
- }
- void post_order(N *root)
- {
- if(root==-1)
- return ;
- post_order(root->left);
- post_order(root->right);
- printf("%d\t",root->data);
- }
- int main()
- {
- N *root=NULL;
- root=create();
- printf("The pre_order is:\n");
- pre_order(root);
- printf("The post_order is :\n");
- post_order(root);
- printf("The in_order is :\n");
- in_order(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement