In C++, Write a program that stores book data (title, author name, ISB#, publisher (name), year of publication) using tree data structure.

SUMMARY

The program is written in C++. A tree data structure is implemented which has each node as a book detail. Each of these nodes has the basic details about a book. All the details about all the books are printed as output to the console.

 

EXPLANATION

A class called ‘node’ is defined. This class consists of the title of a book, the author name, the publisher name, year, and the ISB number of the book as its data members. Basically, the node class stores book data. Then a parameterized constructor is defined and is used to initialize the data of the book. A function called display() is defined and it is used to display all the details about a book. Lastly in the main method, three objects are declared and the arguments are passed to the constructor. The data is displayed using the display method. 

 

CODE

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
/* to have the node in the tree data structure */
/* to implement the given book store scenario */
class node   //This class stores book data
{
    public:
    /* getting basic details about a book in the book store */
    string title, authorName, publisher;
    int isbn, year;
    /* pointing to left and right nodes */
    node* lt;
    node* rt;
    /* parametrized constructor */
    node(string t,string an,string p,int i,int y)
    {
        title=t;
        authorName=an;
        publisher=p;
        isbn=i;
        year=y;
        lt=NULL;
        rt=NULL;
    }
    /* to display the details about a book */
    void display()
    {
        cout<<left;
        cout<<setw(20)<<title<<setw(20)<<authorName<<setw(20);
        cout<<publisher<<setw(8)<<isbn<<setw(8)<<year<<endl;
    }
};
 
int main()
{
    /* defining first node in the tree */
    node *nd1=new node("Wings of Fire","Abdul Kalam","Pearson",3240,1998);
    node *nd2=new node("Bahubali","Rajamouli","Dil Raj",3241,2015);
    node *nd3=new node("Love Story","Sekhar Kammala","Suresh Babu",3242,2021);
    /* to have a node with left and right branches */
    nd1->lt=nd2;
    nd1->rt=nd3;
    cout<<left;
    cout<<setw(20)<<"Title"<<setw(20)<<"Author Name"<<setw(20);
    cout<<"Publisher"<<setw(8)<<"ISBN"<<setw(8)<<"Year"<<endl;
    nd1->display();
    nd1->lt->display();
    nd1->rt->display();
}

 

OUTPUT

 

Also, read add the function max as an abstract function to the class arrayListType to return the largest element of the list.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *