Professional Documents
Culture Documents
Data Structures and Algorithms Assignment #04: Bahria University, Islamabad
Data Structures and Algorithms Assignment #04: Bahria University, Islamabad
CSC 221
Assignment #04
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();
}
}
}
}
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;
}
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;
}
else if (obj->Title.compare(ttl) == 0)
{
obj->Title = "Last Knights ";
obj->Author = "Usman";
obj->ISBN = 23;
}
delete obj;
}
obj->Title = book;
obj->Author = name;
obj->ISBN = id;
}
if (obj == NULL)
{
cout << "No Node found";
return obj;
}
}
}
}
}
Node* BinarySearchTree::findminimum(Node* curr)
{
if (curr->left == NULL)
{
return curr;
}
else
{
return findminimum(curr->left);
}
#include<iostream>
using namespace std;
class Node
{
public:
string Title, Author;
int ISBN;
Node* right, *left;
Node()
{
left = NULL;
right = 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;