Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a program for binary search tree Solution #include #include #include /*Def

ID: 3630386 • Letter: W

Question

write a program for binary search tree

Explanation / Answer

#include #include #include /*Defining the structure for the BST*/ typedef int keyTyp; typedef struct bstTag{ keyTyp key; bstTag *lchild,*rchild; }; bstTag bstTyp,*bstPtr; /*Function to insert a key in the BST*/ void insert_node(bstTag *current_ptr,keyTyp element) { if(bstPtr==NULL){ /*Initialising the parent of the BST*/ bstTyp.key=element; bstTyp.lchild=NULL; bstTyp.rchild=NULL; bstPtr=&bstTyp; } /*Checking conditions for the preference of the key*/ else{ if(current_ptr->key>element){ if(current_ptr->lchild==NULL){ bstTag *new_node; new_node=(bstTag *)malloc(sizeof(bstTag)); new_node->key=element; new_node->lchild=NULL; new_node->rchild=NULL; current_ptr->lchild=new_node; } else insert_node(current_ptr->lchild,element); /*insert_node function used recursively for left child*/ } else if(current_ptr->keyrchild==NULL){ bstTag *new_node; new_node=(bstTag *)malloc(sizeof(bstTag)); new_node->key=element; new_node->lchild=NULL; new_node->rchild=NULL; current_ptr->rchild=new_node; } else insert_node(current_ptr->rchild,element); /*insert_node function used recursively for right child*/ } else printf("Wrong input.Key entered was already there in the BST!!! "); } } /*Function to find a key in the BST*/ void find_node(bstTag *current_ptr,keyTyp element) { if(current_ptr!=NULL){ if(current_ptr->key==element) printf("->%d The key entered is 'FOUND' in the binary search tree!!! ",element); else if(current_ptr->key>element){ printf("->%d",current_ptr->key); find_node(current_ptr->lchild,element); /*find_node function used recursively for left child*/ } else if(current_ptr->keykey); find_node(current_ptr->rchild,element); /*find_node function used recursively for right child*/ } } else printf(" The key entered is 'NOT FOUND' in the binary search tree!!! "); } main() { int choice; keyTyp element; bstPtr=(bstTag *)malloc(sizeof(bstTag)); bstPtr=NULL; getch(); printf("Enter: 1:To insert a key in the binary search tree 2:Find a key in the binary search tree 3:Quit Choice:"); scanf("%d",&choice); /*Accessing different choices*/ while(choice!=3){ if(choice==1){ printf("Enter the element or the key to insert in the BST:"); scanf("%d",&element); insert_node(bstPtr,element); printf("The key has been entered in the BST!!! "); } else if(choice==2){ printf("Enter the element or the key to find in the BST:"); scanf("%d",&element); find_node(bstPtr,element); } else printf("Wrong choice!!! "); printf(" To continue enter the choice again:"); scanf("%d",&choice); } exit(1); }