Lab 7: Binary Search Tree Part 2 (100 pts)
Due Tuesday, May 30 at 1:20pm on Catalyst

Book.h and Book.cpp Files
  • Copy and paste the below code into a file named Book.h. Make sure this file is inside the same folder as your BST.h file in Eclipse.
  • Fill in the missing functions, using the concept of operator overloading.
  • #ifndef BOOK_H_
    #define BOOK_H_

    #include <string>
    #include <ostream>
    using namespace std;

    class Book {
    private:
        string title;
        string author;
        unsigned isbn;
        double price;

    public:

    /**Constructors*/
        Book();
        Book(string t, string a, unsigned i, double p);


        /**Access Functions*/
        string get_title();
        string get_author();
        unsigned get_isbn();
        double get_price();

        /**Manipulation Procedures*/
        void set_title(string t);
        void set_author(string a);
        void set_isbn(unsigned i);
        void set_price(double p);


    /**Additional Functions*/
        
        friend ostream& operator<<(ostream& os, const Book& book);
        //prints out a book to the designated stream in the following format
        //<title> by <author>
        //$<price>
        //isbn #<isbn>
        //note that the << is required to be a friend function, not a member function
        //note2: do not print out the <> as part of the output

        bool operator==(const Book& book);
        //compares two books to determine if they are the same book
        
        bool operator<(const Book& book);
        //compares two books to determine if one comes before the other
        //alphabetically by title and secondarily by author if the two
        //books contain the same title
        //returns false if the two books are the same
        
        bool operator>(const Book& book);
        //compares two books to determine if one comes after the other
        //alphabetically by title and secondarily by author if the two
        //books contain the same title
        //returns false if the two books are the same

    };


    #endif /* BOOK_H_ */

  • Additionally, copy and paste the following code into a file named Book.cpp.
  • Please do not alter this file in any way or you may not receive credit for this Lab. You must only add to it.
  • You are required to implement the <, > and << functions.
  • For more information about operator overloading for the << function, see this article


#include "Book.h"
#include <iostream>
#include <iomanip>

Book::Book():title(""), author(""), isbn(0), price(0.0){};

Book::Book(string t, string a, unsigned i, double p) {
    title = t;
    author = a;
    isbn = i;
    price = p;
}

/**Access Functions*/

string Book::get_title() {
    return title;
}

string Book::get_author() {
    return author;
}

unsigned Book::get_isbn() {
    return isbn;
}

double Book::get_price() {
    return price;
}

/**Manipulation Procedures*/

void Book::set_title(string t){
    title = t;
}

void Book::set_author(string a) {
    author = a;
}

void Book::set_isbn(unsigned i) {
    isbn = i;
}

void Book::set_price(double p) {
    price = p;
}

/**Additional Functions*/

bool Book::operator==(const Book& book) {
    return (title == book.title && author==book.author);
}

//To-do: Add <, > and << here

Implementation of BST.cpp
  • Create a file named BST.cpp in which you have a main function which opens a file, reads the contents of the file into a BST and then performs operations on the tree, the result of which you then write to an output file.
  • To follow along with the example, the name of your file must be books1.txt and it must contain the following.
  • Note: recall that to add a text file to your Eclipse project, right-click on the project main folder, then go to New-> File and follow the prompts.

Pride and Prejudice
Jane Austen
10.95
2345678

Bleak House
Charles Dickens
8.99
3567897

A Room with a View
E.M. Forster
7.50
1177889

Jane Eyre
Charlotte Bronte
7.90
2345674

The Woman in White
Wilkie Collins
10.75
3256789

Lady Audley's Secret
Mary Elizabeth Braddon
5.50
1212134

Gulliver's Travels
Jonathan Swift
6.80
3434343

Middlemarch
George Elliot
12.50
1256743

Vanity Fair
William Thackery
11.99
3456839

  • Now create a second input file called books2.txt and copy and paste the following content into it:

Pedro Paramo
Juan Rulfo
8.95
1123456

Como agua para chocolate
Laura Esquivel
6.90
2375689

La casa de los espiritus
Isabel Allende
7.50
2117889

Los diarios de motocicleta
Ernesto Che Guevara
10.99
2241174

Cien anos de soledad
Gabriel Garcia Marquez
9.75
3556789

El alquimista
Paulo Coelho
8.50
3212132

La casa en Mango Street
Sandra Cisneros
6.80
1134231

El beso de la mujer arana
Manuel Puig
7.75
3456743

Cajas de carton
Francisco Jimenez
10.99
2256839


