Home » » program turbo pascal

program turbo pascal

PROGRAM BINARY SEARCH TREE (POHON)

Uses Crt;
Type
Pointer = ^TypeData;
TypeData = Record
Nilai : integer;
Kiri,Kanan : Pointer;
End;
Var
Tree : Pointer;
{=========================== MEMBENTUK TREE===========================}
Procedure Sisip_Tree(Var Tree : Pointer; Data : Integer);
Var
Baru : Pointer;
Begin
if Tree = Nil Then
Begin
New(Baru);
Baru^.Nilai := Data;
Baru^.Kanan := Nil;
Baru^.Kiri := Nil;
Tree := Baru;
End
Else if Data > Tree^.Nilai Then
Sisip_Tree(Tree^.Kanan,Data)
Else if Data < Tree^.Nilai Then
Sisip_Tree(Tree^.Kiri,Data)
Else Writeln('Data ',Data,' Sudah Ada.....');
End;
{=================== TELUSUR SECARA IN ORDER =========================}
Procedure In_Order(Tree : Pointer);
Begin
if Tree <> Nil Then
Begin
In_Order(Tree^.Kiri);
Write(Tree^.Nilai:3);
In_Order(Tree^.Kanan);
End;
End;
{==================== TELUSUR SECARA POST ORDER ======================}
Procedure Post_Order(Tree : Pointer;var k : integer);
Begin
if Tree <> Nil Then
Begin
Post_Order(Tree^.Kiri,k);
Post_Order(Tree^.Kanan,k);
Write(Tree^.Nilai:3);
k:=k+Tree^.Nilai; {===> HITUNG JUMLAH SEMUA NODE }
End;
End;
{== TELUSUR SECARA PRE ORDER ==}
Procedure Pre_Order(Tree : Pointer);
Begin
if Tree <> Nil Then
Begin
Write(Tree^.Nilai:3);
Pre_Order(Tree^.Kiri);
Pre_Order(Tree^.Kanan);
End;
End;
{================= HITUNG JUMLAH NODE SEBELAH KIRI ===================}
Function Jumlah_Kiri(Tree : Pointer) : integer;
Var
p : Pointer;
k : Integer;
Begin
k:=0;
p:=Tree;
While p <> nil do
Begin
k:=k+p^.Nilai;
p:=p^.kiri;
end;
Jumlah_Kiri:=k;
End;
{================= HITUNG JUMLAH NODE SEBELAH KANAN ==================}
Function Jumlah_Kanan(Tree : Pointer) : integer;
Var
p : Pointer;
k : Integer;
Begin
k:=0;
p:=Tree;
While p <> nil do
Begin
k:=k+p^.Nilai;
p:=p^.kanan;
end;
Jumlah_Kanan:=k;
End;
{=========== HITUNG SEMUA JUMLAH NODE SEBELAH KANAN DAN ==============}
{============ APABILA KANAN TELAH NIL MAKA KE NODE KIRI =============}
Function Jumlah_Kanan_Kiri(Tree : Pointer) : integer;
Var
p : Pointer;
k,l : Integer;
Begin
k:=0;
p:=Tree;
While p <> nil do
Begin
k:=k+p^.Nilai;
if p^.Kanan <> Nil then p:=p^.Kanan
else
p:=p^.Kiri;
end;
Jumlah_Kanan_Kiri:=k;
End;
{======================= PROGRAM UTAMA ==============================}
Var
Bil,i,Jum,JumNode : Integer;
Begin
New(Tree);
Tree := Nil;
Randomize;
Repeat
Bil:=Random(100);
Sisip_Tree(Tree,Bil);
write(Bil:4);
i:=i+1;
until i=10;
Writeln;
write('Cetak Secara IN ORDER');
Writeln;
In_Order(Tree);
Writeln;writeln;
write('Cetak Secara Post ORDER');
Writeln;
Post_Order(Tree,JumNode);
Writeln;writeln;
write('Cetak Secara PRE ORDER');
Writeln;
Pre_Order(Tree);
Writeln;Writeln;
Writeln('Jumlah seluruh Node sebelah kiri ');
Jum:=Jumlah_Kiri(Tree);
writeln('Nilai Jumlah = ',Jum);
writeln;
Writeln('Jumlah seluruh Node sebelah Kanan ');
Jum:=Jumlah_Kanan(Tree);
writeln('Nilai Jumlah = ',Jum);
writeln;
Writeln('Jlh Node Kanan semua kemudian Kiri satu node
sebelumnya');
Jum:=Jumlah_Kanan_Kiri(Tree);
writeln('Nilai Jumlah = ',Jum);
writeln;
Writeln('Jumlah Seluruh Node Pada Tree ');
writeln('Nilai Jumlah = ',JumNode);
end.

HASIL NYA SEPERTI GAMBAR DI BAWAH



0 comments:

Posting Komentar