0% found this document useful (0 votes)
51 views12 pages

Maham Dsa Lab 2

The document describes implementing a binary search tree (BST) for a library management system. It includes code to create BST nodes, add nodes, search, delete, and print the tree level-order. Example inputs and outputs are provided for inserting books into the tree, printing the tree structure, and performing searches and deletions.

Uploaded by

Maham Fatima
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)
51 views12 pages

Maham Dsa Lab 2

The document describes implementing a binary search tree (BST) for a library management system. It includes code to create BST nodes, add nodes, search, delete, and print the tree level-order. Example inputs and outputs are provided for inserting books into the tree, printing the tree structure, and performing searches and deletions.

Uploaded by

Maham Fatima
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/ 12

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;

q.push(root);

q.push(NULL);

cout<<"\n level : "<<level<<endl;


while(!q.empty())

bstnode* node=q.front();

q.pop();

if(node!=NULL)

cout<<"\nid : "<<node->id<<" ,name : "<<node->name;

if(node->left)

q.push(node->left);

if(node->right)

q.push(node->right);

else if(!q.empty())

level++;

cout<<"\n level : "<<level<<endl;

q.push(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;

t1.insert(id,name);

else if(num==2)
{

cout<<"\n\t\t Enter id to delete ";

cin>>id;

t1.delet(id);

else if(num==3)

cout<<"\n\t\t Enter id to search ";

cin>>id;

(t1.search(id));//display itself

else if(num==4)

t1.printlevelorder();

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 :

You might also like