Group B Assignment 3 : Binary Tree Program (Book )

 A book consists of chapters, chapters consist of sections and sections consist of subsections. Construct a tree and print the nodes. Find the time and space requirements of your method

#include<iostream>
using namespace std;
struct node
{
        string data;
        node* child[10];
        int count;
};
class csbook
{
public:
        node* root;
        csbook()
        {
                root=NULL;
        }
    void create();
    void display();
};
void csbook::create()
{
        int i,j,k;
        root=new node;
 cout<<"Enter the book name \n";
 cin>>root->data;
 cout<<"How many chapters \n";
 cin>>root->count;

 for(i=1;i<=root->count;i++)
 {
         cout<<"Enter the chapter name \n";
         root->child[i]=new node;
     cin>>root->child[i]->data;
     cout<<"How many sections \n";
     cin>>root->child[i]->count;


     for(j=1;j<=root->child[i]->count;j++)
     {
         cout<<"Enter the sections name \n";
         root->child[i]->child[j]=new node;
         cin>>root->child[i]->child[j]->data;
     }
}
}
void csbook::display()
{
        int i,j,k;

 cout<<"the book name : "<<root->data<<"\n";
 for(i=1;i<=root->count;i++)
 {
   cout<<"the chapter "<<i<<" : "<<root->child[i]->data<<"\n";

     for(j=1;j<=root->child[i]->count;j++)
     {
         cout<<"the sections "<<i<<" : "<<root->child[i]->child[j]->data<<"\n";
     }
}
}
int main()
{
        csbook p;
        char ans;
    int ch;
    do
    {
   cout<<"-----------MAIN MENU------------ \n";
   cout<<"1.for create \n";
   cout<<"2.for display \n";
   cin>>ch;
   switch(ch)
   {
   case 1 :
           p.create();
           break;
   case 2 :
           p.display();
           break;
   }
   cout<<"Do u want to continue";
   cin>>ans;
    }
    while(ans=='y'||ans=='Y');
    return 0;
}

/*
 * -------------output--------------

 * -----------MAIN MENU------------
1.for create
2.for display
1
Enter the book name
dsa
How many chapters
3
Enter the chapter name
array
How many sections
2
Enter the sections name
2d
Enter the sections name
3d
Enter the chapter name
linkedlist
How many sections
3
Enter the sections name
sll
Enter the sections name
dll
Enter the sections name
cll
Enter the chapter name
queue
How many sections
3
Enter the sections name
linear
Enter the sections name
circular
Enter the sections name
priority
Do u want to continue y
-----------MAIN MENU------------
1.for create
2.for display
2
the book name : dsa
the chapter 1 : array
the sections 1 : 2d
the sections 1 : 3d
the chapter 2 : linkedlist
the sections 2 : sll
the sections 2 : dll
the sections 2 : cll
the chapter 3 : queue
the sections 3 : linear
the sections 3 : circular
the sections 3 : priority
Do u want to continue n
 */


Comments