扩展二叉树(树)

Description

由于先序、中序和后序中任一个都不能确定一棵二叉树,所以对二叉树做如下处理,将二叉树的空结点用.补齐,如图所示。我们把这样处理后的二叉树称为原二叉树的扩展二叉树,扩展二叉树的先序和后序序列能唯一确定其二叉树。 
现给出扩展二叉树的先序序列,要求出其中中序和后序序列。

Input

 

Output

 

Sample Input

 

ABD..EF..G..C..

 

Sample Output

 

DBFEGAC 
DFGEBCA


解题思路:先用指针变量和线性链表建立一个二叉树,然后用递归分别输出中序序列和后续序列即可。


程序:
type
  tree=^node;
  node=record
         a:char;
         lchild,rchild:tree;
       end;
var
  s:string;
  i:longint;
  t:tree;

procedure build(var t:tree);
  begin
    inc(i);
    if s[i]<>'.' then
      begin
        new(t);
        t^.a:=s[i];
        build(t^.lchild);
        build(t^.rchild);
      end
      else t:=nil;
end;

procedure printzx(t:tree);
  begin
    if t<>nil then
      begin
        printzx(t^.lchild);
        write(t^.a);
        printzx(t^.rchild);
      end;
end;

procedure printhx(t:tree);
  begin
    if t<>nil then
      begin
        printhx(t^.lchild);
        printhx(t^.rchild);
        write(t^.a);
      end;
end;

begin
  readln(s);
  i:=0;
  build(t);
  printzx(t);
  writeln;
  printhx(t);
end. 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值