Professional Documents
Culture Documents
A. Implementation of List Adt Using Singly Linked List
A. Implementation of List Adt Using Singly Linked List
ii. Free the node pointed by p iii. Make tmp as p 7. If the choice is Find a. Accept the data x b. Traverse the list till the data x is found or end of list i. If found display Element found ii. Else if null reached Element is not found 8. If the choice is Display a. Display all the elements by traversing the list of nodes using a pointer p until p becomes NULL.
SOURCE CODE #include <stdio.h> #include <malloc.h> typedef typedef typedef typedef struct node* ptrtonode; ptrtonode list; ptrtonode position; int elementtype;
struct node { elementtype element; ptrtonode next; }; list createlist(void); void insert(elementtype x,list l,position p); int islast(position p,list l); int isempty(list l); void delete1(elementtype x,list l); position find(elementtype x,list l); position findprevious(elementtype x, list l); void deletelist(list l); position header(list l); position advance(position p); elementtype retrieve(position p); list createlist(void) { list l; l= malloc (sizeof (struct node)); if (l==NULL) { printf ("Out of space!"); return l; } else { l->next=NULL; return l; } } void insert(elementtype x,list l,position p) { ptrtonode tmpcell; tmpcell=(list) malloc (sizeof (struct node)); if (tmpcell==NULL) printf ("Out of space\n"); else {
} }
int islast(position p,list l) { return p->next==NULL; } int isempty(list l) { return l->next==NULL; } void delete1(elementtype x,list l) { position p,tmpcell; p=findprevious(x,l); if(!islast(p,l)) { tmpcell=p->next; p->next=tmpcell->next; free(tmpcell); }
position find(elementtype x,list l) { position p; p=l->next; while(p!=NULL && p->element!=x) p=p->next; return p; } position findprevious(elementtype x, list l) { position p; p=l; while(p->next!=NULL && p->next->element!=x) p=p->next; return p; } void deletelist(list l) { position p,tmp; p=l->next; /* header assumed */
position header(list l) { return l; } elementtype retrieve(position p) { return p->element; } position advance(position p) { return p->next; } void printlist(const list l) { position p=header(l); if(isempty(l)) printf("empty list\n"); else do { p=advance(p); printf("%d\t",retrieve(p)); }while(!islast(p,l)); printf("\n"); } void main() { int ch,p1,i; elementtype data; ptrtonode prev,p,pos; list l; clrscr(); l=createlist(); printf("\n1. Insert an element (Assume header is at position 0)"); printf("\n2. Delete an element"); printf("\n3. Delete the list"); printf("\n4. Find an element"); printf("\n5. Display List elements"); printf("\n6. Quit"); while(1) { printf("\n Enter your choice "); scanf("%d",&ch);
switch(ch) { case 1: printf("\n Enter the element "); scanf("%d",&data); printf("\n Enter the position for insertion "); scanf("%d",&p1); p=l; i=1; while (p->next!=NULL && i<p1) { p=p->next; i++; } insert(data,l,p); break; case 2: printf("\n Enter the element to be Deleted "); scanf("%d",&data); delete1(data,l); break; case 3: deletelist(l); break; case 4: printf("\n Enter the element to be searched "); scanf("%d",&data); pos=find(data,l); if (pos==NULL) printf("\nElement not found"); else printf("\nElement found at %d",pos); break; case 5: printlist(l); break; case 6: exit(0); } } }
OUTPUT 1. Insert an element 2. Delete an element 3. Delete the list 4. Find an element 5. Display List elements 6. Quit Enter your choice 1 Enter the element 5 Enter the position for insertion;header is at position 0 1 Enter your choice 1 Enter the element 6 Enter the position for insertion;header is at position 0 2 Enter your choice 1 Enter the element 8 Enter the position for insertion;header is at position 0 3 Enter your choice5 5 6 8 Enter your choice 1 Enter the element 7 Enter the position for insertion;header is at position 0 3 Enter your choice 5 5 6 7 8 Enter your choice 4 Enter the element to be searched 6 Element found at 2266 Enter your choice 4 Enter the element to be searched 9
Element not found Enter your choice 2 Enter the element to be Deleted 7 Enter your choice 5 5 6 8 Enter your choice 2 Enter the element to be Deleted 8 Enter your choice 5 5 6 Enter your choice 3 Enter your choice 5 empty list Enter your choice 6
RESULT The list ADT is implemented in C language using singly linked list for the operations insert, delete, find, findprevious, deletelist and isempty and is executed and verified for sample input.
iv. else 1. Make the next of p->prev as NULL. 2. Free the memory of node p. 6. If the Choice is DeleteList a. Move a pointer p through each node until p is not NULL i. Make p->next as tmp ii. Free the node pointed by p iii. Make tmp as p 7. If the choice is Find a. Accept the data x b. Traverse the list till the data x is found or end of list i. If found display Element found ii. Else if null reached Element is not found 8. If the choice is Display a. Display all the elements by traversing the list of nodes using a pointer p until p becomes NULL.
SOURCE CODE #include <stdio.h> #include <malloc.h> typedef typedef typedef typedef struct node* ptrtonode; ptrtonode list; ptrtonode position; int elementtype;
struct node { elementtype element; ptrtonode next; ptrtonode prev; }; list createlist(void); void insert(elementtype x,list l,position p); int islast(position p,list l); int isempty(list l); void delete1(elementtype x,list l); position find(elementtype x,list l); position findprevious(elementtype x, list l); void deletelist(list l); position header(list l); position advance(position p); elementtype retrieve(position p); list createlist(void) { list l; l= malloc (sizeof (struct node)); if (l==NULL) { printf ("Out of space!"); return l; } else { l->next=NULL; l->prev=NULL; return l; } } void insert(elementtype x,list l,position p) { ptrtonode tmpcell,q; tmpcell=(list) malloc (sizeof (struct node)); if (tmpcell==NULL) printf ("Out of space\n");
else { tmpcell->element=x; tmpcell->next=p->next; tmpcell->prev=p; p->next=tmpcell; if(tmpcell->next) p->next->prev=tmpcell; } } int islast(position p,list l) { return p->next==NULL; } int isempty(list l) { return l->next==NULL; } void delete(elementtype x,list l) { position p,q,r; p=find(x,l); if(!islast(p,l)) { p->prev->next=p->next; p->next->prev=p->prev; free(p); } else { p->prev->next=NULL; free(p); } } position find(elementtype x,list l) { position p; p=l->next; while(p!=NULL && p->element!=x) p=p->next; return p; }
void deletelist(list l) { position p,tmp; p=l->next; /* header assumed */ l->next=NULL; while(p!=NULL) { tmp=p->next; free(p); p=tmp; } } free(l);
position header(list l) { return l; } elementtype retrieve(position p) { return p->element; } position advance(position p) { return p->next; } void printlist(const list l) { position p=header(l); if(isempty(l)) printf("empty list\n"); else do { p=advance(p); printf("%d\t",retrieve(p)); }while(!islast(p,l)); printf("\n"); } void main() { int ch,i,p1; elementtype data; ptrtonode prev,p,pos; list l; clrscr(); l=createlist(); printf("\n1. Insert operation(Assume header is at position 0");
Delete an element"); Delete the list"); Find operation"); Display List elements"); Quit");
while(1) { printf("\n Enter your choice "); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter the element "); scanf("%d",&data); printf("\n Enter the position for insertion "); scanf("%d",&p1); p=l; i=1; while (p->next!=NULL && i<p1) { p=p->next; i++; } insert(data,l,p); break; case 2: printf("\n Enter the element to be Deleted "); scanf("%d",&data); delete(data,l); break; case 3: deletelist(l); break; case 4: printf("\n Enter the element to be searched "); scanf("%d",&data); pos=find(data,l); if (pos==NULL) printf("\nElement not found"); else printf("\nElement found at position %d",pos); break; case 5: printlist(l); break; case 6: exit(0); } } }
OUTPUT 1. 2. 3. 4. 5. 6. Insert operation(Assume header is at position 0) Delete an element Delete the list Find operation Display List elements Quit
Enter your choice 1 Enter the element 2 Enter the position for insertion 1 Enter your choice 1 Enter the element 4 Enter the position for insertion 2 Enter your choice 1 Enter the element 8 Enter the position for insertion 3 Enter your choice 5 2 4 8 Enter your choice 1 Enter the element 6 Enter the position for insertion 3 Enter your choice 5 2 4 6 8 Enter your choice 4 Enter the element to be searched 6 Element found at position 2314 Enter your choice 4 Enter the element to be searched 9 Element not found
Enter your choice 2 Enter the element to be Deleted 6 Enter your choice 5 2 4 8 Enter your choice 2 Enter the element to be Deleted 8 Enter your choice 5 2 4 Enter your choice 3 Enter your choice 5 empty list Enter your choice 6
RESULT The list ADT is implemented in C language using doubly linked list for the operations insert, delete, find, findprevious, deletelist and isempty and is executed and verified for sample input.
typedef struct node* ptrtonode; typedef ptrtonode position; typedef ptrtonode polynomial; struct node { int coefficient; int exponent; ptrtonode next; }; polynomial createpoly (void); polynomial createheader(void); void insert(int coeff,int expo,polynomial l,position p); void copypoly(ptrtonode rest,polynomial p3,ptrtonode p3last); polynomial polyadd(polynomial p1,polynomial p2); position advance(position p); polynomial createpoly(void) { polynomial l,p; int expo,coeff,no,i; l=createheader(); printf("\n Enter no. of terms in the polynomial"); scanf("%d",&no); p=l; printf("\nEnter nodes sorted by exponent\n"); for(i=1;i<=no;i++) { printf("\n Enter the exponent"); scanf("%d",&expo); printf("\n Enter the coefficient"); scanf("%d",&coeff); p=advance(p); insert(coeff,expo,l,p); } return l; } position advance(position p) { while (p->next!=NULL) p=p->next; return p; } polynomial createheader(void) { polynomial l; l=malloc(sizeof(struct node)); if (l==NULL) {
printf ("Out of space!"); return l; } else { l->next=NULL; return l; } } void insert(int coeff,int expo,polynomial l,position p) { ptrtonode tmpcell; tmpcell=(ptrtonode)malloc(sizeof(struct node)); if (tmpcell==NULL) printf ("Out of space\n"); else { tmpcell->coefficient=coeff; tmpcell->exponent=expo; tmpcell->next=NULL; p->next=tmpcell; } } polynomial polyadd(polynomial p1,polynomial p2) { polynomial l,p; ptrtonode a1,a2; int coeff,expo; l=createheader(); p=l; a1=p1->next; a2=p2->next; while((a1!=NULL) && (a2!=NULL)) { if(a1->exponent==a2->exponent) { coeff=a1->coefficient+a2->coefficient; expo=a1->exponent; a1=a1->next; a2=a2->next; } else { if(a1->exponent > a2->exponent) { coeff=a1->coefficient; expo=a1->exponent; a1=a1->next;
} else {
} } p=advance(p); insert(coeff,expo,l,p);
while (a1!=NULL) { p=advance(p); insert(a1->coefficient ,a1->exponent,l,p); a1=a1->next; } while(a2!=NULL) { p=advance(p); insert(a2->coefficient ,a2->exponent,l,p); a2=a2->next; } return l; } void printpoly(polynomial p) { p=p->next; while(p->next) { printf( "%dx^%d + ", p->coefficient, p->exponent ); p=p->next; } if(p->exponent==0) { printf( "%d", p->coefficient); printf("\n"); } else { printf( "%dx^%d ", p->coefficient, p->exponent ); printf("\n"); } } void main() { polynomial p1,p2,p3;
clrscr(); printf("\n Create the First polynomial\n"); p1=createpoly(); printf("\n Create the Second polynomial\n"); p2=createpoly(); p3=polyadd(p1,p2); printf("\nFirst Polynomial= "); printpoly(p1); printf("\nSecond Polynomial= "); printpoly(p2); printf("\nSum of the above two Polynomials= "); printpoly(p3); }
Enter no. of terms in the polynomial3 Enter nodes sorted by exponent Enter the exponent2 Enter the coefficient5 Enter the exponent1 Enter the coefficient9 Enter the exponent0 Enter the coefficient7 Create the Second polynomial Enter no. of terms in the polynomial4 Enter nodes sorted by exponent Enter the exponent3 Enter the coefficient5 Enter the exponent2 Enter the coefficient12 Enter the exponent1 Enter the coefficient7 Enter the exponent0 Enter the coefficient8 First Polynomial= 5x^2 + 9x^1 + 7 Second Polynomial= 5x^3 + 12x^2 + 7x^1 + 8 Sum of the above two Polynomials= 5x^3 + 17x^2 + 16x^1 + 15
RESULT The polynomial ADT is implemented in C using linked list to perform polynomial addition and is executed and verified.
AIM Write a C program to convert an infix expression to postfix expression using Stack ADT. ALGORITHM 1. Get the infix expression to be converted 2. Initialize an empty stack.
3. Scan the Infix expression from left to right one character at a time and repeat
the steps below until null is read.
4. If stack is not empty pop all operators in the stack and add it to the postfix string. 5. Return the Postfix string.
SOURCE CODE
#include<stdio.h> #include "stackadt.c" int isoperator(char e) { if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%') return 1; else return 0; } int priority(char e) { int pri = 0; if(e == '*' || e == '/' || e =='%') pri = 2; else { if(e == '+' || e == '-') pri = 1; } return pri;
void infix2postfix(char *infix, char *postfix) { char *i,*p,n1; stack s; s=createstack(); i = infix; p = postfix; while(*i) { while(*i == ' ' || *i == '\t') { i++; } if( isdigit(*i) || isalpha(*i) ) { while( isdigit(*i) || isalpha(*i)) { *p = *i; p++; i++; } } if( *i == '(' ) { push(*i,s);
i++;
if( *i == ')') { n1 = topandpop(s); while( n1 != '(' ) { *p = n1; p++; n1 = topandpop(s); } i++; } if( isoperator(*i) ) { if(isempty(s)) push(*i,s); else { n1 = topandpop(s); while(priority(n1) >= priority(*i)) { *p = n1; p++; n1 = topandpop(s); } push(n1,s); push(*i,s); } i++; } } while(!isempty(s)) { n1 = topandpop(s); *p = n1; p++; } *p = '\0'; } int main() { char in[50],post[50]; strcpy(post,""); printf("Enter the Infix Expression : "); fflush(stdin); gets(in); infix2postfix(in,post); printf("\nPostfix Expression is : %s\n",post);
return 0;
stackadt.c #include <stdio.h> #include <string.h> #include <ctype.h> typedef struct stack* stack; typedef int elementtype; int MAX=10; #define EMPTYTOS -1 struct stack { elementtype *array; int top; int capacity; }; stack createstack() { stack s; s=malloc(sizeof(struct stack)); if(s==NULL) { printf("out of space); return s; } s->array=malloc(sizeof(elementtype)*MAX); if(s->array==NULL) { printf("out of space); return s; } s->capacity=MAX; s->top=EMPTYTOS; return s; } void push(elementtype x,stack s) { if(isfull(s)) printf("\nSTACK FULL"); else s->array[++s->top]=x; } elementtype topandpop(stack s) {
int isfull(stack s) { return s->top==MAX-1; } int isempty(stack s) { return s->top == EMPTYTOS; } void emptystack(stack s) { s->top=EMPTYTOS; }
OUTPUT
Enter Infix Expression : a+b*c+(d*e+f)+g Postfix Expression is : abc*+de*f++g+ Enter Infix Expression : a+b*c+(d*e+f)*g Postfix Expression is : abc*+de*f+g*+ Enter Infix Expression : ((a+b)*(d-e+f))/(g+h) Postfix Expression is : ab+de-f+*gh+/
RESULT Thus the conversion of an infix expression to postfix expression using stack ADT is implemented in C and is executed and verified.
implementation of circular queue. ALGORITHM 1. Create a queue structure a. Let capacity denotes the maximum number of elements in the queue, size denotes the actual number of elements in the queue and the rear and front point where insertion and deletion is to be done respectively. 2. 3. Initialize an array of size N If the choice is produce, a. If the queue is not full. i. Increment size by one. ii. Increment the rear pointer, if rear is equal to capacity then wrap around rear to position 0. iii. Store the value in rear position. 4. If the choice is consume , a. If the queue is not empty. i. Decrement size by one. ii. Increment the front pointer, if front is equal to capacity then wrap around front to position 0. iii. Retrieve the value from front position. 5. If the choice is display, then display the elements of the queue.
SOURCE CODE #include <stdio.h> #include <stdlib.h> typedef int elementtype; typedef struct queue *queue; int isempty( queue q); int isfull( queue q); queue createqueue( int maxelements ); void disposequeue( queue q ); void makeempty( queue q ); void enqueue( elementtype x, queue q ); elementtype frontanddequeue( queue q ); struct queue { int capacity; int front; int rear; int size; elementtype *array; }; int isempty( queue q ) { } int isfull( queue q ) { } queue createqueue( int maxelements ) return q->size == q->capacity; return q->size == 0;
{ queue q; q = malloc( sizeof( struct queue) ); if( q == NULL ) printf( "Out of space!!!" ); q->array = malloc( sizeof( elementtype ) * maxelements ); if( q->array == NULL ) printf( "Out of space!!!" ); q->capacity = maxelements; makeempty( q ); return q; } void makeempty( queue q ) { q->size = 0; q->front = 1; q->rear = 0; } void disposequeue( queue q ) { if( q != NULL ) { } } void enqueue( elementtype x, queue q ) { if( isfull( q ) ) { printf( "\nFull queue- cannot produce further" ); return; free( q->array ); free( q );
} else { q->size++; if (++q->rear==q->capacity) q->rear=0; q->array[ q->rear ] = x; } getch(); } elementtype frontanddequeue( queue q ) { elementtype x = 0; if( isempty(q)) { printf("\ncannot consume-empty queue" ); return -1; } else { q->size--; x = q->array[ q->front ]; if (++q->front==q->capacity) q->front=0; } return x; } void printqueue( queue q) { int i,f; if( isempty( q ) ) printf( "\nqueue is empty" ); else { f=q->front;
for(i=1;i<=q->size;i++) { printf("\t%d",q->array[f]); if (++f==q->capacity) f=0; } } } void main() { queue q; int x,ch,data; clrscr(); q = createqueue(3); printf("Queue size is 3\n"); printf("\n1. Produce "); printf("\n2. Consume "); printf("\n3. Display queue elements"); printf("\n4. Quit"); while(1) { printf("\n Enter your choice "); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter the element "); scanf("%d",&data); enqueue(data,q); break; case 2: x=frontanddequeue(q);
if(x!=-1) printf("\ndeleted element %d ",x); break; case 3: printqueue(q); break; case 4: exit(0); } } }
OUTPUT Queue size is 3 1. Produce 2. Consume 3. Display queue elements 4. Quit Enter your choice 1 Enter the element 2 Enter your choice 1 Enter the element 4 Enter your choice 1 Enter the element 6 Enter your choice 3 2 4 6 Enter your choice 1 Enter the element 8 Full queue- cannot produce further Enter your choice 2 deleted element 2 Enter your choice 3 4 6 Enter your choice 1 Enter the element 8 Enter your choice 3 4 6 8 Enter your choice 2 deleted element 4 Enter your choice 2 deleted element 6 Enter your choice 2
deleted element 8 Enter your choice 2 cannot consume-empty queue Enter your choice 4
RESULT Thus C program is written to simulate producer-consumer problem using array implementation of circular queue and is executed ad verified. .
b) When an operator is encountered, Create a node and store the operator in it Pop two pointers from the stack and make them as child nodes for the operator (make the first popped pointer the right child and the other pointer the left child) Push the operator nodes pointer into the stack
2. The stack will have the pointer of the expression trees root. TREE TRAVERSAL Pre-order Perform the following operations recursively at each node, starting with the root node: 1. Visit the root node. 2. Traverse the left subtree. 3. Traverse the right subtree. In-order Perform the following operations recursively at each node: 1. Traverse the left subtree. 2. Visit the root node. 3. Traverse the right subtree.
Post-order Perform the following operations recursively at each node: 1. Traverse the left subtree. 2. Traverse the right subtree. 3. Visit the root node. SOURCE CODE #include <stdio.h> #include<conio.h> #include <string.h> typedef struct tree { char data; struct tree *left; struct tree *right; }*pos; pos stack[30],temp; int top=-1; pos newnode(char b) { temp=(struct tree*)malloc(sizeof(struct tree)); temp->data=b; temp->left=NULL; temp->right=NULL; return(temp); } void push(pos temp) { stack[++top]=temp; } pos pop() { pos p; p=stack[top--]; return(p); }
void inorder(pos t) { if(t!=NULL) { inorder(t->left); printf("%c",t->data); inorder(t->right); } } void preorder(pos t) { if(t!=NULL) { printf("%c",t->data); preorder(t->left); preorder(t->right); } } void postorder(pos t) { if(t!=NULL) { postorder(t->left); postorder(t->right); printf("%c",t->data); } } void main() { char a[20]; pos temp,t; int j,i; clrscr(); printf("\nEnter the postfix expression "); gets(a); for(i=0;a[i]!='\0';i++) { if(a[i]=='*' || a[i]=='/'|| a[i]=='+' || a[i]=='-') { temp=newnode(a[i]); temp->right=pop(); temp->left=pop(); push(temp); } else { temp=newnode(a[i]); push(temp);
} } printf(The infix expression is \n); inorder(temp); printf("\n"); printf(The prefix expression is \n); preorder(temp); printf("\n"); printf(The postfix expression is \n); postorder(temp); getch(); } OUTPUT Enter the postfix expression abc*de+fg//+The infix expression is a-b*c+d+e/f/g The prefix expression is -a+*bc/+de/fg The postfix expression is abc*de+fg//+RESULT Thus a C program to construct an expression tree from a postfix expression and to perform the in-order, pre-order and post-order traversals was written, executed and verified.
6.
AIM
Write a C program to implement binary search Tree ADT and perform operations
such as find, findmin, findmax, insert and delete operation on the binary search tree. ALGORITHM 1. 2. Initialize node structure containing a data element X and two pointers for left subtree and right subtree. If the choice is Insert, a. Accept the data element X b. If the subtree T is null, then create a new treenode T and set X as Ts element and both Ts left and Ts right as NULL c. Otherwise if X is less than the data element of treenode T, recursively traverse left subtree and store X in appropriate place. d. Otherwise if X is greater than the data element of treenode T, recursively traverse right subtree and store X in appropriate place. 3. If the choice is delete, a. Accept the data element to be deleted X. b. If the X is less than Ts element then set then recursively traverse left and delete X. c. Otherwise, check if X is greater than Ts element recursively traverse right and delete X. Repeat the steps below for the above steps (b) and (c) d. If the treenode which is to be deleted has two children, then Replace the element of treenode T with the smallest data in its right subtree and recursively delete that smallest element. e. If the treenode which is to be deleted has one or zero children, then 4. Return left subtree if the right subtree is null or
Return right subtree if the left subtree is null and Finally remove the treenode T from the tree
a. Accept the Data element X to be found. b. If X is less than Ts element then continue the search in the left subtree recursively. c. Otherwise if X is greater than Ts element then continue the search in the right subtree recursively. d. Otherwise return the treenode T 5. If the choice is findmin a. b. 6. a. b. Recursively traverse the left subtree until Ts left becomes NULL. Return treenode Ts element. Recursively traverse the right subtree until Ts right becomes NULL. Return treenode Ts element.
SOURCE CODE #include<stdio.h> #include<conio.h> #include<malloc.h> struct treenode; typedef struct treenode *position; typedef struct treenode *searchtree; typedef int elementtype; position find(elementtype x,searchtree t); position findmin(searchtree t); position findmax(searchtree t); searchtree insert(elementtype x,searchtree t); searchtree delete1(elementtype x,searchtree t); struct treenode { elementtype element; searchtree left; searchtree right; }; position find(elementtype x,searchtree t) { if(t==NULL) return NULL; else if(x<t->element) return find(x,t->left); else if(x>t->element) return find(x,t->right); else return t; } position findmin(searchtree t) { if(t==NULL) return NULL; else if(t->left==NULL) return t; else return findmin(t->left); } position findmax(searchtree t) { if(t!=NULL)
while(t->right!=NULL) t=t->right; return t; } searchtree insert(elementtype x,searchtree t) { if(t==NULL) { t=malloc(sizeof(struct treenode)); if(t==NULL) { printf("\n Out of space"); exit(0); } t->element=x; t->right=t->left=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; } void display(searchtree t) { if(t==NULL) return; display(t->left); printf("\t%d",t->element); display(t->right); } searchtree deletion(elementtype x,searchtree t) { position tmpcell; if(t==NULL) { printf("\nElement not found"); return NULL; } else if(x<t->element) t->left=deletion(x,t->left); else if(x>t->element) t->right=deletion(x,t->right); else if(t->left && t->right) { tmpcell=findmin(t->right); t->element=tmpcell->element; t->right=deletion(t->element,t->right); }
return t; }
void main() { int ch,x; position p; searchtree t=NULL,min,max; clrscr(); while(1) { printf("\n1.insert\n2.delete\n3.findmin\n4.findmax\n5.display\n6.exit"); printf("\nEnter your choice"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the element"); scanf("%d",&x); t=insert(x,t); break; case 2: printf("\nEnter the element to be deleted"); scanf("%d",&x); t=deletion(x,t); break; case 3: min=findmin(t); if(min) printf("\n minimun element is %d",min->element); else printf("\n Empty tree"); break; case 4: max=findmax(t); if(max) printf("\n maximum element is %d",max->element); else printf("\n Empty tree");
break; case 5: display(t); break; case 6: exit(0); default : printf("\n Wrong choice"); } } }
OUTPUT 1.insert 2.delete 3.findmin 4.findmax 5.display 6.exit Enter your choice 1 Enter the element 12 Enter your choice 1 Enter the element 17 Enter your choice 1 Enter the element 9 Enter your choice 1 Enter the element 15 Enter your choice 1 Enter the element 8 Enter your choice 1 Enter the element 10 Enter your choice 1 Enter the element 19 Enter your choice 1 Enter the element 18 Enter your choice 1 Enter the element 11 Enter your choice 5 8 9 10 Enter your choice 3 minimum element is 8 11 12 15 17 18 19
Enter your choice 4 maximum element is 19 Enter your choice 2 Enter the element to be deleted 8 Enter your choice 5 9 10 11 Enter your choice 2 Enter the element to be deleted 14 Element not found Enter your choice 2 Enter the element to be deleted 19 Enter your choice 5 9 10 11 Enter your choice 2 Enter the element to be deleted 12 Enter your choice 5 9 10 11 Enter your choice 6 15 17 18 12 15 17 18 12 15 17 18 19
RESULT Thus the binary search tree ADT is implemented in C with the operations such as find, findmin, findmax, insert and delete and is executed and verified.