我写了一个,大家看看:用类Pascal语言
procedure CreatTree(s:string; var bt:biTree);
new(bt);
i:=1;
if (s[i]='#') //string end with '#'
then [
bt:=nil;
return;
]
bt^.data:=s[i];
Ini_Stack(stack); //initial a stack
push(stack,(bt,0)); //关键,栈元素为(ptr,m)
while not empty(stack) do //其中m=0: ptr没有孩子
[ (pre,m):=pop(stack); m=1: ptr有一个孩子
i:=i+1;
if (s[i]='#') return;
if (s[i]='$')
then [push(stack,(pre,m+1));
continue;
];
if IsChar(s[i])
then [new(p); p^.data:=s[i];
if (m=0)
then [push(stack,(pre,m+1);
pre^.lchild:=p;
];
if (m=1)
then pre^.rchild:=p;
push(stack,(p,0);
];
writeln("input error!");
return;
] //while
endp;