program binary_tree;
uses
wincrt;
type
pbinarytree = ^node;
node = record
info : integer;
left : pbinarytree;
right: pbinarytree;
end;
var
binarytree : pbinarytree;
pil : char;
procedure insert(var bt:pbinarytree; info:integer);
var
x:pbinarytree;
begin
if (bt=nil) then
begin
new(x);
x^.info:=info;
x^.left:=nil;
x^.right:=nil;
bt:=x;
end
else
if (info <=bt^.info) then
insert(bt^.left,info)
else
insert(bt^.right,info);
end;
function find(bt:pbinarytree; search:integer):boolean;
begin
if (bt=nil) then
find:= false
else
if (search find:=find(bt^.left,search)
else
if (search>bt^.info)then
find:=find(bt^.right,search)
else
find:=true;
end;
function delete(var bt:pbinarytree; info:integer):boolean;
var
x,y:pbinarytree;
begin
if not (find(bt,info))then
delete:=false
else
if (info delete:=delete(bt^.left,info)
else
if (info>bt^.info) then
delete:=delete(bt^.right,info)
else
begin
x:=bt;
if(x^.right<>nil)then
begin
bt:=x^.right;
y:=bt;
while(y^.left<>nil) do
y:=y^.left;
y^.left:=x^.left;
end
else
bt:=x^.left;
delete:=true;
end;
end;
function nodecount(bt:pbinarytree):integer;
begin
if(bt=nil) then
nodecount:=0
else
nodecount:=1+nodecount(bt^.left)+nodecount(bt^.right);
end;
procedure inorder(bt :pbinarytree);
begin
if (bt<>nil) then
begin
inorder(bt^.left);
write(bt^.info:6);
inorder(bt^.right);
end;
end;
procedure preorder(bt :pbinarytree);
begin
if (bt<>nil) then
begin
write(bt^.info:6);
preorder(bt^.left);
preorder(bt^.right);
end;
end;
procedure postorder(bt:pbinarytree);
begin
if (bt<>nil)then
begin
postorder(bt^.left);
postorder(bt^.right);
write(bt^.info:6);
end;
end;
procedure menu;
begin
clrscr;
writeln('[ ORDERED BINARY TREE ]');
writeln('=======================');
writeln;
writeln('[1] INPUT DATA');
writeln('[2] CARI DATA');
writeln('[3] HAPUS DATA');
writeln('[4] JUMLAH NODE ');
writeln('[5] TAMPILKAN DATA SECARA IN-ORDER');
writeln('[6] TAMPILKAN DATA SECARA PRE-ORDER');
writeln('[7] TAMPILKAN DATA SECARA POST-ORDER');
writeln;
writeln('[0] EXIT');
writeln('=======================');
write('Pilihan anda : ');
end;
procedure inputdata(var bt:pbinarytree);
var
code,k,d :integer;
i:string;
begin
clrscr;
writeln('[ INPUT DATA BINARY TREE ]');
writeln('==========================');
writeln;
k:=1;
repeat
write('Masukkan data ke-', k,' :');
readln(i);
if (i<>'')then
begin
val(i,d,code);
if(code=0)then
insert(bt,d);
end;
inc(k)
until(i='');
end;
procedure caridata(bt:pbinarytree);
var
s:integer;
ulangi:char;
begin
clrscr;
writeln('[ MENCARI DATA BINARY TREE ]');
writeln('============================');
writeln;
repeat
write('Masukkan data yang akan dicari : ');readln(s);
if (find(bt,s)) then
writeln('Data ditemukkan')
else
writeln('Data Tidak ada');
writeln;
writeln('Ulangi [Y/T]? :');
ulangi:=readkey;
writeln;
until (upcase(ulangi) = 'T');
end;
procedure hapusdata(var bt:pbinarytree);
var
s:integer;
ulangi:char;
begin
clrscr;
writeln('[ HAPUS DATA BINARY TREE ]');
writeln('============================');
writeln;
repeat
write('Masukkan data yang akan dihapus : ');readln(s);
if (delete(bt,s)) then
writeln('Data berhasil dihapus')
else
writeln('Data Tidak ada');
writeln;
writeln('Ulangi [Y/T]? :');
ulangi:=readkey;
writeln;
until (upcase(ulangi) = 'T');
end;
procedure jumlahnode(bt:pbinarytree);
begin
clrscr;
writeln('Jumlah node= ',nodecount(bt));
write('Tekan sembarang tombol...');
readkey;
end;
procedure cetakinorder(bt:pbinarytree);
begin
clrscr;
writeln('IN-ORDER');
inorder(bt);
writeln;
write('Tekan sembarang tombol...');
readkey;
end;
procedure cetakpreorder(bt:pbinarytree);
begin
clrscr;
writeln('PRE-ORDER');
preorder(bt);
writeln;
write('Tekan sembarang tombol...');
readkey;
end;
procedure cetakpostorder(bt:pbinarytree);
begin
clrscr;
writeln('POST-ORDER');
postorder(bt);
writeln;
write('Tekan sembarang tombol...');
readkey;
end;
begin
binarytree :=nil;
repeat
menu;
pil:=readkey;
case pil of
'1' : inputdata(binarytree);
'2' : caridata(binarytree);
'3' : hapusdata(binarytree);
'4' : jumlahnode(binarytree);
'5' : cetakinorder(binarytree);
'6' : cetakpreorder(binarytree);
'7' : cetakpostorder(binarytree);
end;
until pil = '0';
end.
[Pascal] Binary Tree
Reviewed by
I Made Purwantara
on
10.10
Rating:
5
Tidak ada komentar: