Repository navigation
Expand file tree
/
Copy pathA2book.cpp
More file actions
160 lines (131 loc) · 2.95 KB
/
Copy pathA2book.cpp
File metadata and controls
160 lines (131 loc) · 2.95 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.h>
using namespace std;
//Node Declaration
struct node
{
char label[10]; // TO STORE CHAPTER HEAD,SECTION HEAD
int ch_count; // To store no of child ele
struct node *child[10]; //Array of pointers to store addresses of child nodes
}*root; // To store the address of root
// Class Declaration
class BST
{
public:
void create_tree();
void display(node * r1);
BST()
{
root = NULL;
}
};
void BST::create_tree()
{
int tbooks,tchapters,i,j,k;
root = new node(); // root node is created
cout<<"Enter name of book";
cin>>root->label;
cout<<"Enter no. of chapters in book";
cin>>tchapters;
root->ch_count = tchapters;
for(i=0;i<tchapters;i++) //chapters
{
root->child[i] = new node; //chapter is created
cout<<"Enter Chapter name\n";
cin>>root->child[i]->label;
cout<<"Enter no.of sections in this Chapter:";
cin>>root->child[i]->ch_count;
for(j=0; j<root->child[i]->ch_count; j++)//sections
{
root->child[i]->child[j]=new node;
cout<<"Enter section head";
cin>>root->child[i]->child[j]->label;
}
}
}
void BST::display(node * r1)
{
int i,j,k,tchapters;
if(r1 != NULL)
{
cout<<"\n-----Book Hierarchy---";
cout<<"\n Book title : "<<r1->label;
tchapters = r1->ch_count; // how many chapters are there
for(i=0;i<tchapters;i++)
{
cout<<"\n Chapter "<<i+1;
cout<<" "<<r1->child[i]->label;
cout<<"\n Sections";
for(j=0;j<r1->child[i]->ch_count;j++)
{
//cin>>r1->child[i]->child[j]->label;
cout<<"\n "<<r1->child[i]->child[j]->label;
}
}
}
}
/*
* Main Contains Menu
*/
int main()
{
int choice;
BST bst;
while (1)
{
cout<<"-----------------"<<endl;
cout<<"Book Tree Creation"<<endl;
cout<<"-----------------"<<endl;
cout<<"1.Create"<<endl;
cout<<"2.Display"<<endl;
cout<<"3.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
bst.create_tree();
case 2:
bst.display(root);
break;
case 3:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
}
/*
OUTPUT :
-----------------
Book Tree Creation
-----------------
1.Create
2.Display
3.Quit
Enter your choice : 1
Enter name of book DS
Enter no. of chapters in book 2
Enter Chapter name
A
Enter no.of sections in this Chapter:1
Enter section head X
Enter Chapter name
B
Enter no.of sections in this Chapter:1
Enter section head Y
-----Book Hierarchy---
Book title : DS
Chapter 1 A
Sections
X
Chapter 2 B
Sections
Y-----------------
Book Tree Creation
-----------------
1.Create
2.Display
3.Quit
Enter your choice : 3
*/