zoukankan      html  css  js  c++  java
  • 扩展二叉树(信息学奥赛一本通 1340)

    【问题描述】

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

    【输入样例】

    tree_b.in ABD..EF..G..C..

    【输出样例】

    tree_b.out DBFEGAC DFGEBCA


    【方法一】数组模拟

    【方法二】指针

     1 #include <iostream>
     2 #include <stdlib.h>
     3 #include <string>
     4 #include <cstring>
     5 #include <algorithm>
     6 #include <cstdio>
     7 using namespace std;
     8 
     9 typedef struct node;
    10 typedef node *tree;
    11 struct node
    12 {
    13     char data;
    14     tree lchild, rchild;
    15 };
    16 tree bt;
    17 
    18 int i;
    19 string s;
    20 
    21 void build(tree &bt)  //建树
    22 {
    23     if(s[++i]!='.')
    24     {
    25         bt = new node; 
    26         bt = new node; 
    27         bt->data = s[i];
    28         build(bt->lchild);  
    29         build(bt->rchild);  
    30     }
    31     else bt = NULL;
    32 }
    33 void printzx(tree bt)  //输出中序序列 
    34 {
    35     if(bt)
    36     {
    37         printzx(bt->lchild);
    38         cout << bt->data;
    39         printzx(bt->rchild);
    40     }
    41 }
    42 void printhx(tree bt)  //输出后序序列 
    43 {
    44     if(bt)
    45     {
    46         printhx(bt->lchild);
    47         printhx(bt->rchild);
    48         cout << bt->data;
    49     }
    50 }
    51 int main()
    52 {
    53     cin >> s;
    54     i = -1;
    55     build(bt);
    56     printzx(bt);
    57     cout << endl;
    58     printhx(bt);
    59     cout << endl;
    60     return 0;
    61 }
  • 相关阅读:
    多级导航Menu的CSS
    Centos7在线安装PostgreSQL和PostGIS
    PostGis 根据经纬度查询两点之间距离
    在PowerDesigner中表显示中添加Code的显示
    Tomcat部署Geoserver
    PostGIS之路AddGeometryColumn函数添加一个几何类型字段
    怎样把多个excel文件合并成一个
    Error:java: 无效的目标发行版: 11
    PowerDesigner导出Excel
    GeoServer发布高清电子地图
  • 原文地址:https://www.cnblogs.com/ljy-endl/p/11260494.html
Copyright © 2011-2022 走看看