1. Trang chủ
  2. » Công Nghệ Thông Tin

cây trong passcall cài đặc bằng con trỏ

6 134 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 6
Dung lượng 33,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

tientut;writeln; readln; end.

Trang 1

Cây trong passcall cài đặc bằng con trỏ

uses crt;

const max=30;

type cay=^tree;

tree=record

data:integer;

lnext:cay;

rnext:cay;

end;

elementtype=cay;

var i, x:integer;

ch:char;

t:cay;

{========tao rong =========}

procedure taorong(var t:cay);

begin

t:=nil;

end;

{====== kiem tra rong =======}

function ktrong(var t:cay):boolean;

begin

ktrong:=(t=nil);

end;

{=========== xen ============}

procedure xen(var t:cay;x:integer);

begin

if t=nil then

begin

new(t);

t^.rnext:=nil;

t^.lnext:=nil;

t^.data:=x;

end else

if x>t^.data then

xen(t^.rnext,x)

else xen(t^.lnext,x);

end;

{===== nhap =========}

procedure nhap(var t:cay);

var n,m,x:integer;

begin

taorong(t);

write('nhap vao so nut cua cay:');

readln(n);

Trang 2

for i:=1 to n do

begin

write('nhap vao nut thu ',i,':');

readln(m);

xen(t,m);

end;

end;

{========== xuat ===========} procedure tientu(t:cay);

begin

if t<> nil then

begin

write(t^.data);

tientu(t^.lnext);

tientu(t^.rnext);

end;

end;

{========== xuat ===========} Procedure trung(var t:cay);

begin

if t<> nil then

begin

trung(t^.lnext);

write(t^.data:4);

trung(t^.rnext);

end;

end;

{============ Xuat ===========} Procedure hau(var t:cay);

begin

if t<> nil then

begin

hau(t^.lnext);

hau(t^.rnext);

write(t^.data:4);

end;

end;

{====== chieu cao cua cay=======} function mac(a,b:integer):integer;

begin

if a<b then mac:=b

else

mac:=a;

end;

{=============}

Trang 3

function chieucao(t:cay):integer;

begin

if t=nil then

chieucao:=0

else

begin

if (t^.lnext=nil) and (t^.rnext=nil) then chieucao:=0 else

chieucao:=mac(chieucao(t^.lnext),

chieucao(t^.rnext))+1;

end;

end;

{==== xoa nut nho nhat =======}

function xoa(var t:cay):integer;

begin

if t^.lnext=nil then

begin

xoa:=t^.data;

t:=t^.rnext;

end

else

xoa:=xoa(t^.lnext);

end;

{====== xoa mot nut =========}

procedure xoa1(var t:cay;x:integer);

begin

if t<> nil then

begin

if x>t^.data then

xoa1(t^.rnext,x)

else

if x<t^.data then

xoa1(t^.lnext,x)

else if (t^.lnext=nil) and (t^.rnext=nil)

then t:=nil else

if t^.lnext=nil then

t:=t^.rnext else

if t^.rnext=nil then

t:=t^.lnext else

t^.data:=xoa(t^.rnext);

end;

end;

{====== tong so nut ========}

function tong(var t:cay):integer;

begin

if t=nil then

Trang 4

else

tong:=tong(t^.lnext)+tong(t^.rnext)+1;

end;

{===== in nut la ==========}

procedure nutla(t:cay);

begin

if t<> nil then

begin

if (t^.lnext=nil) and (t^.rnext=nil) then

write(t^.data:4);

nutla(t^.lnext);

nutla(t^.rnext);

end;

end;

{====== nut le cua cay =========}

procedure nutle(var t:cay);

begin

if t<> nil then

begin

if odd(t^.data) then

write(t^.data:4);

nutle(t^.lnext);

nutle(t^.rnext);

end;

end;

{====== nut trung gian ========}

procedure trunggian(t:cay);

begin

if t<> nil then

begin

if (t^.lnext<> nil) or (t^.rnext<>nil) then

write(t^.data:4);

trunggian(t^.lnext);

trunggian(t^.rnext);

end;

end;

{===== in ra nut co mot con =====}

procedure nutcon(var t:cay);

begin

if t<> nil then

begin

if (t^.lnext=nil) and (t^.rnext<>nil)or ((t^.rnext<>nil) and (t^.rnext=nil)) then

begin

write(t^.data:4);

Trang 5

nutcon(t^.rnext);

end

else

write('khong co');

end;

end;

{=== tim kien nut cau cay =======}

function tim(x:integer;t:cay):boolean;

begin

if t<> nil then

if t^.data=x then

write('nut duoc tim thay trong cay')

else

if t^.data>x then

tim:=tim(x,t^.lnext)

else

tim:=tim(x,t^.rnext)

else

write('khong tim thay mut trong cay');

end;

{================chuong trinh chinh ========} begin

clrscr;

nhap(t);

write(' nut cua cay la; ');

tientu(t);writeln;readln;

write('tong so nut cua cay la : ',tong(t)); writeln; write(' nut la cua cay la : ');

nutla(t);writeln;

write(' nut le cua cay la : ');

nutle(t);writeln;

write(' nut trung gian cua cay la : ');

trunggian(t); writeln;

write('nut chi co mot con la: ');

nutcon(t);writeln;

write('chieu cao cau cay la: ',chieucao(t)); writeln; write('nhap vao nut ban muon tim : ');readln(x); tim(x,t);writeln;

write('ENTER de xoa nut nho nhat');readln;

write(' nut sau khi xoa la : ');

xoa(t);

tientu(t);writeln;

write('nhap vao nut ban muong xoa : ');readln(x); xoa1(t,x);

write('nut con lai trong cay la: ');

Trang 6

tientu(t);writeln; readln;

end

Ngày đăng: 28/08/2014, 18:18

TỪ KHÓA LIÊN QUAN

w