Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- using namespace std;
- class Node {
- public:
- string label;
- int count;
- Node *child[10];
- };
- Node *root;
- class Tree {
- private:
- int chapterCount, sectionCount;
- public:
- Tree() {
- root = nullptr;
- }
- void createTree();
- void displayTree();
- };
- void Tree::createTree() {
- root = new Node;
- string bookName;
- cout << "\nEnter name of the Book: ";
- cin.ignore();
- getline(cin, bookName);
- root->label = bookName;
- cout << "Enter the number of chapters: "; cin >> chapterCount;
- root->count = chapterCount;
- for (int i = 0; i < root->count; i++) {
- root->child[i] = new Node;
- string chapterName;
- cout << "\nEnter name of the Chapter " << i + 1 << ": ";
- if (i == 0) cin.ignore();
- getline(cin, chapterName);
- root->child[i]->label = chapterName;
- cout << "Enter the number of sections: "; cin >> sectionCount;
- root->child[i]->count = sectionCount;
- for (int j = 0; j < root->child[i]->count; j++) {
- root->child[i]->child[j] = new Node;
- string sectionName;
- cout << "Enter name of the Section " << j + 1 << ": ";
- if (j == 0) cin.ignore();
- getline(cin, sectionName);
- root->child[i]->child[j]->label = sectionName;
- }
- }
- }
- void Tree::displayTree() {
- Node *temp = root;
- cout << endl;
- cout << "Displaying details for the book: " << root->label << endl;
- for (int i = 0; i < root->count; i++) {
- cout << "Chapter " << i + 1 << ": " << root->child[i]->label << endl;
- for (int j = 0; j < root->child[i]->count; j++) {
- cout << setw(4) << "" << "Section " << j + 1 << ": " << root->child[i]->child[j]->label << endl;
- }
- }
- cout << endl;
- }
- int main() {
- Tree t;
- while (true) {
- char ch;
- cout << "----- MENU -----" << endl;
- cout << "1. Create" << endl;
- cout << "2. Display" << endl;
- cout << "3. Exit" << endl;
- cout << "Enter your choice: "; cin >> ch;
- if (ch == '1') {
- t.createTree();
- t.displayTree();
- } else if (ch == '2') {
- t.displayTree();
- } else if (ch == '3') {
- cout << "\nTerminating program" << endl;
- break;
- } else {
- cout << "Invalid Input" << endl;
- }
- }
- return 0;
- };
- /*
- OUPTUT
- ----- MENU -----
- 1. Create
- 2. Display
- 3. Exit
- Enter your choice: 1
- Enter name of the Book: Fundamentals of Data Stuctures
- Enter the number of chapters: 2
- Enter name of the Chapter 1: Hashing
- Enter the number of sections: 3
- Enter name of the Section 1: Linear Probing
- Enter name of the Section 2: Quadratic Probing
- Enter name of the Section 3: Chaining
- Enter name of the Chapter 2: Queues
- Enter the number of sections: 4
- Enter name of the Section 1: Queue
- Enter name of the Section 2: Circular Queue
- Enter name of the Section 3: Double Ended Queue
- Enter name of the Section 4: Priority Queue
- Displaying details for the book: Fundamentals of Data Stuctures
- Chapter 1: Hashing
- Section 1: Linear Probing
- Section 2: Quadratic Probing
- Section 3: Chaining
- Chapter 2: Queues
- Section 1: Queue
- Section 2: Circular Queue
- Section 3: Double Ended Queue
- Section 4: Priority Queue
- ----- MENU -----
- 1. Create
- 2. Display
- 3. Exit
- Enter your choice: 3
- Terminating program
- */
Advertisement
Add Comment
Please, Sign In to add comment