You are on page 1of 23

Data Structures and Algorithms

CSC 221

Assignment #04

Name: Saad Zafar and Fahad Rumman


Enrollment no: 01–134182-046 and 01-235172-080
Class: BS CS - 3A

Department of Computer Science


BAHRIA UNIVERSITY, ISLAMABAD
Library Management System

Book Node:

Create a BookNode class to store the Title, Author and ISBN of each book. Provide appropriate
constructors, get(), set() and display functions in the class.

Problem Description:

Write a C++ program which allows a librarian to manage data for a number of books and store
the entered data in a BST. Each node of the BST should contain an object of class BookNode.
Data is to be arranged with respect to book title.

You program should allow the librarian to perform any of the given functionalities by giving him
a menu to choose from. Basic functionalities include:
1) to add new books’ data
2) to search for a book using title as the search parameter and display all its information
3) to modify an existing book’s data
4) to delete a book’s record
5) to display the list of currently present books

BookNode.h

#include <string.h>
#include <string>
#include <iostream>
using namespace std;
class BookNode
{
private:
string title;
string author;
int ISBN;
public:

BookNode* left;
BookNode* right;
BookNode();
BookNode(string, string, int);
void setTitle(string);
string getTitle();
void setAuthor(string);
string getAuthor();
void setISBN(int);
int getISBN();
};
BookNode.cpp

#include "BookNode.h"

BookNode::BookNode()
{
title = " ";
author = " ";
ISBN = NULL;
this->left = NULL;
this->right = NULL;
}
BookNode::BookNode(string title, string author, int ISBN)
{
this->ISBN = ISBN;
this->title = title;
this->author = author;
this->left = NULL;
this->right = NULL;
}
void BookNode::setTitle(string t)
{
title = t;
}
string BookNode::getTitle()
{
return title;
}
void BookNode::setAuthor(string a)
{
author = a;
}
string BookNode::getAuthor()
{
return author;
}
void BookNode::setISBN(int i)
{
ISBN = i;
}
int BookNode::getISBN()
{
return ISBN;
}
BST.h

#include"BookNode.h"
class BST
{
public:
BookNode* root;
BST();
BST(string, string, int);
void AddBook(BookNode**, string, string, int);
BookNode* findMin(BookNode*);
BookNode* getParent(BookNode*, string);
void DeleteBook(BookNode**, string);
bool SearchBook(BookNode*, string);
void Display(BookNode*);
void ModifyBook(BookNode*, string);
};
BST.cpp

#include "BST.h"
#include <conio.h>
BookNode bn;
BST::BST()
{
this->root = NULL;
}
BST::BST(string author, string title, int ISBN)
{
this->root = new BookNode(author, title, ISBN);
}
void BST::AddBook(BookNode** curr, string author, string title, int ISBN)
{
if (*curr == NULL)
{
BookNode* node = new BookNode(author, title, ISBN);
*curr = node;
}
else
{
if (title.compare((*curr)->getTitle()) < 0)
{
AddBook(&(*curr)->left, author, title, ISBN);
}
if (title.compare((*curr)->getTitle()) >= 0)
{
AddBook(&(*curr)->right, author, title, ISBN);
}
}
}
BookNode* BST::getParent(BookNode* curr, string title)
{
if (curr == NULL)
return NULL;
else if ((curr->left != NULL) && (curr->left->getTitle() == title))
return curr;
else if ((curr->right != NULL) && (curr->right->getTitle() == title))
return curr;
else if (title.compare(curr->getTitle()) < 0)
return getParent(curr->left, title);
if (title.compare(curr->getTitle()) >= 0)
return getParent(curr->right, title);
}
BookNode* BST::findMin(BookNode* curr)
{
if (curr->left == NULL)
return curr;
else
return findMin(curr->left);
}
void BST::DeleteBook(BookNode** curr, string title)
{
if (*curr == NULL)
{
cout << " Book is not found" << endl;
}
else
{
if (title < (*curr)->getTitle())
{
DeleteBook(&(*curr)->left, title);
}
else if (title > (*curr)->getTitle())
{
DeleteBook(&(*curr)->right, title);
}
else if (title == (*curr)->getTitle())
{
if (((*curr)->left == NULL) && ((*curr)->right == NULL))
{
BookNode* p = getParent(*curr, title);
if (p->left == *curr)
{
p->left = NULL;
}
else if (p->right == *curr)
{
p->right = NULL;
}
*curr = NULL;
delete* curr;
}
else if ((((*curr)->left == NULL) && ((*curr)->right != NULL)) ||
(((*curr)->left != NULL) && ((*curr)->right == NULL)))
{
BookNode* p = getParent(root, (*curr)->getTitle());
if ((*curr)->left != NULL)
{
if (p->left == *curr)
{
p->left = (*curr)->left;
*curr = p->left;
}
else if (p->right == *curr)
{
p->right = (*curr)->left;
*curr = p->right;
}
}
else if ((*curr)->right != NULL)
{
if (p->left == *curr)
{
p->left = (*curr)->right;
*curr = p->right;
}
else if (p->right == *curr)
{
p->right = (*curr)->right;
*curr = p->right;
}
}
}
else if (((*curr)->left != NULL) && ((*curr)->right != NULL))
{
BookNode* min = findMin((*curr)->right);
DeleteBook(&(*curr), min->getTitle());
(*curr)->getTitle() = min->getTitle();
}
}
}
}

