BINARY SEARCH TREE
#include<stdio.h>#include<stdlib.h>#include<conio.h>struct searchtree{int element;struct searchtree *left,*right;}*root;typedef struct searchtree *node;typedef int ElementType;node insert(ElementType, node);node delet(ElementType, node);void makeempty();node findmin(node);node findmax(node);node find(ElementType, node);void display(node, int);void main(){int ch;ElementType a;node temp;makeempty();while(1){ printf("\n1. Insert\n2. Delete\n3. Find min\n4. Find max\n5. Find\n6.Display\n7. Exit\nEnter Your Choice : ");scanf("%d",&ch);switch(ch){case 1: printf("Enter an element : ");scanf("%d", &a);root = insert(a, root); break;case 2: printf("\nEnter the element to delete : ");scanf("%d",&a);root = delet(a, root); break;case 3: printf("\nEnter the element to search : ");scanf("%d",&a);temp = find(a, root);if (temp != NULL) printf("Element found");else printf("Element not found"); break;case 4:temp = findmin(root);if(temp==NULL) printf("\nEmpty tree");else printf("\nMinimum element : %d", temp->element); break;case 5:temp = findmax(root);if(temp==NULL) printf("\nEmpty tree");else printf("\nMaximum element : %d", temp->element); break;case 6:if(root==NULL) printf("\nEmpty tree");elsedisplay(root, 1); break;case 7:exit(0);default: printf("Invalid Choice");}}}node insert(ElementType x,node t){if(t==NULL){t = (node)malloc(sizeof(node));t->element = x;t->left = t->right = NULL;}else{if(x < t->element)t->left = insert(x, t->left);else if(x > t->element)t->right = insert(x, t->right);}return t;
No comments:
Post a Comment