.


:




:

































 

 

 

 





1. , , , .

Type Uk=^Tree;

Tree=record

Inf:integer;

Left, right:Uk;

End;

, , (Root).

2. :

;

;

;

.

. :

procedure Init (var root:Uk);

begin

root :=nil;

end;

 

:

( , ),

, , ,

, .. .

 

.

 

Function Poisk (var root:Uk; a:integer):Uk;

{- }

Begin

p:=root;

while (p<>nil) and (p^.inf<>a) do

if a<p^.inf then p:=p^.left

else p:=p^.right;

Poisk:=p;

End;

 

, .

2.3

, :

(). .

. , , .

. , , . : , .

Procedure Delete (var r:Uk; a:integer);

{r- , - }

{ }

Procedure DEL (var r:Uk; q:Uk);

{r- , q- }

Var q1:Uk;

Begin

if r^.right=nil then { }

begin

q^.inf:=r^.inf; { }

q1:=r;

r:=r^.left; { }

dispose(q1); { }

end

else DEL(r^.right,q); { }

End;

Var q:Uk;

Begin

if r=nil then writeln(' ')

else { }

if a<r^.inf then { , }

DELETE(r^.left,a)

else

if a>r^.inf then { , }

DELETE (r^.right,a)

else

begin

{ }

if r^.right=nil then

{ }

begin

q:=r;

r:=r^.left;

dispose(q);

end

else

if r^.left=nil then { }

begin

q:=r;

r:=r^.right;

dispose(q);

end

else

{ }

DEL(r^.left,r);

end;

end;

2.4 . . Postorder.

Procedure Del_tree (var beg:Uk);

Begin

If beg<>nil then begin

Del_tree(beg^.left)

Del_tree(beg^.right);

Dispose(beg);

Beg:=nil;

End;

End;

, . -, . , , , , . , , . :

 

type uk=^derev;

derev=record

inf:string;

cen:integer;

cena:integer;

im:string;

l,r:uk;

spisok:ukaz;

end;

, Var , :

var root:uk;

, :

Procedure Init (var root:Uk);

begin

root :=nil;

end;

 

. :

procedure poisk_imja(var p:uk);

var g:ukaz;

begin

if p<> nil then

begin

poisk_imja(p^.r);

g:=p^.spisok;

 

while g<>nil do

begin

if g^.imja=s then

begin

writeln(g^.imja:3);

writeln(g^.strana:3);

writeln(g^.koli4estvo:3);

writeln(g^.cena:3);

fl:=true;

end;

g:=g^.next;

end;

poisk_imja(p^.l);

if fl=false then writeln(' !');

if s<p^.im then poisk_imja(p^.l)

else poisk_imja(p^.r);

end;

end;

:

procedure poisk_strana(var p:uk);

var g:ukaz;

begin

if p<> nil then

begin

poisk_strana(p^.r);

g:=p^.spisok;

while g<>nil do

begin

if g^.strana=f then

begin

writeln(g^.imja:3);

writeln(g^.strana:3);

writeln(g^.koli4estvo:3);

writeln(g^.cena:3);

fl:=true;

end;

g:=g^.next;

end;

poisk_strana(p^.r);

end;

if fl=false then writeln(' !');

end;

 

:

procedure poisk_cena(p:uk);

var g:ukaz;

begin

if p<> nil then

begin

poisk_cena(p^.l);

g:=p^.spisok;

while g<>nil do

begin

if g^.cena=k then

begin

writeln(p^.im);

writeln(g^.imja:3);

writeln(g^.strana:3);

writeln(g^.koli4estvo:3);

writeln(g^.cena:3);

fl:=true;

end;

g:=g^.next;

end;

poisk_cena(p^.r);

end;

if fl=false then writeln(' !'); end;

:

procedure poisk_koli4estvo(p:uk);

var g:ukaz;

koli4estvo:integer;

begin

if p<> nil then

begin

poisk_koli4estvo(p^.l);

g:=p^.spisok;

while g<>nil do

begin

if g^.koli4estvo=m then

begin

writeln(g^.imja:3);

writeln(g^.strana:3);

writeln(g^.koli4estvo:3);

writeln(g^.cena:3);

fl:=true;

end;

g:=g^.next;

end;

poisk_koli4estvo(p^.r);

end;