Expected UI:

  • Create a file named BST.cpp which will implement a user interface named the Best Reads App
  • Note that your program must work identically to the output below for each file (see extensive examples below)
  • Your user interaction must follow these steps:
  1. Prompt the user for the name of file
    1. Use a loop to prompt the user for a valid file name if the file entered is not valid
  2. Prompt the user for the genre
  3. Print out the number of books in that tree, referring to the genre (as shown below)
  4. Print out the height of the tree (referring to the genre as shown below).
  5. Print out the first book in the collection by calling minimum
  6. Print out the last book in the collection by calling maximum
  7. Prompt the user to enter the title and author of a book to remove
  8. Search the tree to see if it contains a book with the entered title and author
    1. Repeatedly prompt the user for a valid title and author if the input is not contained in the tree
  9. Remove the requested book from the tree
  10. Again print out the number of books in the tree (should be one less)
  11. Do an inOrderPrint of the tree to the console
  12. Prompt the user for a title, author, isbn and price of a book to insert
  13. Insert this new book
  14. Again print out the number of books in the tree (should be one less)
  15. Do an inOrderPrint of the tree to the console
  16. Ask the user whether he or she would like to write the current BST to a file
    1. If input is yes, Yes, y, or Y, prompt for the name of the file
    2. Write the bst out to a file using preOrderPrint
  17. Print out "Thank you! Goodbye!"
  • Note that spacing and wording must be identical to the examples below to receive full credit


book1.txt required output (user input will vary)

Welcome to the Best Reads App!

Enter the name of the file for your book collection: boks1.txt
Invalid file name!

Please enter the name of your file: boooks1.txt
Invalid file name!

Please enter the name of your file: books1.txt
What genre of literature is this: 19th Century British

The number of books in the 19th Century British book tree is: 9
The height of the 19th Century British book tree is: 4

The first book in this collection is:
A Room with a View by E.M. Forster
$7.50
ISBN: 1177889

The last book in this collection is:
Vanity Fair by William Thackery
$11.99
ISBN: 3456839

Enter the information about the book you wish to remove.
Title: The Castle of Otranto
Author: Horace Walpole
Sorry, The Castle of Otranto is not in this collection!

Enter the title of a book you wish to remove: Gulliver's Travels
Author: Jonathan Swift
Gulliver's Travels has been removed!

The number of books in the 19th Century British book tree is now: 8

The books contained in this tree are now:

A Room with a View by E.M. Forster
$7.50
ISBN: 1177889

Bleak House by Charles Dickens
$8.99
ISBN: 3567897

Jane Eyre by Charlotte Bronte
$7.90
ISBN: 2345674

Lady Audley's Secret by Mary Elizabeth Braddon
$5.50
ISBN: 1212134

Middlemarch by George Elliot
$12.50
ISBN: 1256743

Pride and Prejudice by Jane Austen
$10.95
ISBN: 2345678

The Woman in White by Wilkie Collins
$10.75
ISBN: 3256789

Vanity Fair by William Thackery
$11.99
ISBN: 3456839

Enter the title of a book you wish to insert: The Castle of Otranto
Enter the author of the book: Horace Walpole
Enter the price of the book: 6.99
Enter the isbn number of the book: 3434562

The number of books in the 19th Century British book tree is now: 9

The books contained in this tree are now:

A Room with a View by E.M. Forster
$7.50
ISBN: 1177889

Bleak House by Charles Dickens
$8.99
ISBN: 3567897

Jane Eyre by Charlotte Bronte
$7.90
ISBN: 2345674

Lady Audley's Secret by Mary Elizabeth Braddon
$5.50
ISBN: 1212134

Middlemarch by George Elliot
$12.50
ISBN: 1256743

Pride and Prejudice by Jane Austen
$10.95
ISBN: 2345678

The Castle of Otranto by Horace Walpole
$6.99
ISBN: 3434562

The Woman in White by Wilkie Collins
$10.75
ISBN: 3256789

Vanity Fair by William Thackery
$11.99
ISBN: 3456839

Would you like to write this collection to a file? (yes/no): Yes
Enter the file name: output.txt
The data is now written to the file output.txt

Thank you! Goodbye!


Matching outfile.txt file which will contain the preorder print of the bst for book1.txt

Pride and Prejudice by Jane Austen
$10.95
ISBN: 2345678

Bleak House by Charles Dickens
$8.99
ISBN: 3567897

A Room with a View by E.M. Forster
$7.50
ISBN: 1177889

Jane Eyre by Charlotte Bronte
$7.90
ISBN: 2345674

