Name : Maham Fatima
Class : BSCS III
Roll no : 222201002
Assingment no : 2
Subject : DSA Lab
Question:
Tasks:Implementing a Binary Search and customising the code for library management system. Use
book id, isbn and book name to store the details in the binary search tree.
1. Print the tree in a hierarchical format
2. Insert data in tree.
3. Search books inside the tree.
4. Delete books from the tree
Code:
#include<iostream>
#include<queue>
using namespace std;
struct bstnode{
int id;
string name;
bstnode *left,*right;
};
class bst{
bstnode *root;
bstnode* getbstnode(int num,string name)
bstnode* temp=new bstnode();
temp->id=num;
temp->name=name;
temp->left=temp->right=NULL;
return temp;
bstnode* addnode(int num,string name,bstnode* root)
if(root==NULL)
root = getbstnode(num,name);
else if(root->id<num)
root->right=addnode(num,name,root->right);
else
root->left=addnode(num,name,root->left);
return root;
bool check(int num,bstnode* root)
if(root==NULL)
cout<<"\n\t Invalid id";
return false;
else if(root->id==num)
{
cout<<"\n\t"<<root->id<<" , "<<root->name;
return true;
else if(root->id>num)
return check(num,root->left);
else
return check(num,root->right);
void displaynode(bstnode* tmp)
cout<<"\n\t Book id : "<<tmp->id;
cout<<"\t name : "<<tmp->name;
bstnode* deletinbst(int num,bstnode* root)
if(num<root->id)
root->left=deletinbst(num,root->left);
else if(num>root->id)
root->right=deletinbst(num,root->right);
}
else//only if equal to num which is id
if(root->left==NULL)
return root->right;
else if(root->right==NULL)
return root->right;
bstnode* tmp=inor(root->right);
root->id=tmp->id;
root->name=tmp->name;
root->right=deletinbst(tmp->id,root->right);
bstnode* inor(bstnode* root)
bstnode* temp=root;
while(temp!=NULL&&temp->left!=NULL)
temp=temp->left;
return temp;
public:
bst()
root=NULL;
void delet(int num)
root=deletinbst(num,root);
void insert(int num,string name)
root=addnode(num,name,root);
bool search(int num)
return check(num,root);
void printlevelorder()
{bstnode * root=this->root;
int level=0;
if(root==NULL)
return ;
queue<bstnode*> q;
[Link](root);
[Link](NULL);
cout<<"\n level : "<<level<<endl;
while(![Link]())
bstnode* node=[Link]();
[Link]();
if(node!=NULL)
cout<<"\nid : "<<node->id<<" ,name : "<<node->name;
if(node->left)
[Link](node->left);
if(node->right)
[Link](node->right);
else if(![Link]())
level++;
cout<<"\n level : "<<level<<endl;
[Link](NULL);
};
int main()
{
bst t1;
int id,num;
string name;
start:
system("cls");
cout<<"\n\t------------------------------------------------------";
cout<<"\n\t-------------Library Management System----------------";
cout<<"\n\t------------------------------------------------------";
cout<<endl;
cout<<"\n\t\t press 1 to add book ";
cout<<"\n\t\t press 2 to delet a book ";
cout<<"\n\t\t press 3 to search a book ";
cout<<"\n\t\t press 4 to print all book ";
cout<<"\n\t\t press any other number to exit ";
cout<<"\n\t\t Enter number : ";
cin>>num;
if(num==1)
cout<<"\n\t\t Enter id of book ";
cin>>id;
cout<<"\n\t\t Enter name of book ";
cin>>name;
[Link](id,name);
else if(num==2)
{
cout<<"\n\t\t Enter id to delete ";
cin>>id;
[Link](id);
else if(num==3)
cout<<"\n\t\t Enter id to search ";
cin>>id;
([Link](id));//display itself
else if(num==4)
[Link]();
else
exit(0);
system("pause");
goto start;
return 0;
Output:
input:
(inserting following)
(50 , frost) , (89 , ledge) , (34 , wanderlove) , (23 , roomies) , (90 , hunted) , (79 , captivate) ,
(2 , winter) , (09 , ingarnate) , (78 , shiver) , ( 10 , boomerang )
adding one by one
50
(l50)34 89(r50)
(l34)23 (l89)79 , 90(r89)
(l23)2 (l79)78
9(r2)
(l9)10
(l89 means left child of 89 , similarly r89 mean right child of 89 )
Adding
Printing
Deleting
Searching :