void BST::ModifyBook(BookNode* curr, string title)


{
string tt=" ", auth=" ";
int i = 0;

if (curr == NULL)
{
cout << "Not found " << endl;
}
else if (curr->getTitle() == title)
{
cout << "Enter new title to modify :"; cin.ignore(); cout << endl;
getline(cin, tt);
curr->setTitle(tt);
cout << "Enter new author to modify: "; cin.ignore(); cout << endl;
getline(cin, auth);
curr->setAuthor(auth);
cout << "Enter new ISBN to modify: " << endl;
cin >> i;
curr->setISBN(i);
}
else if (curr->getTitle() > title)
{
ModifyBook(curr->left, title);
}
else if (curr->getTitle() < title)
{
ModifyBook(curr->right, title);
}

}
bool BST::SearchBook(BookNode* curr, string title)
{
if (curr == NULL)
return false;
else if (curr->getTitle() == title)
{
cout << "The following book exists in the library!\nBook tile: " << curr-
>getTitle() << "\tAuthor's name: " << curr->getAuthor() << "\tISBN: " << curr->getISBN();
cout << endl;
}
else if (curr->getTitle() > title)
{
SearchBook(curr->left, title);
}
else if (curr->getTitle() < title)
{
SearchBook(curr->right, title);
}
cout << "The following book does not exist in the library";
}
void BST::Display(BookNode* curr)
{
if (curr == NULL)
return;
Display(curr->left);
cout << "------------------------------------------------";
cout << "\nBook tile: " << curr->getTitle() << "\tAuthor's name: " << curr-
>getAuthor() << "\tISBN: " << curr->getISBN() << endl;
Display(curr->right);
}

Main.cpp

#include "BST.h"
int main()
{
BST b;
int choice;
string t, a;
int i;
while (1)
{
cout << "\nPress 1 to add new books' data\nPress 2 to to search for a book
by its title\nPress 3 to to modify an existing book's data\nPress 4 to delete a books'
record\nPress 5 to display the list of currently present books\nPress 6 to exit" << endl;
cin >> choice;
switch (choice)
{
case 1:
{
cout << "Enter the title: ";
cin.ignore(); cout << endl;
getline(cin, t);
cout << "Enter the name of author: "; getline(cin, a);
cout << "Enter the ISBN number: "; cin >> i;
b.AddBook(&b.root, t, a, i);
cout << "Book is added!" << endl;
break;
}
case 2:
{
cout << "Enter the name of book title: "; cin.ignore(); cout <<
endl;
getline(cin, t);
b.SearchBook(b.root, t);
break;
}
case 3:
{
cout << "Enter the OLD title to modify: "; cin.ignore(); cout <<
endl;
getline(cin, t);
b.ModifyBook(b.root, t);
cout << "Data of the book is successfully updated.";
break;
}
case 4:
{
cout << "Enter the title of the book to delete: "; cin.ignore();
cout << endl;
getline(cin, t);
b.DeleteBook(&b.root, t);
cout << "The book is successfully deleted from the record.";
break;
}
case 5:
{
b.Display(b.root);
break;
}
case 6:
{
exit(0);
}
}
}
return 0;
}
Outputs:

Insertion of Book:

Deletion of Book:
Searching of book:

Modification of a book:
s
#pragma once
#include"BookNode.h"
class BinarySearchTree
{
public:
Node* root;
BinarySearchTree();
bool is_empty();
void insert(Node* root, string, string, int);
void Modify(Node* root, string);
void Search(Node* root, string);
Node* deletenode(Node* root, string);
void PresentBooks(Node* root);
Node* findminimum(Node* curr_ptr);
Node* getparent(Node* curr_ptr, string );
void author(Node* root, string);

};

#include "BinarySearchTree.h"

BinarySearchTree::BinarySearchTree()
{
root = NULL;
}

bool BinarySearchTree::is_empty()
{
if (root == NULL)
return true;
else
return false;
}

void BinarySearchTree::insert(Node* ptr, string ttl, string auth, int isbn)


