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.
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.