13. Write a C Program a. To construct a binary search tree of integers. b. To traverse the tree using all the methods i.

e,inorder, preorder and postorder. c. To display the elements in the tree. PROGRAM #include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> struct node { int info; struct node *left,*right; }; typedef struct node NODE; NODE *root=NULL; void create() { NODE *tmp,*curptr,*prev; int item; tmp=(NODE*)malloc(sizeof(NODE)); if(tmp==NULL) { printf("\nNo memory..\n"); return; } printf("\nEnter the value\n"); scanf("%d",&item); tmp->info=item; tmp->left=tmp->right=NULL; if(root==NULL) root=tmp; else { curptr=root; prev=NULL; while(curptr!=NULL) { if(curptr->info<item) { prev=curptr; curptr=curptr->right; } else { prev=curptr; curptr=curptr->left;

} if(curptr->info==item) { printf("\nDuplicate elements not allowed\n"); free(tmp); return; } } if(prev->info<item) prev->right=tmp; else prev->left=tmp; } printf("%d inserted",item); } void inord(NODE *tmp) { if(tmp==NULL) return; inord(tmp->left); printf("%d\n",tmp->info); inord(tmp->right); } void preord(NODE *tmp) { if(tmp==NULL) return; printf("%d\n",tmp->info); preord(tmp->left); preord(tmp->right); } void postord(NODE *tmp) { if(tmp==NULL) return; postord(tmp->left); postord(tmp->right); printf("%d\n",tmp->info); } void disp(NODE *tmp,int lev) { int i; if (tmp!=NULL) { disp(tmp->right,lev+1); for(i=0;i<lev;i++) printf(" "); printf("%d\n",tmp->info); disp(tmp->left,lev+1); } }

void main() { int ch; while(1) { clrscr(); printf("\nMENU\n\n1.Insert\n2.Traverse preorder\n3.Traverse postorder\n4.Traverse inorder\n5.Display\n6.Exit\n"); scanf("%d",&ch); switch(ch) { case 1: create();getch(); break; case 2: printf("\nPreorder\n");preord(root);getch();break; case 3: printf("\nPostorder\n");postord(root);getch();break; case 4: printf("\nInorder\n");inord(root);getch();break; case 5: disp(root,0);getch();break; case 6: exit(0); default: printf("\nIncorrect choice..\n"); } } } OUTPUT MENU 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 1 Enter the value 20 20 inserted 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 1 Enter the value 20 Duplicate elements not allowed

1.Insert

2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 1 Enter the value 30 30 inserted

1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 1 Enter the value 10 10 inserted 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 1 Enter the value 40 40 inserted 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 5 40 30 20 10

1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 2 Preorder 20 10 30 40 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 3 Postorder 10 40 30 20 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 4 Inorder 10 20 30 40 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display

6.Exit 6

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times