-
Notifications
You must be signed in to change notification settings - Fork 0
/
studentTree.cpp
302 lines (301 loc) · 6.66 KB
/
studentTree.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;
//Dinh nghia kieu du lieu sinh vien
struct sinhVien{
int MSSV;
char hoTen[30];
int tuoi;
};
//Dinh nghia kieu cay
struct Node{
sinhVien data;
Node* left;
Node* right;
};
//Ten kieu du lieu la Tree
typedef Node* Tree;
//Ham nhap thong tin sinh vien va luu vao x
sinhVien nhapsinhVien(){
sinhVien x;
cout << "\nMSSV: ";
cin >> x.MSSV;
cout << "Ho va ten: ";
fflush(stdin);
gets(x.hoTen);
cout << "Tuoi: ";
cin >> x.tuoi;
return x;
}
//Tao cay rong
void Init(Tree &t){
t = NULL;
};
//Them sinh vien x vao cay t
int insertNode(Tree &t, sinhVien x){
if (t == NULL){
Node* p = new Node;
p->data = x;
p->left = p->right = NULL;
t = p;
return 1;
}
if (t != NULL){
if (t->data.MSSV < x.MSSV)
return insertNode(t->right, x);
if (t->data.MSSV > x.MSSV)
return insertNode(t->left, x);
else if(t->data.MSSV == x.MSSV);
return -1;
}
};
int classcounter = 0;//Bien dem so lop da tao
Tree Tr[1000];//Mang luu goc cua cac cay
//Ham tao lop moi
void createTree(){
char test;
classcounter++;
Init(Tr[classcounter]);
do{
sinhVien x;
x = nhapsinhVien();
int check = insertNode(Tr[classcounter], x);
if (check == -1)
cout << "\nSinh vien da ton tai";
else if (check == 0)
cout << "\nBo nho day";
else cout << "\nThem thanh cong";
cout << "\nBan co muon nhap tiep (y/n): ";
fflush(stdin);
cin >> test;
if (test != 'y') cout << "\nTao thanh cong lop " << classcounter;
}while(test == 'y');
}
//Tim kiem sinh vien MSSV la maso trong lop t
Node* findNode(Tree &t, int maso){
if (t == NULL){
return t ;
}
if (t->data.MSSV < maso){
return findNode(t->right, maso);
}
if (t->data.MSSV > maso){
return findNode(t->left, maso);
}
else if (t->data.MSSV == maso){
return t;
}
}
//In thong tin sinh vien
void printNode(Tree &t){
cout << "\nMa so sinh vien: " << t->data.MSSV <<" - " << "Ho va ten: " << t->data.hoTen <<" - " << "Tuoi: " << t->data.tuoi;
}
//Duyet cay theo kieu LNR
void LNR(Tree &t){
if (t != NULL){
LNR(t->left);
printNode(t);
LNR(t->right);
}
}
//Xoa sinh vien maso trong lop t
int deleteNode(Tree &t, int maso){
if (t == NULL) return 0;
else if (t->data.MSSV > maso) return deleteNode(t->left, maso);
else if (t->data.MSSV < maso) return deleteNode(t->right, maso);
else{
Node *p = t;
if (t->left == NULL) t = t->right;
else if (t->right == NULL) t = t->left;
else{
Node *s = t, *q = s->left;
while (q->right != NULL){
s = q;
q = q->right;
}
p->data = q->data;
deleteNode(t->left, q->data.MSSV);
}
}
return 1;
}
//Ghep cay t2 vao t1
void mergeTree(Tree &t1, Tree &t2){
if (t2 == NULL) return;
insertNode(t1, t2->data);
mergeTree(t1, t2->left);
mergeTree(t1, t2->right);
return;
}
//Ham thuc hien tim sinh vien
void do_findNode(){
Node* x =NULL;
int maso, find;
cout << "\nNhap lop can tim sinh vien: ";
cin >> find;
cout << "\nNhap MSSV can tim: ";
cin >> maso;
x = findNode(Tr[find], maso);
if (x != NULL){
cout << "\nThong tin sinh vien:";
cout << "\nMa so sinh vien: " << x->data.MSSV;
cout << "\nHo va ten: " << x->data.hoTen;
cout << "\nTuoi: " << x->data.tuoi;
}
else cout << "Khong tim thay sinh vien";
}
//Ham thuc hien them sinh vien
void do_insertNode(){
sinhVien x;
char test;
do{
int whichclass;
cout << "\nNhap lop can them sinh vien: ";
cin >> whichclass;
if (Tr[whichclass] == NULL){
cout <<"\nLop khong ton tai. Ban can tao lop truoc.";
break;
}
x = nhapsinhVien();
int check = insertNode(Tr[whichclass], x);
if (check == -1)
cout << "\nSinh vien da ton tai";
else if (check == 0)
cout << "\nBo nho day";
else cout << "\nThem thanh cong";
cout << "\nBan co muon nhap tiep (y/n): ";
fflush(stdin);
cin >> test;
}while(test == 'y');
}
//Ham thuc hien noi cay con vao cay to
void do_merge_childTree(){
while(1){
int class1, class2;
cout << "\nNhap lop lon: ";
cin >> class1;
cout << "\nNhap lop nho: ";
cin >> class2;
mergeTree(Tr[class1], Tr[class2]);
Tr[class2] = NULL;
cout << "\nDa them cay con thanh cong. Ban muon them cay con khac (y/n): ";
char test;
cin >> test;
if (test == 'n') break;
}
}
//Ham xoa sinh vien trong lop
void do_delete(){
int lop, maso;
cout << "\nNhap lop can xoa sinh vien: ";
cin >> lop;
if (Tr[lop] == NULL) {
cout <<"\nLop khong ton tai";
}
else{
char test;
do{
cout << "\nNhap MSSV can xoa: ";
cin >> maso;
int x = deleteNode(Tr[lop], maso);
if (x == 0) {
cout << "\nKhong co sinh vien nay";
break;
}
else if (x == 1) {
cout << "\nXoa thanh cong. Ban co muon xoa tiep (y/n) : ";
fflush(stdin);
cin >> test;
}
}while(test == 'y');
}
}
//Ham xoa cac phan tu o cay con
void deletechildTree(Tree &t, Node* x){
Node* p = x;
if (p->left != NULL) deletechildTree(t, p->left);
if (p->right != NULL) deletechildTree(t, p->right);
deleteNode(t, p->data.MSSV);
delete x;
}
//Ham xoa cay con
void do_delete_child_tree(){
int lop, maso;
Node* x = NULL;
cout << "\nNhap lop lon chua lop con can xoa: ";
cin >> lop;
if (Tr[lop] == NULL){
cout << "\nLop khong ton tai";
}
else{
cout << "\nNhap MSSV tai goc cay con can xoa: ";
cin >> maso;
x = findNode(Tr[lop], maso);
if (x == NULL) cout <<"\nKhong tim thay sinh vien";
else{
deletechildTree(Tr[lop], x);
cout << "\nXoa cay con thanh cong";
}
}
}
//Ham in danh sach lop
void do_print(){
int lop;
cout << "\nNhap lop can hien thi: ";
cin >> lop;
if (Tr[lop] != NULL){
cout << "\nDanh sach lop " << lop;
LNR(Tr[lop]);
}
else cout << "\nLop khong co sinh vien";
}
int main(){
do{
int lc;
cout << "\n\t\t\t ================MENU============================\n";
cout << "\t\t\t |1. Tao 1 lop hoc |\n";
cout << "\t\t\t |2. Tim kiem sinh vien |\n";
cout << "\t\t\t |3. Them sinh vien |\n";
cout << "\t\t\t |4. Them lop con |\n";
cout << "\t\t\t |5. Xoa sinh vien |\n";
cout << "\t\t\t |6. Xoa lop con |\n";
cout << "\t\t\t |7. In danh sach lop |\n";
cout << "\t\t\t |8. Thoat |\n";
cout << "\t\t\t ================================================\n";
cout << "\nBan chon: ";
cin >> lc;
switch(lc){
case 1:{
createTree();
break;
}
case 2:{
do_findNode();
break;
}
case 4:{
do_merge_childTree();
break;
}
case 3:{
do_insertNode();
break;
}
case 5:{
do_delete();
break;
}
case 6:{
do_delete_child_tree();
break;
}
case 7:{
do_print();
break;
}
case 8: return 0;
}
}while(1);
}