if fl=false then writeln(' !');

end;

 

, . , , :

(preorder): , , .

Procedure PREORDER(var p:Uk);

Begin

if p<>nil then

begin

writeln(p^.inf);

PREORDER(p^.l);

PREORDER(p^.r);

end;

End;

 

, , , , : , , , .

procedure vivod(p:uk);

var g:ukaz;

begin

if p<>nil then

begin

vivod(p^.r);

g:=p^.spisok;

writeln(p^.im);

while g<>nil do

begin

writeln(g^.imja:5);

writeln(g^.strana:5);

writeln(g^.cena:5);

writeln(g^.koli4estvo:5);

g:=g^.next;

end;

vivod(p^.r);

end;

end;

begin

writeln(' --- ');

readln(k);

poisk_cena(root);

writeln(' --- ');

readln(s);

poisk_imja(root);

writeln(' --- ');

readln(m);

poisk_koli4estvo(root);

writeln(' --- ');

readln(f);

poisk_strana(root);

end;

 

, :

procedure vivod_v_fail(p:uk);

var g:ukaz;

begin

if p<>nil then

begin

vivod_v_fail(p^.l);

g:=p^.spisok;

while g<>nil do

begin

writeln(x,g^.imja:3);

writeln(x,g^.strana:3);

writeln(x,g^.koli4estvo:3);

writeln(x,g^.cena:3);

g:=g^.next;

end;

vivod_v_fail(p^.r);

end;

end;

 

, , :

procedure menu1;

begin

writeln(' : ');

writeln(' 1 ');

writeln(' 2 ');

writeln(' 3 ');

writeln(' 4 ');

writeln(' 5 ');

writeln(' 6 ');

end;

procedure podmenu;

begin

writeln(' 1 ');

writeln(' 2 ');

writeln(' 3 ');

writeln(' 4 ');

end;

procedure podmenu2;

begin

writeln('1 - ');

poisk_imja(root);

writeln(' 2 - ');

poisk_koli4estvo(root);

end;

 

- ! , , , -.

Program Cvetochny_Magazin;

uses crt;

type ukaz=^spisok;

spisok=record

imja:string;

strana:string;

cena,koli4estvo:integer;

next:ukaz;

end;

type uk=^derev;

derev=record

inf:string;

cen:integer;

cena:integer;

im:string;

l,r:uk;

spisok:ukaz;

end;

var root:uk;

p:ukaz;

a,m:integer;

k:byte;

f,s:string;

fl:boolean;

x:text;

procedure Init(var root:Uk);

begin

root:=nil;

end;

procedure add_1(var temp:uk);

var w:ukaz;

begin

new(w);

with w^ do

begin

write(' ---> ');

readln(imja);

write(' ---> ');

readln(strana);

write(' ---> ');

readln(cena);

write(' ---> ');

readln(koli4estvo);

next:=nil;

end;

if temp^.spisok=nil then

temp^.spisok:=w

else

begin

w^.next:=temp^.spisok;

temp^.spisok:=w;

end;

end;

procedure add(var root:uk);

var p,p1,w:uk;

n:integer;

begin

new(w);

with w^ do

begin

spisok:=nil;

l:=nil;

r:=nil;

end;

read(n);

while n=1 do

begin

add_1(w);

readln(n);

end;

if root=nil then root:=w

else

begin

p:=root;

while p<>nil do

begin

p1:=p;

if w^.cen<p^.cen then p:=p^.l

else p:=p^.r;

end;

if w^.cena<p1^.cena then p1^.l:=w

else p1^.r:=w;

end;

end;

procedure poisk_imja(var p:uk);

var g:ukaz;

begin

if p<> nil then

begin

poisk_imja(p^.r);

g:=p^.spisok;

while g<>nil do

begin

if g^.imja=s then

begin

writeln(g^.imja:3);

writeln(g^.strana:3);

writeln(g^.koli4estvo:3);

writeln(g^.cena:3);

fl:=true;

end;

g:=g^.next;

end;

poisk_imja(p^.l);

if fl=false then writeln(' !');

if s<p^.im then poisk_imja(p^.l)

else poisk_imja(p^.r);

end;

end;

procedure poisk_strana(var p:uk);

var g:ukaz;

begin

if p<> nil then

begin

poisk_strana(p^.r);

