//Roll No.
: 12
//Assignment No. : 03
/* Problem Statement:A Book connsists of chapters, chapter consists
of sections and sections consists of subsections.Construct
a tree and print the nodes. Find the time and space requirrements
of your methods. */
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Node {
string name;
vector<Node*> children;
Node* parent;
Node(string n) : name(n), parent(nullptr) {}
};
class Book {
public:
string bookName;
Node* root;
Book() : root(nullptr) {}
void createBook() {
if (root) {
cout << "Book already created!" << endl;
return;
}
cout << "Enter the book name: ";
getline(cin >> ws, bookName);
root = new Node(bookName);
}
Node* addChapter() {
if (!root) {
cout << "Book not created yet." << endl;
return nullptr;
}
string chapterName;
cout << "Enter chapter name: ";
getline(cin >> ws, chapterName);
Node* newChapter = new Node(chapterName);
root->children.push_back(newChapter);
newChapter->parent = root;
return newChapter;
}
Node* addSection() {
if (!root || root->children.empty()) {
cout << "No chapters available. Add a chapter first." << endl;
return nullptr;
}
cout << "Select a chapter to add section: " << endl;
for (size_t i = 0; i < root->children.size(); i++) {
cout << i + 1 << ". " << root->children[i]->name << endl;
}
int choice;
cout << "Enter choice: ";
cin >> choice;
if (choice < 1 || choice > root->children.size()) {
cout << "Invalid choice." << endl;
return nullptr;
}
Node* parentChapter = root->children[choice - 1];
string sectionName;
cout << "Enter section name: ";
getline(cin >> ws, sectionName);
Node* newSection = new Node(sectionName);
parentChapter->children.push_back(newSection);
newSection->parent = parentChapter;
return newSection;
}
Node* addSubSection() {
if (!root) {
cout << "Book not created yet." << endl;
return nullptr;
}
vector<Node*> sections;
for (Node* chapter : root->children) {
for (Node* section : chapter->children) {
sections.push_back(section);
}
}
if (sections.empty()) {
cout << "No sections available. Add a section first." << endl;
return nullptr;
}
cout << "Select a section to add subsection: " << endl;
for (size_t i = 0; i < sections.size(); i++) {
cout << i + 1 << ". " << sections[i]->name << endl;
}
int choice;
cout << "Enter choice: ";
cin >> choice;
if (choice < 1 || choice > sections.size()) {
cout << "Invalid choice." << endl;
return nullptr;
}
Node* parentSection = sections[choice - 1];
string subSectionName;
cout << "Enter subsection name: ";
getline(cin >> ws, subSectionName);
Node* newSubSection = new Node(subSectionName);
parentSection->children.push_back(newSubSection);
newSubSection->parent = parentSection;
return newSubSection;
}
void displayStructure(Node* node, int level) {
if (!node) return;
for (int i = 0; i < level; i++) {
cout << " ";
}
cout << node->name << endl;
for (Node* child : node->children) {
displayStructure(child, level + 1);
}
}
};
int main() {
Book book;
int choice;
do {
cout << "\n***********************************" << endl;
cout << "\tBook Management Menu" << endl;
cout << "1. Create Book" << endl;
cout << "2. Add Chapter" << endl;
cout << "3. Add Section to Chapter" << endl;
cout << "4. Add Sub-section to Section" << endl;
cout << "5. Display Book Structure" << endl;
cout << "6. Exit" << endl;
cout << "***********************************" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
book.createBook();
break;
case 2:
book.addChapter();
break;
case 3:
book.addSection();
break;
case 4:
book.addSubSection();
break;
case 5:
cout << "\nBook Structure:" << endl;
book.displayStructure(book.root, 0);
break;
case 6:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 6);
return 0;
}
/*
OUTPUT:
PS C:\Users\Admin\OneDrive\Desktop\c++\DSA> g++ P3.cpp -o 1
PS C:\Users\Admin\OneDrive\Desktop\c++\DSA> ./1
***********************************
Book Management Menu
1. Create Book
2. Add Chapter
3. Add Section to Chapter
4. Add Sub-section to Section
5. Display Book Structure
6. Exit
***********************************
Enter your choice: 1
Enter the book name: BOOK
***********************************
Book Management Menu
1. Create Book
2. Add Chapter
3. Add Section to Chapter
4. Add Sub-section to Section
5. Display Book Structure
6. Exit
***********************************
Enter your choice: 2
Enter chapter name: abc
***********************************
Book Management Menu
1. Create Book
2. Add Chapter
3. Add Section to Chapter
4. Add Sub-section to Section
5. Display Book Structure
6. Exit
***********************************
Enter your choice: 2
Enter chapter name: bcd
***********************************
Book Management Menu
1. Create Book
2. Add Chapter
3. Add Section to Chapter
4. Add Sub-section to Section
5. Display Book Structure
6. Exit
***********************************
Enter your choice: 2
Enter chapter name: cde
***********************************
Book Management Menu
1. Create Book
2. Add Chapter
3. Add Section to Chapter
4. Add Sub-section to Section
5. Display Book Structure
6. Exit
***********************************
Enter your choice: 2
Enter chapter name: def
***********************************
Book Management Menu
1. Create Book
2. Add Chapter
3. Add Section to Chapter
4. Add Sub-section to Section
5. Display Book Structure
6. Exit
***********************************
Enter your choice: 3
Select a chapter to add section:
1. abc
2. bcd
3. cde
4. def
Enter choice: 2
Enter section name: b
***********************************
Book Management Menu
1. Create Book
2. Add Chapter
3. Add Section to Chapter
4. Add Sub-section to Section
5. Display Book Structure
6. Exit
***********************************
Enter your choice: 3
Select a chapter to add section:
1. abc
2. bcd
3. cde
4. def
Enter choice: 2
Enter section name: c
***********************************
Book Management Menu
1. Create Book
2. Add Chapter
3. Add Section to Chapter
4. Add Sub-section to Section
5. Display Book Structure
6. Exit
***********************************
Enter your choice: 4
Select a section to add subsection:
1. b
2. c
Enter choice: 1
Enter subsection name: xyz
***********************************
Book Management Menu
1. Create Book
2. Add Chapter
3. Add Section to Chapter
4. Add Sub-section to Section
5. Display Book Structure
6. Exit
***********************************
Enter your choice: 5
Book Structure:
BOOK
abc
bcd
b
xyz
c
cde
def
***********************************
Book Management Menu
1. Create Book
2. Add Chapter
3. Add Section to Chapter
4. Add Sub-section to Section
5. Display Book Structure
6. Exit
***********************************
Enter your choice: 6
Exiting program.
*/