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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
| program AVLtreework;
uses CRT,Graph;
type PTree = ^TTree;
TTree = record
info:byte;
balance: integer;
left,right: PTree;
end;
function max(v1,v2:integer):integer;
begin
if (v1>v2) then
max:=v1
else
max:=v2;
end;
function getint(ident:string):byte;
var s:byte;
begin
write('Введите ',ident,' : ');
readln(s);
getint:=s;
end;
procedure RotateSingleLL(var root:PTree);
Var LBranch:PTree;
begin
LBranch:=root^.left;
if (LBranch^.balance=-1) then
begin
LBranch^.balance:=0;
root^.balance:=0;
end
else
begin
LBranch^.balance:=1;
root^.balance:=-1;
end;
root^.left:=LBranch^.right;
Lbranch^.right:=root;
root:=LBRanch;
end;
procedure RotateSingleRR(var root:PTree);
var RBranch:PTree;
begin
RBranch:=root^.right;
if (RBranch^.balance=1) then
begin
RBranch^.balance:=0;
root^.balance:=0;
end
else
begin
RBranch^.balance:=-1;
root^.balance:=1;
end;
root^.right:=RBranch^.left;
RBranch^.left:=root;
root:=RBRanch;
end;
procedure RotateDoubleLR(var root:PTree);
Var LBranch,LRBranch:PTree;
begin
Lbranch:=root^.left;
LRbranch:=Lbranch^.right;
if (LRbranch^.balance = 1) then
lbranch^.balance:=-1
else
lbranch^.balance:=0;
if (LRBranch^.balance = -1) then
root^.balance:=1
else
root^.balance:=0;
lrbranch^.balance:=0;
root^.left:=LRbranch^.right;
Lbranch^.right:=LRbranch^.left;
LRbranch^.left:=Lbranch;
LRBranch^.right:=root;
root:=LRbranch;
end;
procedure RotateDoubleRL (var root:PTree);
Var Rbranch,RLbranch:PTree;
begin
Rbranch:=root^.right;
RLbranch:=RBranch^.left;
if (RLBranch^.balance = -1) then
rbranch^.balance:=1
else
rbranch^.balance:=0;
if (RLBranch^.balance=1) then
root^.balance:=-1
else
root^.balance:=0;
rlbranch^.balance:=0;
root^.right:=RLbranch^.left;
Rbranch^.left:=RLbranch^.right;
RLbranch^.right:=Rbranch;
RLbranch^.left:=root;
root:=RLbranch;
end;
procedure RebalanceTree(var root:PTree;var rebalance:boolean);
var branch:PTree;
begin
if (root^.balance=-1) then
begin
branch:=root^.left;
if (branch^.balance=-1) or (branch^.balance=0) then
begin
RotateSingleLL(root);
rebalance:=false;
end
else
if (branch^.balance=1) then
begin
RotateDoubleLR(root);
rebalance:=false;
end;
end
else
if (root^.balance=1) then
begin
branch:=root^.right;
if (branch^.balance=1) or (branch^.balance=0) then
begin
RotateSingleRR(root);
rebalance:=false;
end
else
if (branch^.balance=-1) then
begin
RotateDoubleRL(root);
rebalance:=false;
end;
end;
end;
Procedure HeavyLeft(var root:PTree;var rebalance:boolean);
begin
case root^.balance of
-1 : begin
RebalanceTree(root,rebalance);
end;
0 : begin
root^.balance:=-1;
rebalance:=true;
end;
1 : begin
root^.balance:=0;
rebalance:=false;
end;
end;
end;
Procedure HeavyRight(var root:PTree;var rebalance:boolean);
begin
case root^.balance of
-1 : begin
root^.balance:=0;
rebalance:=false;
end;
0 : begin
root^.balance:=1;
rebalance:=true;
end;
1: begin
RebalanceTree(root,rebalance);
end;
end;
end;
procedure addelem(var root:PTree;info:byte;var rebalance:boolean);
var elem:PTree;
rebalancethiselem:boolean;
begin
if (root=NIL) then
begin
new(elem);
elem^.left:=NIL;
elem^.right:=NIL;
elem^.info:=info;
elem^.balance:=0;
rebalance:=TRUE;
root:=elem;
end
else
begin
if (info<root^.info) then
begin
addelem(root^.left,info,RebalanceThisElem);
if (RebalanceThisElem) then
HeavyLeft(root,rebalance)
else
rebalance:=false;
end
else
begin
addelem(root^.right,info,rebalancethiselem);
if (rebalancethiselem) then
begin
HeavyRight(root,rebalance)
end
else
rebalance:=false;
end;
end;
end;
procedure addelem_wrapper(var root:PTree;info:byte);
var rebalance:boolean;
begin
rebalance:=false;
addelem(root,info,rebalance);
end;
procedure printLKP(root:PTree);
begin
if (root<>NIL) then
begin
printLKP(root^.left);
write(root^.info,' ');
printLKP(root^.right);
end;
end;
procedure printLKP_wrapper(root:PTree);
begin
clrscr;
if (root=NIL) then
writeln('Дерево пусто!')
else
PrintLKP(root);
writeln;
writeln('Нажмите любую клавишу для выхода в главное меню');
readkey;
end;
procedure printKLP(root:PTree);
begin
if (root<>NIL) then
begin
write(root^.info,' ');
printKLP(root^.left);
printKLP(root^.right);
end;
end;
procedure printKLP_wrapper(root:PTree);
begin
clrscr;
if (root=NIL) then
writeln('Дерево пусто!')
else
PrintKLP(root);
writeln;
writeln('Нажмите любую клавишу для выхода в главное меню');
readkey;
end;
procedure printLPK(root:PTree);
begin
if (root<>NIL) then
begin
printLPK(root^.left);
printLPK(root^.right);
write(root^.info,' ');
end;
end;
procedure printLPK_wrapper(root:PTree);
begin
clrscr;
if (root=NIL) then
writeln('Дерево пусто!')
else
PrintLPK(root);
writeln;
writeln('Нажмите любую клавишу для выхода в главное меню');
readkey;
end;
function countels(root:PTree):integer;
begin
if (root<>NIL) then
countels:=1+countels(root^.left)+countels(root^.right)
else
countels:=0;
end;
procedure countels_wrapper(root:PTree);
begin
writeln('Число вершин дерева : ',countels(root));
writeln('Нажмите любую клавишу');
writeln;
readkey;
end;
function countleafs(root:PTree):integer;
begin
if (root<>NIL) then
if (root^.left=NIL) and (root^.right=NIL) then
countleafs:=1
else
countleafs:=countleafs(root^.left)+countleafs(root^.right)
else
countleafs:=0;
end;
procedure countleafs_wrapper(root:PTree);
begin
writeln('Число листов дерева : ',countleafs(root));
writeln;
writeln('Нажмите любую клавишу');
readkey;
end;
function countdepth(root:PTree;level:integer):integer;
var dr,dl:integer;
begin
if (root=NIL) then
countdepth:=level-1
else
countdepth:=max(countdepth(root^.left,level+1),countdepth(root^.right,level+1));
end;
procedure countdepth_wrapper(root:PTree);
begin
if (root<>NIL) then
begin
writeln('Глубина дерева : ',countdepth(root,0));
writeln;
writeln('Нажмите любую клавишу');
end
else
writeln('Дерево пусто!');
readln;
end;
function getmostright(root:PTree):byte;
begin
if (root^.right=NIL) then
getmostright:=root^.info
else
getmostright:=getmostright(root^.right);
end;
procedure DelRebalanceTree(var root:PTree;var rebalance:boolean);
var branch:PTree;
begin
if (root^.balance=-1) then
begin
branch:=root^.left;
if (branch^.balance=-1) or (branch^.balance=0) then
begin
RotateSingleLL(root);
rebalance:=false;
end
else
if (branch^.balance=1) then
begin
RotateDoubleLR(root);
rebalance:=false;
end;
end
else
if (root^.balance=1) then
begin
branch:=root^.right;
if (branch^.balance=1) or (branch^.balance=0) then
begin
RotateSingleRR(root);
rebalance:=false;
end
else
if (branch^.balance=-1) then
begin
RotateDoubleRL(root);
rebalance:=false;
end;
end;
end;
Procedure LightLeft(var root:PTree;var rebalance:boolean);
begin
case root^.balance of
1 : begin
DelRebalanceTree(root,rebalance);
end;
0 : begin
root^.balance:=1;
rebalance:=false;
end;
-1 : begin
root^.balance:=0;
rebalance:=true;
end;
end;
end;
Procedure LightRight(var root:PTree;var rebalance:boolean);
begin
case root^.balance of
1 : begin
root^.balance:=0;
rebalance:=true;
end;
0 : begin
root^.balance:=-1;
rebalance:=false;
end;
-1: begin
DelRebalanceTree(root,rebalance);
end;
end;
end;
procedure delelem(var root:PTree;info:byte;var rebalance:boolean);
var temp:PTree;
RebalanceThisElem:boolean;
OldRebalance:boolean;
OldDepthL,OldDepthR:integer;
DepthL,DepthR:integer;
begin
if (root<>NIL) then
begin
if (info<root^.info) then
begin
delelem(root^.left,info,RebalanceThisElem);
if (RebalanceThisElem) then
LightLeft(root,rebalance)
else
rebalance:=false;
end
else
if (info>root^.info) then
begin
delelem(root^.right,info,RebalanceThisElem);
if (RebalanceThisElem) then
LightRight(root,rebalance)
else
rebalance:=false;
end
else
begin
if (root^.left=NIL) and (root^.right=NIL) then
begin
dispose(root);
root:=NIL;
rebalance:=TRUE;
end
else
if (root^.left=NIL) and (root^.right<>NIL) then
begin
temp:=root;
root:=root^.right;
dispose(temp);
rebalance:=TRUE;
end
else
if (root^.left<>NIL) and (root^.right=NIL) then
begin
temp:=root;
root:=root^.left;
dispose(temp);
rebalance:=TRUE;
end
else
begin
root^.info:=getmostright(root^.left);
olddepthl:=countdepth(root^.left,1);
olddepthr:=countdepth(root^.right,1);
delelem(root^.left,root^.info,RebalanceThisElem);
depthl:=countdepth(root^.left,1);
depthr:=countdepth(root^.right,1);
if (max(olddepthl,olddepthr)<>max(depthl,depthr)) or (abs(depthr-depthl)>1) then
LightLeft(root,rebalance)
else
begin
rebalance:=false;
root^.balance:=depthr-depthl;
end;
end;
end;
end
else
begin
rebalance:=false;
end;
end;
procedure delelem_wrapper(var root:PTree;info:byte);
var rb:boolean;
begin
rb:=false;
delelem(root,info,rb);
end;
procedure printlevel(root:Ptree;level,curlevel:integer);
begin
if (root<>NIL) then
begin
if (curlevel=level) then
write(root^.info,' ')
else
begin
printlevel(root^.left,level,curlevel+1);
printlevel(root^.right,level,curlevel+1);
end;
end;
end;
procedure printlevel_wrapper(root:PTree;level:integer);
begin
clrscr;
writeln('Все вершины на уровне ',level,' : ');
printlevel(root,level,0);
writeln;
writeln('Нажмите любую клавишу для выхода в главное меню');
readkey;
end;
procedure drawtree(root:PTree);
var Width,Height:integer;
CurVPort:ViewPortType;
s,sbal:string;
begin
if (root<>NIL) then
begin
GetViewSettings(CurVPort);
width:=CurVPort.x2-CurVPort.x1;
height:=CurVPort.y2-CurVPort.y1;
str(root^.info,s);
str(root^.balance,sbal);
sbal:='('+sbal+')';
SetColor(15);
Ellipse(width div 2,11,0,360,20,11);
SetFillStyle(SolidFill,7);
FloodFill(width div 2,11,15);
SetColor(8);
OutTextXY(width div 2,4,s);
OutTextXY(width div 2,12,sbal);
SetColor(15);
if (root^.left<>NIL) then
line(width div 2,22,width div 4,height);
if (root^.right<>NIL) then
line(width div 2,22,3*width div 4,height);
with CurVPort do
setviewport(x1,y2,x1+(width div 2),y2+height,ClipOff);
drawtree(root^.left);
with CurVPort do
setviewport(x1+(width div 2),y2,x2,y2+height,ClipOff);
drawtree(root^.right);
end;
end;
procedure drawtree_wrapper(root:PTree);
Var GraphDevice,GraphMode:integer;
PathToDriver:string;
begin
if (root<>NIL) then
begin
GraphDevice:=Detect;
PathToDriver:='';
InitGraph(GraphDevice,GraphMode,PathToDriver);
if (GraphResult<>grOK) then
begin
Writeln('Error initializing graphics!');
readkey;
end
else
begin
SetColor(White);
SetViewPort(15,0,GetMaxX-15,(GetMaxY div (countdepth(root,0)+1)),ClipOff);
SetTextJustify(CenterText,TopText);
drawtree(root);
readkey;
closegraph;
end
end
else
begin
writeln('Дерево пусто!');
readkey;
end;
end;
procedure showmenu;
begin
clrscr;
writeln(' AVL-дерево');
writeln;
writeln(' 1) Добавить элемент в дерево');
writeln(' 2) Распечатать дерево в виде левая ветвь - корень - правая ветвь (ЛКП)');
writeln(' 3) Распечатать дерево в виде корень - левая ветвь - правая ветвь (КЛП)');
writeln(' 4) Распечатать дерево в виде левая ветвь - правая ветвь - корень (ЛПК)');
writeln(' 5) Вывести число вершин дерева');
writeln(' 6) Вывести число листов дерева');
writeln(' 7) Удалить элемент из дерева');
writeln(' 8) Распечатать все вершины на заданном уровне');
writeln(' 9) Вывести глубину дерева');
writeln(' 10) Нарисовать дерево');
writeln(' 11) Выход');
writeln;
write('Ваш выбор : ');
end;
Var Tree:PTree;
selection:integer;
begin
Tree:=NIL;
repeat
showmenu;
readln(selection);
writeln;
case selection of
1: addelem_wrapper(Tree,getint('элемент для добавления'));
2: printLKP_wrapper(Tree);
3: printKLP_wrapper(Tree);
4: printLPK_wrapper(Tree);
5: countels_wrapper(Tree);
6: countleafs_wrapper(Tree);
7: delelem_wrapper(Tree,getint('элемент для удаления'));
8: printlevel_wrapper(Tree,getint('уровень, который нужно распечатать'));
9: countdepth_wrapper(Tree);
10: drawtree_wrapper(Tree);
11:clrscr;
end;
until selection=11;
end. |