g:=p^.spisok;

while g<>nil do

begin

if g^.strana=f then

begin

writeln(g^.imja:3);

writeln(g^.strana:3);

writeln(g^.koli4estvo:3);

writeln(g^.cena:3);

fl:=true;

end;

g:=g^.next;

end;

poisk_strana(p^.r);

end;

if fl=false then writeln(' !');

end;

procedure poisk_cena(p:uk);

var g:ukaz;

begin

if p<> nil then

begin

poisk_cena(p^.l);

g:=p^.spisok;

while g<>nil do

begin

if g^.cena=k then

begin

writeln(p^.im);

writeln(g^.imja:3);

writeln(g^.strana:3);

writeln(g^.koli4estvo:3);

writeln(g^.cena:3);

fl:=true;

end;

g:=g^.next;

end;

poisk_cena(p^.r);

end;

if fl=false then writeln(' !');

end;

procedure poisk_koli4estvo(p:uk);

var g:ukaz;

koli4estvo:integer;

begin

if p<> nil then

begin

poisk_koli4estvo(p^.l);

g:=p^.spisok;

while g<>nil do

begin

if g^.koli4estvo=m then

begin

writeln(g^.imja:3);

writeln(g^.strana:3);

writeln(g^.koli4estvo:3);

writeln(g^.cena:3);

fl:=true;

end;

g:=g^.next;

end;

poisk_koli4estvo(p^.r);

end;

if fl=false then writeln(' !');

end;

Procedure PREORDER(var p:Uk);

Begin

if p<>nil then

begin

writeln(p^.inf);

PREORDER(p^.l);

PREORDER(p^.r);

end;

End;

procedure vivod(p:uk);

var g:ukaz;

begin

if p<>nil then

begin

vivod(p^.r);

g:=p^.spisok;

writeln(p^.im);

while g<>nil do

begin

writeln(g^.imja:5);

writeln(g^.strana:5);

writeln(g^.cena:5);

writeln(g^.koli4estvo:5);

g:=g^.next;

end;

vivod(p^.r);

end;

end;

procedure vivod_v_fail(p:uk);

var g:ukaz;

begin

if p<>nil then

begin

vivod_v_fail(p^.l);

g:=p^.spisok;

while g<>nil do

begin

writeln(x,g^.imja:3);

writeln(x,g^.strana:3);

writeln(x,g^.koli4estvo:3);

writeln(x,g^.cena:3);

g:=g^.next;

end;

vivod_v_fail(p^.r);

end;

end;

procedure menu1;

begin

writeln(' : ');

writeln(' 1 ');

writeln(' 2 ');

writeln(' 3 ');

writeln(' 4 ');

writeln(' 5 ');

end;

procedure podmenu;

begin

writeln(' 1 ');

writeln(' 2 ');

writeln(' 3 ');

writeln(' 4 ');

end;

begin

fl:=false;

repeat

menu1;

readln(a);

case a of

1:add(root);

2:begin

repeat

podmenu;

readln(a);

case a of

1:begin

writeln(' ---> ');

readln(s);

poisk_imja(root);

preorder(root);

end;

2:begin

writeln(' ---> ');

readln(f);

poisk_strana(root);

end;

3:begin

writeln(' ---> ');

poisk_cena(root);

readln(k);

end;

4:begin

writeln(' ---> ');

poisk_koli4estvo(root);

readln(m);

end;

end;

until a>4;

end;

3:begin

writeln(' : --- ');

readln(k);

poisk_cena(root);

writeln(' : --- ');

readln(s);

poisk_imja(root);

writeln(' : --- ');

readln(m);

poisk_koli4estvo(root);

writeln(' : --- ');

readln(f);

poisk_strana(root);

end;

4:begin

assign(x,' .txt ');

rewrite(x);

vivod_v_fail(root);

close(x);

end;

5:vivod(root);

end;

until a=5;

end.

5. :

:

 

:

:

:

:

1. .. . . .: . .. . 2001 .

2. .. , .. , .. . : . ..: . , 2000 .

3. : / .. . .: , 2001 .

4. .. Pascal 7.0. . . .: -, 2000 .





:


: 2016-03-27; !; : 421 |


:

:

, ,
==> ...

1474 - | 1393 -


© 2015-2024 lektsii.org - -

: 0.214 .