-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTREE.CPP
376 lines (336 loc) · 6.23 KB
/
TREE.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include<iostream>
using namespace std;
class node
{
int info;
node *next;
node* previous;
public:
node(int m = 0 )
{
info = m;
next = NULL;
previous = NULL;
}
void setdata(int m)
{
info = m;
}
node* getnext()
{
return next;
}
void setnext(node* p)
{
next = p;
}
void setprevious(node* p)
{
previous = p;
}
node* getprevious()
{
return previous;
}
int getdata()
{
return info;
}
};
class tree
{
node* start;
public:
tree()
{
start = NULL;
}
void add_node(int s)
{
node *temp = new node(s);
if(start == NULL){start = temp;return;}
node* search=start;
node* papa;
while(search != NULL)
{
papa = search;
if(temp->getdata() < search->getdata())
{
search = search->getprevious();
continue;
}
search = search->getnext();
}
if(temp->getdata() < papa->getdata())
{
papa->setprevious(temp);
return;
}
papa->setnext(temp);
return;
}
void deletion(int m )
{
if(start==NULL)
{
cout<<"OOPS! THERE IS NO ITEM IN THIS LIST."<<endl;
return;
}
node* search=start;
node* last = start;
while(search!=NULL)
{
if(search->getdata()==m)
{
if(search->getnext() != NULL && search->getprevious() != NULL)
{
node* upper = last;
node* traverse = search->getnext();
while(traverse->getprevious() != NULL)
{
upper = traverse;
traverse = traverse->getprevious();
}
if(upper != last)
{
upper->setprevious(traverse->getnext());
}
if(search == start){}
else if(m < last->getdata()){last->setprevious(traverse);}
else{last->setnext(traverse);}
traverse->setprevious(search->getprevious());
if(upper != last)
{
traverse->setnext(search->getnext());
}
if(search == start){start=traverse;}
cout<<"\t"<<m<<" DELETED."<<endl;
delete search;
return;
}
if(search == start)
{
if(search->getnext() != NULL)
{
node* upper = start;
node* traverse = start->getnext();
while(traverse->getprevious() != NULL)
{
upper = traverse;
traverse = traverse->getprevious();
}
if(traverse != start->getnext())
{
upper->setprevious(traverse->getnext());
traverse->setnext(start->getnext());
}
start = traverse;
}
else if(start->getprevious() != NULL)
{
start = start->getprevious();
}
else
{
start =NULL;
}
delete search;
return;
}
if(m < last->getdata())
{
if(search->getnext()==NULL)
{last->setprevious(search->getprevious());}
else
{last->setprevious(search->getnext());}
}
else
{
if(search->getnext()==NULL)
{last->setnext(search->getprevious());}
else
{last->setnext(search->getnext());}
}
cout<<"\t"<<m<<" DELETED."<<endl;
delete search;
return;
}
last = search;
if(m < search->getdata()){search = search->getprevious();}
else
search = search->getnext();
}
cout<<"NODE NOT FOUND"<<endl;
}
void rootfirst(node * dis )
{
if(dis!= NULL)
{
cout<<dis->getdata()<<endl;
rootfirst(dis->getprevious());
rootfirst(dis->getnext());
}
}
void rootin(node * dis)
{
if(dis!= NULL)
{
rootin(dis->getprevious());
cout<<dis->getdata()<<endl;
rootin(dis->getnext());
}
}
void rootlast(node * dis)
{
if(dis!= NULL)
{
rootlast(dis->getprevious());
rootlast(dis->getnext());
cout<<dis->getdata()<<endl;
}
}
void truncate(node * dis)
{
if(dis!= NULL)
{
rootlast(dis->getprevious());
rootlast(dis->getnext());
delete dis;
}
start = NULL;
cout<<"DATABASE TRUNCATED."<<endl;
}
void display(int option)
{
if(start==NULL){cout<<"LIST EMPTY"<<endl;return;}
node* dis=start;
switch(option)
{
case 3:
rootfirst(start);
break;
case 4:
rootin(start);
break;
case 5:
rootlast(start);
break;
case 9:
truncate(start);
break;
default:
cout<<"BAD CHOICE___ GO HOME & SLEEP... FURRRRRRRRRRRRRRRRRRR"<<endl;
}
}
void search(int value)
{
node* khoj=start;
while(khoj!=NULL)
{
if(value==khoj->getdata())
{
cout<<"FOUND ";
int count = 0;
do
{
if(khoj->getdata()==value)
{
khoj = khoj->getnext();
count++;
}
else
khoj = khoj->getprevious();
}while(khoj != NULL);
cout<<count<<" TIMES"<<endl;
return;
}
if(value < khoj->getdata())
{khoj = khoj->getprevious();}
else
khoj = khoj->getnext();
}
cout<<"THIS DATA IS NOT IN LIST."<<endl;
}
int getroot()
{
if(start == NULL){ cout<<"LIST EMPTY.";return 0;}
return start->getdata();
}
};
int main()
{
int chak;
tree list;
tree list7;
{
list.add_node(35);
list.add_node(45);
list.add_node(15);
list.add_node(3);
list.add_node(29);
list.add_node(15);
list.add_node(1);
list.add_node(90);
}
do
{
cout<<endl;
cout<<"1.INSERT A DATA.\n2.INSERT MULTIPLE DATA ENTRY.\n3.TRAVERSE ROOT FIRST (PREORDER).\n4.BETWEEN CHILD (INORDER).\n5.ROOT LAST (POSTORDER).\n6.SEARCH DATA.\n7.ROOT VALUE\n8.DELETE A SPECIFIC DATA\n9.TRUNCATE ENTIRE LIST.\n10.EXIT.\n";
cout<<endl;
cout<<"ENTER YOUR CHOICE FROM THE ABOVE."<<endl;
int data;
cin>>chak;
cout<<endl;
switch(chak)
{
case 1:
case 2:
{
int how_many = 1;
if(chak==2)
{
cout<<"HOW MANY ELEMENTS DO YOU WANT TO ENTER."<<endl;
cin>>how_many;
cout<<"ENTER DATA ELEMENTS ONE BY ONE."<<endl;
}
else
{cout<<"ENTER A DATA ENTERY"<<endl;}
for(int i = 0; i < how_many; i++)
{
cin>>data;
list.add_node(data);
}
break;
}
case 3:
case 4:
case 5:
case 9:
{
list.display(chak);
break;
}
case 6:
{
cout<<"ENTER DATA VALUE TO BE SEARCHED."<<endl
;cin>>data;
list.search(data);
break;
}
case 7:
cout<<list.getroot()<<endl;
break;
case 8:
cout<<"ENTER DATA VALUE TO BE DELETED."<<endl;
cin>>data;
list.deletion(data);
break;
case 10:
break;
default:
cout<<"BAD CHOICE. TRY AGAIN.\n";
}
}while(chak != 10);
cout<<endl;
return 0;
}