Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
226 views

Binary Search Tree

This C program implements a binary search tree with functions to insert, delete, find, find minimum and maximum elements, and traverse the tree using inorder, preorder, and postorder methods. The main function contains a menu to test the tree functions and allows the user to insert elements into the tree, delete elements, find elements, and display the tree traversals. Functions like insert, delete, find, findmin, findmax are implemented to manipulate the binary search tree.

Uploaded by

Saranya Perumal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
226 views

Binary Search Tree

This C program implements a binary search tree with functions to insert, delete, find, find minimum and maximum elements, and traverse the tree using inorder, preorder, and postorder methods. The main function contains a menu to test the tree functions and allows the user to insert elements into the tree, delete elements, find elements, and display the tree traversals. Functions like insert, delete, find, findmin, findmax are implemented to manipulate the binary search tree.

Uploaded by

Saranya Perumal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

/*Binary search Tree Name:Saranya.P*/ #include<stdio.h> #include<stdlib.

h> struct searchtree { int element; struct searchtree *left,*right; }; typedef struct searchtree *node; typedef int ElementType; node insert(ElementType, node); node delet(ElementType, node); //node makeempty(node); node findmin(node); node findmax(node); node find(ElementType, node); void display(node, int); int inorder(node); void preorder(node); node postorder(node); node find_max(node); int main() { int ch; ElementType a; node temp,T; temp=NULL; T=NULL; //T = (node) malloc ( sizeof (struct searchtree) ); temp = (node) malloc ( sizeof (struct searchtree) ); while(1) { printf("\n1. Insert\n2. Delete\n3. Find\n4. Find min\n5. Find max\n6. Display\n7 . Exit\nEnter Your Choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("Enter an element : "); scanf("%d", &a); T = insert(a, T); break; case 2: printf("\nEnter the element to delete : "); scanf("%d",&a); temp = delet(a,T); break; case 3: printf("\nEnter the element to search : "); scanf("%d",&a); temp = find(a,T); if (temp != NULL) printf("Element found"); else printf("Element not found");

break; case 4: temp = findmin(T); if(temp==NULL) printf("\nEmpty tree"); else printf("\nMinimum element : %d", temp->element); break; case 5: temp = find_max(T); //max=inorder(root); if(temp==NULL) printf("\nEmpty tree"); else printf("\nMaximum element : %d", temp->element); break; case 6: if(T==NULL) printf("\nEmpty tree"); printf("\nInorder Traversal:\n"); inorder(T); printf("\nPreorder Traversal:\n"); preorder(T); printf("\nPostorder Traversal:\n"); postorder(T); break; case 7: exit(0); default:printf("Invalid Choice"); }//end of switch } //end of while }//end of main node insert(ElementType x,node t) { if(t==NULL) { t = (node)malloc(sizeof(struct searchtree)); if(t==NULL) printf("\nOut of space!"); else { 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; } node delet(ElementType x,node t) { node temp; if(t == NULL) printf("\nElement not found"); else

if(x < t->element) t->left = delet(x, t->left); else if(x > t->element) t->right = delet(x, t->right); else if(t->left && t->right) { temp = findmin(t->right); t->element = temp->element; t->right = delet(t->element,t->right); } else { temp=t; if(t->left == NULL) t=t->right; else if(t->right==NULL) t=t->left; free(temp); } return t; } /*node makeempty(node temp) { if(temp!=NULL) { temp=makeemepty(temp->left); temp=makeempty(temp->right); free(temp); } return NULL; } */ node findmin(node temp) { if(temp == NULL) return NULL; else if(temp->left==NULL) return temp; else return findmin(temp->left); } node find_max(node temp) { if(temp!=NULL) while(temp->right!=NULL) temp=temp->right; return temp; } node find(ElementType x, node t) { if(t==NULL) return NULL; if(x<t->element) return find(x,t->left); if(x>t->element)

return find(x,t->right); return t; } void display(node t,int level) { int i; if(t) { display(t->right, level+1); printf("\n"); for(i=0;i<level;i++) printf(" "); printf("%d", t->element); display(t->left, level+1); } } int inorder(node t) { int max; if(t!=NULL) { inorder(t->left); printf("\t %d",t->element); inorder(t->right); max=t->element; } return max; } void preorder(node t) { if(t!=NULL) { printf("\t %d",t->element); preorder(t->left); preorder(t->right); } } node postorder(node t) { if(t!=NULL) { postorder(t->left); postorder(t->right); printf("\t %d",t->element); } return t; }

You might also like