From 48a66f7881236dceee1271031c4b7110ba225435 Mon Sep 17 00:00:00 2001 From: Shivarkar Praneel Date: Sat, 25 Oct 2025 20:53:14 +0530 Subject: [PATCH] Implement tree structure for book chapters and sections The program constructs a tree structure representing a book's chapters, sections, and subsections, allowing for user input and display of the hierarchy. --- TREES_DSA_QUESTION_CPP | 116 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 TREES_DSA_QUESTION_CPP diff --git a/TREES_DSA_QUESTION_CPP b/TREES_DSA_QUESTION_CPP new file mode 100644 index 0000000..c7bbb04 --- /dev/null +++ b/TREES_DSA_QUESTION_CPP @@ -0,0 +1,116 @@ +/*Problem Statement: 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*/ + +// BEGINNING OF CODE +#include +#include + +using namespace std; + +struct node{ + string label; + int ch_count; + + struct node* child[10]; + +}*root; + +class GT{ + + public: + GT(){ + root = NULL; + } + + string lbel; + int count; + + void create(){ + root = new node; + + cout<<"Name of the book:\t"; + cin>>root->label; + cout<<"Number of chapters:\t"; + cin>>root->ch_count; + + for(int i=0;ich_count;i++){ + + root->child[i] = new node; + cout<<"Name of chapter "<< i+1 <<":\t"; + cin>>root->child[i]->label; + cout<<"Number of sections:\t"; + cin>>root->child[i]->ch_count; + + for(int j=0;jchild[i]->ch_count;j++){ + root->child[i]->child[j] = new node; + cout<<"Name of section "<< i+1 <<" - "<< j+1<< ":\t"; + cin>>root->child[i]->child[j]->label; + cout<<"Number of sub-sections:\t"; + cin>>root->child[i]->child[j]->ch_count; + + for(int k=0;kchild[i]->child[j]->ch_count;k++){ + root->child[i]->child[j]->child[k] = new node; + cout<<"Name of sub-section "<< i+1 <<" - "<< j+1<< " - "<< k+1<< ":\t"; + cin>>root->child[i]->child[j]->label; + + } + } + } + } + + void display(node * r){ + cout<label<ch_count<ch_count;i++){ + cout<child[i]->label<child[i]->ch_count<child[i]->ch_count;j++){ + cout<<"\t\t"<< i+1 <<" - "<< j+1<< " Name of sections: "; + cout<child[i]->child[j]->label<child[i]->child[j]->ch_count<child[i]->child[j]->ch_count;k++){ + cout<<"\t\t\t"<< i+1 <<" - "<< j+1<< " - "<< k+1<< " Name of sub-section: "; + cout<child[i]->child[j]->label< Add book info"< Display info"<Exit"<>ch; + + switch(ch){ + case 1: + g.create(); + break; + case 2: + g.display(root); + break; + case 3: + cout<