TRANSLATE HERE

Monday, 2 May 2016

CONTOH LAIN PROGRAM TREE DALAM PASCAL



Contoh : Type 2

Program PohonBiner;
Uses crt;
Type Btree = ^simpul;
    Simpul = record
    Isi : char;
    kiri,kanan : Btree;
    end;
var elemen : char;
    p, root, baru : Btree;
Procedure sisip(var root,baru : btree);
Begin
    baru^.kiri := nil;
    baru^.kanan := nil;
   if root = nil then
    root := baru
   else
     begin
      p := root;
      while p <> nil do
      begin
    if baru^.isi < p^.isi then
     begin
      if p^.kiri <> nil  then
        p := p^.kiri
      else
        begin
        p^.kiri := baru;
        p := nil;
        end;
         end
    else
     begin
    if p^.kanan <> nil then
           p := p^.kanan
        else
         begin
       p^.kanan := baru;
           p :=  nil
     end;
     end;
    end;
  end;
end;
Procedure inorder (root : btree);
begin
    if root <> nil then
    begin
        inorder (root^.kiri);
        write (root^.isi :4);
        inorder (root^.kanan);
    end;
end;
Procedure preorder (root : btree);
begin
    if root <> nil then
    begin
        write (root^.isi :4);
        preorder (root^.kiri);
        preorder (root^.kanan);
    end;
end;

procedure postorder (root : btree);
begin
  if root <> nil then
   begin
     postorder (root^.kiri);
     postorder (root^.kanan);
     write (root^.isi : 4);
   end;
end;

begin {Program Utama}
clrscr;
write('masukkan data(x = selesai) : ');readln(elemen);
repeat
    new (baru);
    baru^.isi := elemen;
    sisip (root,baru);
write('masukkan data (x = selesai : ');readln(elemen);
until elemen = 'x';
writeln;
writeln('hasil dari penelusuran pohon biner :');
write('1. Inorder   : ');inorder (root); writeln;
write('2. Postorder : ');postorder (root); writeln;
write('3. Preorder  : ');preorder (root); writeln;
readln;

end.

CONTOH PROGRAM TREE DALAM PASCAL



Program Tree1;
uses crt;
Type
Tree = ^Simpul;
Simpul = Record
Info : char;
Kiri : Tree;
Kanan : Tree;
End;

Function BARU(Hrf : Char) : Tree;
Var Temp : Tree;
Begin
New(Temp);
Temp^.Info := Hrf;
Temp^.Kiri := NIL; Temp^.Kanan := NIL;
BARU := Temp;
End;

Procedure MASUK(Var Pohon : Tree; Hrf : Char);
Begin
If Pohon = NIL Then
Pohon := BARU(Hrf)
Else
Begin
If Pohon^.Info > Hrf then
MASUK(Pohon^.Kiri,Hrf)
Else If Pohon^.Info < Hrf then
MASUK(Pohon^.Kanan,Hrf)
Else
Writeln('Karakter', Hrf, 'Sudah ada di Tree');
End;
End;

Procedure PREORDER(Temp : Tree);
Begin
If Temp <> NIL Then
Begin
Write(Temp^.Info,' ');
PREORDER(Temp^.Kiri);
PREORDER(Temp^.Kanan);
End;
End;

Procedure INORDER(Temp : Tree);
Begin
If Temp <> NIL Then
Begin
INORDER(Temp^.Kiri);
Write(Temp^.Info,' ');
INORDER(Temp^.Kanan);
End;
End;

Procedure POSTORDER(Temp : Tree);
Begin
If Temp <> NIL Then
Begin
POSTORDER(Temp^.Kiri); {Kunjungi cabang kiri}
POSTORDER(Temp^.Kanan); {Kunjungi cabang kanan}
Write(Temp^.Info,' '); {Cetak isi simpul}
End;
End;

var
poon:tree;
Begin
clrscr;

MASUK(poon,'b');
MASUK(poon,'c');
MASUK(poon,'u');
MASUK(poon,'e');
MASUK(poon,'a');
writeln('PERORDER:');
PREORDER(poon);
writeln;
writeln('INORDER:');
INORDER(poon);
writeln;
writeln('POSTORDER:');
POSTORDER(poon);
readln;
end.


Trimakasih .
Jangan lupa comment di bawah :)