Create a binary tree of order link list


Discuss the following:

Q: The program below is an ordered link list. Summary report menu selection shows all books  in sorted alpabetical order and report by year displays books in ascending order by publish year. Keep this format, but modify the program making it an Binary tree instead of an ordered link list. You may use one Binary Tree or two.

#include
#include
using namespace std;


class Book
{
protected:
string title;
int iD;
int year_pub;
string author;
string synopsis;
int linkTitle;
int linkYear;
public:
Book(string title,int iD,int year_pub,string author,string synopsis);
string getTitle();
int getId();
int getYear_Pub();
string getAuthor();
string getSynopsis();
int getNextTitleLink();
int getNextYearLink();
friend int sortByTitle ();
friend int sort_by_year ();
};

int sort_by_year ();
int sortByTitle ();

//Maximum number of books in the system
const int MAX_RECORDS = 10;

//This is an array that holds pointer to book classes
Book* Books[MAX_RECORDS];

//holds record count in Books Table
int RecordCountBooks = 0;


int main()
{
//Prepare the book library. Potentially could ready from file
Books[RecordCountBooks++] = new Book("Zombie Tales",1263,2007,"John Smith","A wide variety of short zombie stories in a massive collection.");
Books[RecordCountBooks++] = new Book("Forbidden Planet",1264,1996,"Sally Lewis","The earth has been ruined by war and humans seek");
Books[RecordCountBooks++] = new Book("Robots Vs Humans",1265,2002,"Nuke Hammil","Robots created by humans begin attacking their owners.");
Books[RecordCountBooks++] = new Book("Winter Nights",1266,1995,"Eric Robertson","A story of infidelity in the household.");
Books[RecordCountBooks++] = new Book("Bloody Weekend",1267,1999,"Mark Harris","A madman goes on a bloody killing spree.");

int headTitleList = sortByTitle ();
int headYearList = sort_by_year ();
int listPtr;

int choice,year;
do
{
cout< <<"============================================================="< <<"choose 1 to see a summary of all of the in our database"< <<"choose 2 to search a book by the Year published"< <<"choose 3 to Report by Year"< <<"choose 4 to exit this program"< <<"Enter your choice and press return:";
cin >> choice;

switch(choice)
{
case 1:
cout< cout<<"We currently have in our library database "<< RecordCountBooks << " books " < cout<

//list the books in sorted order - by Title
listPtr = headTitleList;
while(listPtr != -1)
{
Book *bk = Books[listPtr];
cout<<"Title:" << bk->getTitle()< cout<<"Book ID:" << bk->getId()< cout<<"Year Published:" << bk->getYear_Pub()< cout<<"Author:" << bk->getAuthor()< cout<<"Synopsis:" << bk->getSynopsis() < cout< listPtr = bk->getNextTitleLink();
}
cout< break;
case 2:
{
cout< cout<<"Please enter the year you would like to search"< cin >>year;

int count = 0;
for(int i=0; i {
Book *bk = Books[i];
if (bk->getYear_Pub() == year)
{
if (count == 0)
{
cout<<"The followiing books published on this year were found:\n"< }
count++;
cout<<"Title:" << bk->getTitle()< cout<<"Book ID:" << bk->getId()< cout<<"Year Published:" << bk->getYear_Pub()< cout<<"Author:" << bk->getAuthor()< cout<<"Synopsis:" << bk->getSynopsis() < cout< }
}
if (count==0)
{
cout<<"Books published on this year were not found.\n"< }
cout< }
break;
case 3:
cout< cout<<"We currently have in our library database "<< RecordCountBooks << " books " < cout<

//list the books in sorted order - by Year of Publication
listPtr = headYearList;
while(listPtr != -1)
{
Book *bk = Books[listPtr];
cout<<"Title:" << bk->getTitle()< cout<<"Book ID:" << bk->getId()< cout<<"Year Published:" << bk->getYear_Pub()< cout<<"Author:" << bk->getAuthor()< cout<<"Synopsis:" << bk->getSynopsis() < cout< listPtr = bk->getNextYearLink();
}
cout< break;
case 4:
cout<<"End of program."< break;
default:
cout<<"Not a valid choice.\nChoose again."< }
}while(choice != 4);

//release memory
for(int i=0; i {
Book *bk = Books[i];
delete bk;
}

return 0;
}
Book::Book(string title,int iD,int year_pub,string author, string synopsis)
{
this->title = title;
this->iD = iD;
this->year_pub = year_pub;
this->author = author;
this->synopsis = synopsis;
this->linkTitle = -1;
this->linkYear = -1;
}
string Book ::getTitle()
{
return title;
}
int Book ::getId()
{
return iD;
}
int Book ::getYear_Pub()
{
return year_pub;
}
string Book ::getAuthor()
{
return author;
}
string Book ::getSynopsis()
{
return synopsis;
}
int Book ::getNextTitleLink()
{
return linkTitle;
}
int Book ::getNextYearLink()
{
return linkYear;
}

int sortByTitle ()
{
int listHead = -1;

if (RecordCountBooks > 0)
{
//First book in the ordered linked list.
//Start with first array element.
listHead = 0;
}

//Insert remaining books in the ordered linked list, one by one.
//Insertion sort is being used to order the books by title.
for(int i=1; i {
//Find ith book's position in ordered linked list.
int prevNode = -1;
int listPtr = listHead;

while (listPtr != -1)
{
if (Books[i]->title < Books[listPtr]->title)
break;
else
{
prevNode = listPtr;
listPtr = Books[listPtr]->linkTitle;
}
}

if (prevNode == -1)
{
//Insert the ith book at the head of the ordered linked list.
Books[i]->linkTitle = listHead;
listHead = i;
}
else
{
Books[i]->linkTitle = Books[prevNode]->linkTitle;
Books[prevNode]->linkTitle = i;
}
}

return listHead;
}

int sort_by_year ()
{
int listHead = -1;

if (RecordCountBooks > 0)
{
//First book in the ordered linked list.
//Start with first array element.
listHead = 0;
}

//Insert remaining books in the ordered linked list, one by one.
//Insertion sort is being used to order the books by year of publication.
for(int i=1; i {
//Find ith book's position in ordered linked list.
int prevNode = -1;
int listPtr = listHead;

while (listPtr != -1)
{
if (Books[i]->year_pub < Books[listPtr]->year_pub)
break;
else
{
prevNode = listPtr;
listPtr = Books[listPtr]->linkYear;
}
}

if (prevNode == -1)
{
//Insert the ith book at the head of the ordered linked list.
Books[i]->linkYear = listHead;
listHead = i;
}
else
{
Books[i]->linkYear = Books[prevNode]->linkYear;
Books[prevNode]->linkYear = i;
}
}

return listHead;
}

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Create a binary tree of order link list
Reference No:- TGS01937076

Now Priced at $25 (50% Discount)

Recommended (94%)

Rated (4.6/5)