PracticalNo.
//Name:SATYAM.
//Roll no:52. DIV:B
//EXPERIMENT-3
#incude<iostream>
#include <string>
using namespacestd;
structnode//NodeDeclaration
{
string label;
intch_count;
structnode*child[10];
}*root;
classGT//ClassDeclaration
{
public:
void create_tree();
voiddisplay(node*r1);
GT()
{
root=NULL;
}
};
voidGT::create_tree()
{
inttbooks,tchapters,i,j,k;
root=newnode;
cout<<"Enternameofbook:"; cin.ignore();
getline(cin,root->label);
cout<<"Enternumberofchaptersinbook:"; cin
>> tchapters;
root->ch_count=tchapters;
for(i=0;i<tchapters;i++)
{
root->child[i]=newnode;
cout<<"EnterthenameofChapter"<<i+1<<":"; cin.ignore();
getline(cin,root->child[i]->label);
cout<<"EnternumberofsectionsinChapter"<<i+1<<":"; cin >>
root->child[i]->ch_count;
for(j=0;j<root->child[i]->ch_count;j++)
{
root->child[i]->child[j]=newnode;
cout<<"EnterNameofSection"<<j+1<<":"; cin.ignore();
getline(cin,root->child[i]->child[j]->label);
}
}
}
voidGT::display(node*r1)
{
inti,j;
if(r1!=NULL)
{
cout << "\n----Book Hierarchy---";
cout<<"\nBooktitle:"<<r1->label; int
tchapters = r1->ch_count;
for(i=0;i<tchapters;i++)
{
cout<<"\nChapter"<<i+1<<":"<<r1->child[i]->label; cout <<
"\nSections: ";
for(j=0;j<r1->child[i]->ch_count;j++)
{
cout<<"\n"<<r1->child[i]->child[j]->label;
}
}
}
cout<<endl;
}
intmain()
{
intchoice;
GT gt;
while(1)
{
cout<<"\nBookTreeCreation"<<endl; cout <<
"1. Create" << endl;
cout << "2. Display" << endl;
cout << "3. Quit" << endl;
cout<<"Enteryourchoice:"; cin
>> choice;
switch(choice)
{
case1:
gt.create_tree();
break;
case2:
gt.display(root);
break;
case3:
cout<<"Thanksforusingthisprogram!!!"<<endl; exit(0);
default:
cout<<"Wrongchoice!!!"<<endl;
}
}
return0;
}
//output:-
BookTreeCreation
1. Create
2. Display
3. Quit
Enteryourchoice:1
Enternameofbook:Maths
Enternumberofchaptersinbook:2
EnterthenameofChapter1:chapter1
EnternumberofsectionsinChapter1:1
EnterNameofSection1:ch1_section1
EnterthenameofChapter2:chapter2
EnternumberofsectionsinChapter2:1
EnterNameofSection1:ch2_section2
BookTreeCreation
1. Create
2. Display
3. Quit
Enteryourchoice:2
----BookHierarchy---
Booktitle:Maths
Chapter1:chapter1
Sections:
ch1_section1
Chapter2:chapter2
Sections:
ch2_section2
BookTreeCreation
1. Create
2. Display
3. Quit
Enteryourchoice:3
Thanksforusingthisprogram!!!