-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPRACTICAL3
86 lines (64 loc) · 2.52 KB
/
PRACTICAL3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 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>
#include <vector>
#include <string>
using namespace std;
class TreeNode {
public:
string data;
vector<TreeNode*> children;
TreeNode(string value) : data(value) {}
};
void printNodes(TreeNode* node, int depth = 0) {
if (!node)
return;
for (int i = 0; i < depth; ++i)
cout << " ";
cout << node->data << endl;
for (TreeNode* child : node->children)
printNodes(child, depth + 1);
}
int main() {
string bookTitle;
cout << "Enter the title of the book: ";
getline(cin, bookTitle);
TreeNode* book = new TreeNode(bookTitle);
int numChapters;
cout << "How many chapters does the book have? ";
cin >> numChapters;
cin.ignore(); // clear the newline character from input buffer
for (int i = 0; i < numChapters; ++i) {
string chapterTitle;
cout << "Enter the title of chapter " << (i + 1) << ": ";
getline(cin, chapterTitle);
TreeNode* chapter = new TreeNode(chapterTitle);
book->children.push_back(chapter);
int numSections;
cout << "How many sections does chapter " << (i + 1) << " have? ";
cin >> numSections;
cin.ignore(); // clear the newline character from input buffer
for (int j = 0; j < numSections; ++j) {
string sectionTitle;
cout << "Enter the title of section " << (j + 1) << " in chapter " << (i + 1) << ": ";
getline(cin, sectionTitle);
TreeNode* section = new TreeNode(sectionTitle);
chapter->children.push_back(section);
int numSubsections;
cout << "How many subsections does section " << (j + 1) << " in chapter " << (i + 1) << " have? ";
cin >> numSubsections;
cin.ignore(); // clear the newline character from input buffer
for (int k = 0; k < numSubsections; ++k) {
string subsectionTitle;
cout << "Enter the title of subsection " << (k + 1) << " in section " << (j + 1) << " of chapter " << (i + 1) << ": ";
getline(cin, subsectionTitle);
TreeNode* subsection = new TreeNode(subsectionTitle);
section->children.push_back(subsection);
}
}
}
cout << "\nPrinting all nodes of the book:\n";
printNodes(book);
return 0;
}