0% found this document useful (0 votes)
1 views

Binary Search Tree ex no 10 algorithm

Uploaded by

lavanya a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Binary Search Tree ex no 10 algorithm

Uploaded by

lavanya a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EX.NO 10.

Binary Search Tree Algorithm


STEP 1: Start the program
STEP 2: Create a structure of struct tree
STEP 3: Declare data, left, right
STEP 4: Create a typedef structure of struct tree node
STEP 5: Declare t=NULL,u, c=0
STEP 6: Declare the function such as insert, delete, minimum, find,
Maximum, inorder
STEP 7: Display the menu
1.INSERTION
2.DELETION
3.SEARCHING
4.MINIMUM ELEMENT
5.MAXIMUM ELEMENT
6.EXIT
STEP 8: Read choice, create a switch case
STEP 8.1: Get the choice 1 read ele call ins() and repeat it until ch ==y or ch== Y call
inorder() goto step 7
STEP 8.2: Get the choice 2 read ele call find() if c==1 call deletion() else print element not
found goto step 7
STEP 8.3: Get the choice 3 read ele call find() if c==1 print element found else print
element not found goto step 7
STEP 8.4: Get the choice 4 call min() and print minimum no goto step 7
STEP 8.5: Get the choice 5 call max() goto step 7
STEP 8.6: Get the choice 6 Exit program
STEP 9: Stop the program
ins(t,x):
Step 1.Create the memory allocation for structure of struct tree node
2.if (t==NULL) otherwise goto step 3
2.1:newnode->data=x
2.2: newnode->left=NULL

1
2.3: newnode->right=NULL
2.4: t=newnode
3.else
3.1: if(x<=t->data) otherwise goto 3.2
Step 3.1.1. t->left=call ins(t->left,x)
3.2: else
Step 3.2.1 t->right=call ins(t->right,x)
Step 4 : end of ins method
Inorder(t):
1.if (t!=NULL) otherwise goto step 2
1.1: call inorder(t->left)
1.2: print t->data
1.3: call inorder(t->right)
Step 2 :end of inorder() method
Min(t):
1.if(t!=NULL) otherwise goto step 2
1.1: if(t->left==NULL)
1.1.1: return t
1.2: else
1.2.1: return call min(t->left)
Step 2 : end of min() method
Max(t):
1.if(t!=NULL) otherwise goto step 2
1.1:if(t->right==NULL)
1.1.1: print The maximum element in tree
1.2:else
1.2.1:call max(t->right)
Step 2 : end of max() method
Find(t,x):
1.if(t==NULL) otherwise goto step 2
1.1:c=-1;

2
2.else
2.1:if(t->data==x)otherwise goto step 2.2
2.1.1:c=1;
2.2: if(x<t->data)
2.2.1:call find(t->left,x)
2.3: if(x>t->data)
2.3.1:call find(t->right,x)
Step 3 : end of find() method

Del(t,x):
1. Declare tmp
2. Call find(t,x)
3. If c==1 otherwise goto step 4
3.1: if(x<t->data)otherwise goto step 3.2
3.1.1:t->left=call del(t->left,x)
3.2:if(x>t->data) otherwise goto step 3.3
3.2.1: t->right=call del(t->right,x)
3.3: if(t->left and t->right)
3.3.1: tmp=call min(t->right);
3.3.2: t->data=tmp->data;
3.3.3:t->right=call del(t->right,t->data)
4.otherwise tmp=t
4.1: if(t->left==NULL) otherwise goto step 4.2
4.1.1:t=t->right
4.2 : if(t->right==NULL)
Step 4.2.1:t = t->left
4.3: free(tmp)
5.return t
6.end of del() method.

You might also like