-
Notifications
You must be signed in to change notification settings - Fork 0
/
GROUP-B-5.cpp
205 lines (185 loc) · 3.99 KB
/
GROUP-B-5.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
SAYALI RAHANE
CLASS- SE A
BATCH- C50
GROUP-B-5. 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.
*/
//header files
#include <iostream>
using namespace std;
struct Node //NOde Structure
{
string name;
struct Node *ch1;
struct Node *ch2;
struct Node *ch3;
}*Root,*Ch;
void createTree() //function to create General Tree
{
struct Node *temp;
temp = new struct Node; //memory allocation
cout<<"\n\n Enter the Book Name : "; //Insert data, pointers initialise
cin>>temp->name;
temp->ch1 = NULL;
temp->ch2 = NULL;
temp->ch3 = NULL;
if(Root == NULL) //attach New Node in tree
{
Root = temp;
}
}
void displayTree() //display function
{
if(Root)
{
cout<<"\n\n Book Name : "<<Root->name;
}
Ch = Root->ch1;
if(Ch)
{
cout<<"\n\n Chapter 01 : "<<Ch->name;
if(Ch->ch1)
{
cout<<"\n\n Section 01 : "<<Ch->ch1->name;
}
if(Ch->ch2)
{
cout<<"\n\n Section 02 : "<<Ch->ch2->name;
}
if(Ch->ch3)
{
cout<<"\n\n Section 03 : "<<Ch->ch3->name;
}
}
Ch = Root->ch2;
if(Ch)
{
cout<<"\n\n Chapter 02 : "<<Ch->name;
if(Ch->ch1)
{
cout<<"\n\n Section 01 : "<<Ch->ch1->name;
}
if(Ch->ch2)
{
cout<<"\n\n Section 02 : "<<Ch->ch2->name;
}
if(Ch->ch3)
{
cout<<"\n\n Section 03 : "<<Ch->ch3->name;
}
}
Ch = Root->ch3;
if(Ch)
{
cout<<"\n\n Chapter 03 : "<<Ch->name;
if(Ch->ch1)
{
cout<<"\n\n Section 01 : "<<Ch->ch1->name;
}
if(Ch->ch2)
{
cout<<"\n\n Section 02 : "<<Ch->ch2->name;
}
if(Ch->ch3)
{
cout<<"\n\n Section 03 : "<<Ch->ch3->name;
}
}
}
void insertChapter() //Function to insert chapters in Book
{
struct Node *temp;
temp = new struct Node;
cout<<"\n\n Enter Chapter Name : ";
cin>>temp->name;
temp->ch1 = NULL;
temp->ch2 = NULL;
temp->ch3 = NULL;
if(Root->ch1 == NULL) //Attach New Node in TREE
{
Root->ch1 = temp;
}
else if(Root->ch2 == NULL)
{
Root->ch2 = temp;
}
else if(Root->ch3 == NULL)
{
Root->ch3 = temp;
}
else
{
cout<<"\n\n No More chapter can be added";
}
}
void insertSection() //function to Insert SEctions in Chapters of Book
{
struct Node *temp;
temp = new struct Node; //Allocate memory
cout<<"\n\n Enter Section Name : "; //Insert Data and initializes pointers
cin>>temp->name;
temp->ch1 = NULL;
temp->ch2 = NULL;
temp->ch3 = NULL;
if(Ch->ch1 == NULL) //attach new node in tree
{
Ch->ch1 = temp;
}
else if(Ch->ch2 == NULL)
{
Ch->ch2 = temp;
}
else if(Ch->ch3 == NULL)
{
Ch->ch3 = temp;
}
else
{
cout<<"\n\n No More section can be added ";
}
}
void insertSectionMenu()
{
int ch;
cout<<"\n\n Enter the section of the chapters (1/2/3) : ";
cin>>ch;
switch (ch)
{
case 1:
Ch = Root->ch1;
insertSection();
insertSection();
insertSection();
break;
case 2:
Ch = Root->ch2;
insertSection();
insertSection();
insertSection();
break;
case 3:
Ch = Root->ch3;
insertSection();
insertSection();
insertSection();
break;
default:
break;
}
}
//main function
int main()
{
cout<<"\n---------A C++ Program to implement General Tree----------";
Root = NULL;
createTree();
insertChapter();
insertChapter();
insertChapter();
insertSectionMenu();
insertSectionMenu();
insertSectionMenu();
displayTree();
return 0;
}