Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are going to build a binary search tree to organize book information. The bo

ID: 3686643 • Letter: Y

Question

You are going to build a binary search tree to organize book information. The book information consists of a title, author, and year of publication. The tree will be initialized by reading in the information from a file. The information is line oriented and will consist of the name of the book, the author (last name first), and the year of publication. Once your tree is built you are to do an inorder traversal and print out all of the information. To test the tree you are to do a series of searches by book title. The search strings will also be line oriented and come from a file. You are to keep report on whether each title is found and how many comparisons were required to find it. If a node is not found you should still be reporting on the number of comparisons required to come to this decision.

The input file will be named books.txt and will contain the information as described. A sample file will be posted with this project. The search file will be named searches.txt and a sample will also be posted with this file.

Work in small parts. Build the tree first and see that searches work. Start small. A single node should be easy to test.

C++ plz

Explanation / Answer

#include <bits/stdc++.h>

struct Book{
   public:
       string title;
       string author;
       int year;
       Book(string title,string author,int year){
           this->title = title;
           this->author = author;
           this->year = year;
       }
};

struct Node{
   public:
       Book* book;
       Node* left;
       Node* right;
       Node(string title,string author,int year){
           book = new Book(title,author,year);
           left = NULL;
           right = NULL;
       }
};

struct Tree{
   Node *root;
   Tree(string title,string author,int year){
       root = new Node(title,author,year);
   }
   bool search(Node* root,string title,int &compare){
       if (root == NULL) return false;
       if (root->title == title) {
           compare += 1;
           return true;
       }
       if (root->title > title){
           compare += 1;
           return search(root->left,title,compare);
       }
       else{
           compare += 1;
           return search(root->right,title,compare);
       }
   }
   Node* insert(struct Node* node,string title,string author,int year){
       if (node == NULL){
           Node* temp = new Node(title,author,year);
           return temp;
       }
       if (node->title > title)
           node->left = insert(node->left,title,author,year);
       else
           node->right = insert(node->right,title,author,year);
       return node;
   }
   void inorder(struct Node* node){
       if (node != NULL){
           inorder(node->left);
           cout << node->title << " " << node->author << " " << node->year << endl;
           inorder(node->right);
       }
   }
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote