-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathB5.cpp
More file actions
160 lines (124 loc) · 3.86 KB
/
B5.cpp
File metadata and controls
160 lines (124 loc) · 3.86 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <string>
using namespace std;
struct node {
string label;
int count;
node *child[50];
} *root;
class Tree {
public:
void insert();
void display();
Tree() {
root == NULL;
}
};
void Tree::insert() {
root = new node();
cout <<"\nEnter the name of book: ";
getline(cin >> ws, root->label);
cout <<"Enter the total number of chapters in book: ";
cin >> root->count;
for (int i = 0; i < root->count; i++) {
root->child[i] = new node();
cout <<"\nEnter the name of chapters " << i + 1 << ": ";
getline(cin >> ws, root->child[i]->label);
cout <<"Enter the number of sections: ";
cin >> root->child[i]->count;
for (int j = 0; j < root->child[i]->count; j++) {
root->child[i]->child[j] = new node();
cout <<"\nEnter the name of section " << i + 1 << '.' << j + 1 << ": ";
getline(cin >> ws, root->child[i]->child[j]->label);
cout <<"Enter the number of sub sections: ";
cin >> root->child[i]->child[j]->count;
cout << '\n';
for (int k = 0; k < root->child[i]->child[j]->count; k++) {
root->child[i]->child[j]->child[k] = new node();
cout <<"Enter the name of sub section " << i + 1 << '.' << j + 1 << '.' << k + 1 << ": ";
getline(cin >> ws, root->child[i]->child[j]->child[k]->label);
}
}
}
}
void Tree::display() {
if (root != NULL) {
cout <<"\n\n--------------- Hierarchy of Book ---------------\n\n";
cout <<"Book Name: "<< root->label << endl;
for (int i = 0; i < root->count; i++) {
cout <<"\n> "<< root->child[i]->label << endl;
for (int j = 0; j < root->child[i]->count; j++) {
cout <<"---> "<< root->child[i]->child[j]->label << endl;
for (int k = 0; k < root->child[i]->child[j]->count; k++) {
cout <<"------> "<< root->child[i]->child[j]->child[k]->label << endl;
}
}
}
}
}
int main() {
Tree g;
int ch;
do {
cout <<"\nEnter choice\n1.input 2.display\nChoice [1/2] ";
cin >> ch;
switch (ch) {
case 1:
g.insert();
break;
case 2:
g.display();
break;
}
} while (ch < 3);
return 0;
}
/*
Enter choice
1.input 2.display
Choice [1/2] 1
Enter the name of book: Data Structure & Algorithm
Enter the total number of chapters in book: 2
Enter the name of chapters 1: Hashing
Enter the number of sections: 2
Enter the name of section 1.1: Hash Function
Enter the number of sub sections: 2
Enter the name of sub section 1.1.1: Division Method
Enter the name of sub section 1.1.2: Mid Square
Enter the name of section 1.2: Collision Resolution
Enter the number of sub sections: 2
Enter the name of sub section 1.2.1: Probing
Enter the name of sub section 1.2.2: Chaining
Enter the name of chapters 2: Trees
Enter the number of sections: 2
Enter the name of section 2.1: Properties of Trees
Enter the number of sub sections: 2
Enter the name of sub section 2.1.1: Height
Enter the name of sub section 2.1.2: Depth
Enter the name of section 2.2: Traversals
Enter the number of sub sections: 2
Enter the name of sub section 2.2.1: Depth First Search
Enter the name of sub section 2.2.2: Breadth First Search
Enter choice
1.input 2.display
Choice [1/2] 2
--------------- Hierarchy of Book ---------------
Book Name: Data Structure & Algorithm
> Hashing
---> Hash Function
------> Division Method
------> Mid Square
---> Collision Resolution
------> Probing
------> Chaining
> Trees
---> Properties of Trees
------> Height
------> Depth
---> Traversals
------> Depth First Search
------> Breadth First Search
Enter choice
1.input 2.display
Choice [1/2] 3
*/