tree.pas

来自「广仪表转二插树 广仪表转二插树」· PAS 代码 · 共 69 行

PAS
69 行
字号
program test;
type
  pot=^rec;
  rec=record
    d:char;
    l,r:pot;
  end;
var
  h:pot;
procedure init;
var
  a:array[1..100] of pot;
  ch:char;
  i,k,t:integer;
  p:pot;
begin
  read(ch);
  i:=0;
  t:=0;
  while ch<>'@' do
  begin
    case ch of
      'a'..'z':begin
                 new(p); p^.d:=ch; p^.l:=nil; p^.r:=nil;
                 if i<>0 then
                   case k of
                     1:a[i]^.l:=p;
                     2:a[i]^.r:=p;
                   end;
               end;
      '(':begin
            i:=i+1; a[i]:=p; k:=2;
            if i>t then t:=i;
          end;
      ')':i:=i-1;
      ',':k:=1;
    end;
    read(ch);
  end;
  h:=a[1];
  writeln(t+1);
end;
procedure print(p:pot);
begin
  write(p^.d,' ');
  if p^.l<>nil then print(p^.l);
  if p^.r<>nil then print(p^.r);
end;
procedure print1(p:pot);
begin
  if p^.l<>nil then print1(p^.l);
  write(p^.d,' ');
  if p^.r<>nil then print1(p^.r);
end;
procedure print2(p:pot);
begin
  if p^.l<>nil then print2(p^.l);
  if p^.r<>nil then print2(p^.r);
  write(p^.d,' ');
end;
begin
  init;
  print(h);
  writeln;
  print1(h);
  writeln;
  print2(h);
end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?