Lady Audley's Secret by Mary Elizabeth Braddon
$5.50
ISBN: 1212134

Middlemarch by George Elliot
$12.50
ISBN: 1256743

The Woman in White by Wilkie Collins
$10.75
ISBN: 3256789

The Castle of Otranto by Horace Walpole
$6.99
ISBN: 3434562

Vanity Fair by William Thackery
$11.99
ISBN: 3456839

 

book2.txt required output (user input will vary)

Welcome to the Best Reads App!

Enter the name of the file for your book collection: books.txt
Invalid file name!

Please enter the name of your file: books2.txt
What genre of literature is this: Latin-American

The number of books in the Latin-American book tree is: 9
The height of the Latin-American book tree is: 4

The first book in this collection is:
Cajas de carton by Francisco Jimenez
$10.99
ISBN: 2256839

The last book in this collection is:
Pedro Paramo by Juan Rulfo
$8.95
ISBN: 1123456

Enter the information about the book you wish to remove.
Title: Cien anos de soledad
Author: Gabriel Garcia Marquez
Cien anos de soledad has been removed!

The number of books in the Latin-American book tree is now: 8

The books contained in this tree are now:

Cajas de carton by Francisco Jimenez
$10.99
ISBN: 2256839

Como agua para chocolate by Laura Esquivel
$6.90
ISBN: 2375689

El alquimista by Paulo Coelho
$8.50
ISBN: 3212132

El beso de la mujer arana by Manuel Puig
$7.75
ISBN: 3456743

La casa de los espiritus by Isabel Allende
$7.50
ISBN: 2117889

La casa en Mango Street by Sandra Cisneros
$6.80
ISBN: 1134231

Los diarios de motocicleta by Ernesto Che Guevara
$10.99
ISBN: 2241174

Pedro Paramo by Juan Rulfo
$8.95
ISBN: 1123456

Enter the title of a book you wish to insert: Oficio de tinieblas
Enter the author of the book: Rosario Castellanos
Enter the price of the book: 9.95
Enter the isbn number of the book: 1234567

The number of books in the Latin-American book tree is now: 9

The books contained in this tree are now:

Cajas de carton by Francisco Jimenez
$10.99
ISBN: 2256839
 
Como agua para chocolate by Laura Esquivel
$6.90
ISBN: 2375689

El alquimista by Paulo Coelho
$8.50
ISBN: 3212132

El beso de la mujer arana by Manuel Puig
$7.75
ISBN: 3456743

La casa de los espiritus by Isabel Allende
$7.50
ISBN: 2117889

La casa en Mango Street by Sandra Cisneros
$6.80
ISBN: 1134231

Los diarios de motocicleta by Ernesto Che Guevara
$10.99
ISBN: 2241174

Oficio de tinieblas by Rosario Castellanos
$9.95
ISBN: 1234567

Pedro Paramo by Juan Rulfo
$8.95
ISBN: 1123456

Would you like to write this collection to a file? (yes/no): y
Enter the file name: output.txt
The data is now written to the file output.txt

Thank you! Goodbye!

Matching outfile.txt file which will contain the preorder print of the bst for book2.txt


Pedro Paramo by Juan Rulfo
$8.95
ISBN: 1123456

Como agua para chocolate by Laura Esquivel
$6.90
ISBN: 2375689

Cajas de carton by Francisco Jimenez
$10.99
ISBN: 2256839

La casa de los espiritus by Isabel Allende
$7.50
ISBN: 2117889

El alquimista by Paulo Coelho
$8.50
ISBN: 3212132

El beso de la mujer arana by Manuel Puig
$7.75
ISBN: 3456743

Los diarios de motocicleta by Ernesto Che Guevara
$10.99
ISBN: 2241174

La casa en Mango Street by Sandra Cisneros
$6.80
ISBN: 1134231

Oficio de tinieblas by Rosario Castellanos
$9.95
ISBN: 1234567

A Word About Printing


  • You must print the information about each book to the output.txt file in groups, separated by a blank line, identical to the sample output file shown above.

What To Submit

  • Upload BST.h, BST.cpp (file with I/O logic), as well as Book.cpp with the correctly overloaded operators to Catalyst. 
  • Please do not submit Book.h


Grading

  • Full credit based on a correct BST.cpp file with output identical to the examples shown above
  • Please make your output identical or you will receive a deduction in points for any inconsistencies.
  • 20 point deduction for each main component of the program that is not working.
  • Please do not submit a zip file or you will receive a 5 point deduction