Динамические структуры данных



Download 115 Kb.
bet8/9
Sana01.03.2022
Hajmi115 Kb.
#475698
1   2   3   4   5   6   7   8   9
Bog'liq
Динамические структуры данных

Приложение.
1 Текст программы
Просмотр дерева слева - направо
procedure ViewLR(Root:PNode); {LR -> Left - Right }
begin
if Root<>Nil then
begin
ViewLR(Root^. left); {просмотр левого поддерева}
{Операция обработки корневого элемента - вывод на печать, в файл и др.}
ViewLR(Root^.right); { просмотр правого поддерева }
end;
end;
Просмотр справа налево
procedure ViewRL(Root:PNode); {LR -> Right - Left}
begin
if Root<>Nil then
begin
ViewRL(Root^.right); { правого }ViewRL(Root^.left); { левого }
end;
end;
Просмотр сверху - вниз
procedure ViewTD(Root:PNode); {TD -> Top-Down}
begin
if Root<>Nil then
begin
Операция обработки корневого элемента - вывод на печать, в файл и др.
ViewTD(Root^.left); {просмотр левого поддерева}
ViewTD(Root^.right); { просмотр правого поддерева }
end;
end;
Просмотр снизу-вверх
procedure ViewDT(Root:PNode); {DT -> Down - Top}
begin
if Root<>Nil then
begin
ViewDT(Root^.left); {просмотр левого поддерева}
ViewDT(Root^.right); { просмотр правого поддерева }
end;
end;
Поиск элемента в двоичном упорядоченном дереве
function Search(SearchValue:integer;Root:PNode):PNode;
begin
if (Root=Nil) or (Root^.Data=SearchValue) then
Search := Root
else if (Root^.Data > SearchValue) then
Search := Search(SearchValue,Root^.left)
else
Search := Search(SearchValue,Root^.right);
end;
2 Текст программы
бинарный дерево динамический алгоритм
type link = ^element;
element = record
data : integer;
left : link;
right : link;
end;
var
m,x, depth, minim : integer;
pn : link;
procedure add(var n : link; arg:integer);
var
ind, neo : link;
begin
new(neo);
neo^.data:=arg;
neo^.left:=nil;
neo^.right:=nil;
if n=nil then n:= neo
else begin
ind:=n;
while neo<>nil do begin
if argthen begin
if ind^.left=nil then begin
ind^.left:=neo;
neo:=nil
end
else ind:=ind^.left
end
else
if arg>ind^.data then begin
if ind^.right=nil then begin
ind^.right:=neo;
neo:=nil
end
else ind:=ind^.right
end
else begin
writeln('Such element is already existent');
neo:=nil;
end;
end;
end;
end; { add }
procedure restruct(var d : link);
var
ind1, ind2 : link;
begin
ind1:=d;
if ind1^.right=nil then begin
ind2:=d;
d:=ind2^.left;
dispose(ind2)
end
else
if ind1^.left=nil then begin
ind2:=d;
d:=ind2^.right;
dispose(ind2)
end
else begin
ind2:=ind1^.left;
while ind2^.right<>nil do begin
ind1:=ind2;
ind2:=ind2^.right;
end;
ind1^.right:=ind2^.left;
ind2^.left:=d^.left;
ind2^.right:=d^.right;
dispose(d);
d:=ind2;
end;
end; { restruct }
procedure delete(var n : link; arg:integer);
var
del, ind : link;
t : boolean;
begin
t:=false;
del:=n;
while (del<>nil) and (not t) do begin
if arg=del^.data then t:=true
else
if argind:=del;
del:=del^.left;
end
else begin
ind:=del;
del:=del^.right;
end;
end;
if t then begin
if (del^.left=nil) and (del^.right=nil) then begin
if del=n then begin n:=nil; dispose(del) end else
if ind^.left=del then begin
ind^.left:=nil;
dispose(del)
end
else begin
ind^.right:=nil;
dispose(del)
end
end
else
if del=n then restruct(n) else
if ind^.left=del then restruct(ind^.left)
else restruct(ind^.right)
end
else writeln('Element is absent');
end; { delete }
procedure view( n : link; var d:integer);
var
i : integer;
begin
for i:=1 to d do begin
write(' ') end;
writeln(n^.data);
if (n^.left=nil) and (n^.right=nil) then d:=d-1
else begin
if n^.right<>nil then begin
d:=d+1;
view(n^.right,d);
end;
if n^.left<>nil then begin
d:=d+1;
view(n^.left, d);
end;
d:=d-1;
end;
end; { view }
procedure obhod1( n : link; var d, min:integer);
begin
if (n^.left=nil) and (n^.right=nil) then begin
if dd:=d-1 end
else begin
if n^.right<>nil then begin
d:=d+1;
obhod1(n^.right, d, min); end;
if n^.left<>nil then begin
d:=d+1;
obhod1(n^.left, d,min) end;
d:=d-1;
end;
end; { obhod1 }
procedure obhod2( n : link; var d:integer; min:integer);
begin
if (n^.left=nil) and (n^.right=nil) then begin
if d=min then writeln(n^.data);
d:=d-1;
end
else begin
if n^.right<>nil then begin
d:=d+1;
obhod2(n^.right,d,min);
end;
if n^.left<>nil then begin
d:=d+1;
obhod2(n^.left, d,min);
end;
d:=d-1;
end;
end; { obhod2 }
begin
m:=1;
pn:=nil;
while m<>0 do begin
writeln;
writeln('--- Type "1" to ADD new element');
writeln('--- Type "2" to DELETE element');
writeln('--- Type "3" to VIEW tree');
writeln('--- Type "4" to FIND elements with minimal depth');
writeln('--- Type "0" to EXIT program');
writeln;
write('Enter : ');
readln(m);
writeln;
case m of
1 : begin
write('Enter new element : ');
readln(x);
add(pn, x);
end;
2 : begin
write('Enter element you want to delete : ');
readln(x);
delete(pn, x);
end;
3 : begin
depth:=1;
if pn=nil then writeln('The tree is empty') else begin
writeln('The tree is : ');
view(pn, depth);
end;
end;
4 : begin
depth:=1;
minim:=20;
if pn<>nil then begin
writeln('Elements with minimal depth');
obhod1(pn,depth,minim);
writeln(minim);
depth:=1;
obhod2(pn,depth,minim);
end
else writeln('The tree is empty');
end;
end; { case }
end;
end.

Download 115 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish