输入广义表形式,输出二叉树?~
来源:百度知道 编辑:UC知道 时间:2024/05/04 05:08:34
RT~
怎么完成啊~各位大大留个QQ啥的。。。
例如:
(A(B(D,E),C(,F)))
怎么输出成 A
/ \
B C
/ \ \
D E F
我用的是C++。高人留个QQ嘛,我有些问题还不会呢。
怎么完成啊~各位大大留个QQ啥的。。。
例如:
(A(B(D,E),C(,F)))
怎么输出成 A
/ \
B C
/ \ \
D E F
我用的是C++。高人留个QQ嘛,我有些问题还不会呢。
program ltree;
const n=8;
type trlist=^node;
node=record
da:char;
l,r:trlist;
end;
var s:array[1..n] of trlist;
p,root:trlist;
ch:char;
top,k:integer;
procedure creat(var head:trlist);
begin
read(ch);
top:=0;
while ch<>'#' do
begin
case ch of
'A'..'Z':begin new(p);p^.da:=ch;p^.l:=nil;p^.r:=nil;
if top<>0 then
case k of
1:s[top]^.l:=p;
2:s[top]^.r:=p;
end
end;
'(':begin top:=top+1;s[top]:=p;k:=1;end;
')': top:=top-1;
',': k:=2;
end;
read(ch);
end;
head:=s[1];
end;
留个邮箱地址,我把源代码发给你,里面只有几个小错误。