Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- int count=0,max=0,min=0;
- typedef struct node
- {
- int info;
- struct node *lc,*rc;
- }bin_tere;
- bin_tere *stack[500];
- int top=-1;
- void push(bin_tere *root)
- {
- stack[++top]=root;
- }
- bin_tere *pop()
- {
- bin_tere *temp;
- temp=stack[top--];
- return temp;
- }
- void create(bin_tere *root)
- {
- char ans;
- bin_tere *ptr;
- printf("\nDo you want to create the left child of %d? ",root->info);
- fflush(stdin);
- scanf("%c",&ans);
- if(ans=='Y' || ans=='y')
- {
- ptr=(bin_tere*)malloc(sizeof(bin_tere));
- printf("\nenter the information for the left child: ");
- scanf("%d",&ptr->info);
- ptr->lc=ptr->rc=NULL;
- root->lc=ptr;
- create(root->lc);
- }
- else
- root->lc=NULL;
- printf("\nDo you want to create the right child of %d? ",root->info);
- fflush(stdin);
- scanf("%c",&ans);
- if(ans=='Y' || ans=='y')
- {
- ptr=(bin_tere*)malloc(sizeof(bin_tere));
- printf("\nenter the information for the right child: ");
- scanf("%d",&ptr->info);
- ptr->lc=ptr->rc=NULL;
- root->rc=ptr;
- create(root->rc);
- }
- else
- root->rc=NULL;
- }
- void inorder(bin_tere *root)
- {
- bin_tere *curr,*temp;
- curr=root;
- //printf("im here %d",curr->info);
- while(1)
- {
- while(curr!=NULL)
- {
- push(curr);
- curr=curr->lc;
- }
- if(curr==NULL && top>-1)
- {
- temp=pop();
- printf("%d ",temp->info);
- curr=temp->rc;
- }
- if(curr==NULL & top==-1)
- break;
- }
- }
- void preorder(bin_tere *root)
- {
- struct node *stack[100];
- int top = -1;
- struct node *current = root;
- while (1)
- {
- while (current)
- {
- printf("%d ", current->info);
- stack[++top] = current;
- current = current->lc;
- }
- if (top < 0)
- {
- break;
- }
- current = stack[top--];
- current = current->rc;
- }
- }
- void postorder(bin_tere *root)
- {
- do
- {
- while (root)
- {
- if (root->rc)
- push(root->rc);
- push(root);
- root = root->lc;
- }
- root = pop();
- if (root->rc && stack[top] == root->rc)
- {
- pop();
- push(root);
- root = root->rc;
- }
- else
- {
- printf("%d ", root->info);
- root = NULL;
- }
- } while (top != -1);
- top = -1;
- }
- int main()
- {
- bin_tere *root=NULL,*ptr;
- int ch,n;
- printf("\nenter the information for the root: ");
- ptr=(bin_tere*)malloc(sizeof(bin_tere));
- scanf("%d",&ptr->info);
- ptr->lc=ptr->rc=NULL;
- root=ptr;
- create(root);
- printf("The in-order traversal is :");
- inorder(root);
- printf("\nThe Pre-order traversal is :");
- preorder(root);
- printf("\nThe Post-order traversal is :");
- postorder(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement