
Sinh viên:D ng Anh Vũươ
L p Sp Tin 2ớ
1)
uses crt;
type
tree=^node;
node=record
info:integer;
left:tree;
right:tree;
end;
var
root:tree;x,tong,chon,sonut:integer;ch:char;
procedure Init(var root:tree);
begin
new(root);
root:=nil;
end;
procedure Add(var root:tree;x:integer);
var p,q,l:tree;
begin
new(p);
p^.info:=x;
p^.left:=nil;
p^.right:=nil;
if(root=nil)then
root:=p
else
begin
new(q);new(l);
q:=root;
while(q<>nil)and(p^.info<>q^.info)do
begin
l:=q;
if(p^.info>q^.info)then
q:=q^.right
else q:=q^.left;
end;
if(q=nil)then

if(p^.info>l^.info)then l^.right:=p
else if(p^.info<l^.info)then l^.left:=p
else if(x=q^.info)then write('da co');
end;
end;
procedure PrintLNR(root:tree);
begin
if(root<>nil)then
begin
printLNR(root^.left);
write(root^.info:4);
printLNR(root^.right);
end;
end;
function Sum(root:tree;var tong:integer):integer;
begin
if(root<>nil)then
begin
Sum:=Sum(root^.left,tong);
tong:=tong+1;
Sum:=Sum(root^.right,tong);
end;
Sum:=tong;
end;
function Find(root:tree;x:integer):boolean;
var p:tree;
begin
new(p);
p:=root;
while(p<>nil)and(p^.info<>x)do
begin
if(x>p^.info)then
p:=p^.right
else p:=p^.left;
end;
if(p=nil)then Find:=false
else Find:=true;
end;
procedure Delete(var root:tree;x:integer);
var p,q,l,r,t:tree;

begin
new(p);new(q);
q:=nil;
p:=root;
while(p<>nil)and(p^.info<>x)do
begin
q:=p;
if(x>p^.info)then
p:=p^.right
else p:=p^.left;
end;
if(p^.info=x)then
begin
if(p^.right=nil)and(p^.left=nil)then
if(x>q^.info)then q^.right:=nil
else q^.left:=nil;
if(p^.right=nil)and(p^.left<>nil)then
if(p^.info>q^.info)then
q^.right:=p^.left
else q^.left:=p^.left;
if(p^.right<>nil)and(p^.left=nil)then
if(p^.info>q^.info)then
q^.right:=p^.right
else q^.left:=p^.right;
if(p^.right<>nil)and(p^.left<>nil)then
begin
new(r);r:=p^.right;
new(t);t:=p;
while(r^.left<>nil)do
begin
t:=r;r:=r^.left;
end;
if(t^.info>r^.info)then
t^.left:=r^.right
else
t^.right:=r^.right;
p^.info:=r^.info;
end;
end;

end;
{function So_Node(root:tree;var sonut:integer):integer;
begin
if(root<>nil)then
begin
So_node:=So_Node(root^.left,sonut);
So_node:=So_node(root^.right,sonut);
if(root^.left=nil)and(root^.right=nil)then
inc(sonut);
end;
So_node:=sonut;
end;}
procedure So_Node(root:tree;var sonut:integer);
begin
if(root<>nil)then
begin
So_Node(root^.left,sonut);
So_node(root^.right,sonut);
if(root^.left=nil)and(root^.right=nil)then
inc(sonut);
end;
end;
begin
clrscr;
init(root);
repeat
writeln(' MENU');
writeln(' 1_Them ');
writeln(' 2_Tim ');
writeln(' 3_Xoa ');
writeln(' 4_TinhTong');
writeln(' 5_InCay ');
writeln(' 6_So_Nut_La');
writeln(' 7_Exit ');
Write('Ban chon:');readln(chon);
case(chon) of
1:begin
repeat

Write('Nhap phan tu can them(nhap -1 de dung):');
readln(x);
if(x<>-1)then
add(root,x);
until x=-1;
end;
2:begin
Write('nhap phan tu can tim:');
readln(x);
if(Find(root,x)=true)then
writeln('tim thay')
else writeln('khong tim thay');
end;
3:begin
write('nhap gia tri can xoa:');readln(x);
delete(root,x);
end;
4:begin
tong:=0;
writeln('Tong cay nhi phan la:',Sum(root,tong));
end;
5:begin
printLNR(root);
writeln;
end;
6:begin
sonut:=0;
so_node(root,sonut);
writeln('so nut la:',sonut);
end;
end
until chon=7;
end.
2)
Program GiaiThua;
Uses crt;
Var n: byte;
Function Giaithua(n:byte):longint;
Begin
If (n<=1) then