{
Node* prev = NULL;
while (ptr != NULL)
{
prev = ptr;
if (ttl.compare(ptr->Title) < 0)
{
ptr = ptr->left;
}
else if (ttl.compare(ptr->Title) > 0)
{
ptr = ptr->right;
}
else
{
cout << "Already exist";
return;
}
}

Node* obj = new Node;


obj->Title = ttl;
obj->Author = auth;
obj->ISBN = isbn;
obj->right = 0;
obj->left = 0;

if (prev == 0)
{
root = obj;
}
else if (ttl.compare(prev->Title) < 0)
{
prev->left = obj;
}
else if (ttl.compare(prev->Title) > 0)
{
prev->right = obj;
}

void BinarySearchTree::Search(Node* obj, string ttl)


{
if (obj == NULL)
{
return;
}
else if (obj->Title.compare(ttl) == 0)
{
cout << obj->Title << " , " << obj->Author << " , " << obj->ISBN;
}
else if (obj->Title.compare(ttl) > 0)
{
return Search(obj->left, ttl);
}

else if (obj->Title.compare(ttl) < 0)


{
return Search(obj->right, ttl);
}

void BinarySearchTree::Modify(Node* obj, string ttl)


{
if (obj == NULL)
{
return;
}

else if (obj->Title.compare(ttl) == 0)
{
obj->Title = "Last Knights ";
obj->Author = "Usman";
obj->ISBN = 23;
}

else if (obj->Title.compare(ttl) < 0)


{
return Modify(obj->right, ttl);
}

else if (obj->Title.compare(ttl) > 0)


{
return Modify(obj->left, ttl);
}
}

Node* BinarySearchTree::deletenode(Node* obj, string ttl)


{
while (obj != NULL)
{
if (ttl.compare(obj->Title) < 0)
{
obj = obj->left;
}
else if (ttl.compare(obj->Title) > 0)
{
obj = obj->right;
}

else if ((obj->Title.compare(ttl) == 0))


{
Node* Parent;
Node* Mini;
string name;
string book;
int id;

if ((obj->left == NULL && obj->right == NULL))


{
Parent = getparent(root, obj->Title);
if (Parent->left == obj)
{
Parent->left = NULL;
}
else if (Parent->right == obj)
{
Parent->right = NULL;
}

delete obj;
}

else if ((obj->left == NULL && obj->right != NULL) || (obj->right ==


NULL && obj->left != NULL))
{
Parent = getparent(root, obj->Title);
if (obj->left != NULL)
{
if (Parent->left == obj)
{
Parent->left = obj->left;
}
else if (Parent->right == obj)
{
Parent->right = obj->left;
}
}

else if (obj->right != NULL)


{
if (Parent->left == obj)
{
Parent->left = obj->right;
}
else if (Parent->right == obj)
{
Parent->right = obj->right;
}
}
delete obj;
}

else if (obj->left != NULL && obj->right != NULL)


{
Mini = findminimum(obj->right);
book = Mini->Title;
name = Mini->Author;
id = Mini->ISBN;
deletenode(obj->right, Mini->Title);
//Recursion will happen

obj->Title = book;
obj->Author = name;
obj->ISBN = id;
}
if (obj == NULL)
{
cout << "No Node found";
return obj;
}
}

}
}

void BinarySearchTree::PresentBooks(Node* root)


{
if (root == NULL)
return;
else
{
PresentBooks(root->left);
cout << root->Title << " " << root->Author << " " << root->ISBN << endl;
PresentBooks(root->right);
}
}

void BinarySearchTree::author(Node* root, string str)


{
if (root == NULL)
return;
else
{
author(root->left, str);
if (str.compare(root->Author) == 0)
{
cout << root->Title << endl;
}
author(root->right, str);
}

}
Node* BinarySearchTree::findminimum(Node* curr)
{
if (curr->left == NULL)
{
return curr;
}
else
{
return findminimum(curr->left);
}

Node* getparent(Node* curr_ptr, string ttl)


{
if ((curr_ptr->left->Title.compare(ttl) == 0) || (curr_ptr->right->Title == ttl))
{
return curr_ptr;
}

else if (ttl.compare(curr_ptr->Title) < 0)


{
return getparent(curr_ptr->left, ttl);
}
else if (ttl.compare(curr_ptr->Title) > 0)
{
return getparent(curr_ptr->right, ttl);
}
}

#include<iostream>
using namespace std;
class Node
{
public:
string Title, Author;
int ISBN;
Node* right, *left;

Node()
{
left = NULL;
right = NULL;
}

Node(string ttl, string auth, int id)


{
Title = ttl;
Author = auth;
ISBN = id;
right = NULL;
left = NULL;

};
#include<iostream>
#include"BookNode.h"
#include"Binarysearchtree.h"
using namespace std;
int main()
{
BinarySearchTree obj1, obj2;
obj1.insert(obj1.root, "Last Knights " ,"Usman", 1785);
obj1.insert(obj1.root, "To Kill a Mockingbird", "Saud", 2109);
obj1.insert(obj1.root, "The Lord of the Rings", " Danyal ", 2134);
obj1.insert(obj1.root, "The Great Gatsby", "Aqsa", 2387);
obj1.PresentBooks(obj1.root);
cout << obj1.root->Author;
system("pause");
return 0;

You might also like