Friday, July 30, 2010

Binary Search Tree pada Pascal

program treea;
uses crt;

type
tipeinfo=char;
tree=^simpul;
simpul = record
   info : tipeinfo;
   kiri,kanan : tree;
end;

var kata:string;
    pohon : tree;
    i: byte;


procedure inisialisasi(var pohon: tree);
begin
new(pohon);
  pohon:=nil;
end;


procedure insertdata(var pohon:tree;data:tipeinfo);
var baru :tree;
begin
  new(baru);
  baru^.info:=data;
  baru^.kiri:=nil;
  baru^.kanan:=nil;
  if(pohon=nil) then pohon:= baru
  else if (data <>
  else insertdata(pohon^.kanan,data);
end;

procedure prefix(pohon :tree);
begin

if pohon<>nil then begin
  write(pohon^.info);
  prefix(pohon^.kiri);
  prefix(pohon^.kanan); end;
end;
procedure infix(pohon :tree);
begin
if pohon<>nil then begin
  infix(pohon^.kiri);
  write(pohon^.info);
  infix(pohon^.kanan);  end;
end;

procedure posfix(pohon :tree);
begin
if pohon<> nil then begin
  posfix(pohon^.kiri);
  posfix(pohon^.kanan);
  write(pohon^.info);    end;
end;

begin
write('masukkan kata');
readln(kata);inisialisasi(pohon);
for i:=1 to length(kata) do
 insertdata(pohon,kata[i]);
writeln('prefix');
prefix(pohon);
writeln;
writeln('infix');
infix(pohon);
writeln;
writeln('posfix');
posfix(pohon);
writeln;
readln;
end.

No comments: