Tuesday, 23 October 2012

Program Deret Angka Binary Tree Inorder Preorder Postorder (PASCAL)

Program Menyusun_Deretan_Angka_Dengan_Binary_Tree;
uses Crt;

type
PTR = ^Data;
Data = record
Info: Integer;
Leftson,Rightson: PTR;
end;

type
angkaa = array [1..100]of integer;
var
BinaryTree: PTR;

Menu : char;
Selesai : char;
Angka : integer;

{----------------------------------}

procedure InOrder(bt: PTR);
begin
if (bt <> nil) then
begin
InOrder(bt^.Leftson);
Write(bt^.Info:6);
InOrder(bt^.Rightson);
end;
end;
{----------------------------------}
procedure PreOrder(bt: PTR);
begin
if (bt <> nil) then
begin
Write(bt^.Info:6);
PreOrder(bt^.Leftson);
PreOrder(bt^.Rightson);
end;
end;
{----------------------------------}
procedure PostOrder(bt: PTR);
begin
if (bt <> nil) then
begin
PostOrder(bt^.Leftson);
PostOrder(bt^.Rightson);
Write(bt^.Info:6);
end;
end;

{----------------------------------}


Procedure Insert(var bt: PTR; Info: Integer);
var
Baru: PTR;
begin
if (bt = nil) then
begin
new(baru);
baru^.Info := Info;
baru^.Leftson := nil;
baru^.Rightson := nil;
bt := baru;
end
else
if (Info <= bt^.Info) then
Insert(bt^.Leftson, Info)
else
Insert(bt^.Rightson, Info);
end;

 {----------------------------------}

procedure MenuUtama;
begin
WriteLn('   Menyusun_Deretan_Angka_Dengan_Binary_Tree   ');
WriteLn('===============================================');
WriteLn;
WriteLn('1. Isi data!');
WriteLn('2. Tampilkan data secara In order');
WriteLn('3. Tampilkan data secara Pre order');
WriteLn('4. Tampilkan data secara Post order');
WriteLn('5. Keluar');

WriteLn;
Write('Silahkan pilih (1-5): ');
end;
{----------------------------------}

var i,n : integer;
begin
write('Masukkan Jumlah Data Deretan Angka!  :');readln(n);
selesai:='N';
repeat
clrscr;
menuUtama;
readln(menu);
case menu of
'1' : begin
clrscr;
for i := 1 to n do
begin
write('input data ke ',i,' :');readln(angka);
insert(binarytree,angka);

end;
end;


'2' : begin

writeln;
Writeln('Tampilkan Deretan Angka Secara In Order');
writeln;
Writeln('=======================================');
writeln;
inorder(binarytree);
writeln;
end;
'3' :begin

writeln;
Writeln('Tampilkan Deretan Angka Secara Pre Order');
Writeln;
Writeln('========================================');
writeln;
preorder(binarytree);
writeln;
end;
'4' : begin

writeln;
Writeln('Tampilkan Deretan Angka Secara Post Order');
Writeln;
Writeln('========================================');
writeln;
postorder(binarytree);
writeln;
end;

'5' :begin
selesai:='Y';
end;
end;
readln;
until (selesai='Y') or (selesai='y');
end.

No comments:

Post a Comment