• 【例3-5】扩展二叉树


    http://ybt.ssoier.cn:8088/problem_show.php?pid=1340

    写法一:结构体指针

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 struct node {//树结点
     4     char data;//存放值
     5     node* lchild;//左儿子地址 注意数据类型为node *
     6     node* rchild;//右儿子地址 注意数据类型为node *
     7 };
     8 void create(node* &root) {
     9     char c;
    10     scanf("%c", &c);
    11     if (c == '.') root=NULL;
    12     else {
    13         root = new node;//新建树结点
    14         root->data = c; //先存根数据
    15         create(root->lchild);//左子树
    16         create(root->rchild);//右子树
    17     }
    18 
    19 }
    20 void inorder(node* root) {//中序输出树
    21     if (root == NULL) return;
    22     inorder(root->lchild);
    23     printf("%c", root->data);
    24     inorder(root->rchild);
    25 }
    26 void porder(node* root) {//后序输出树
    27     if (root == NULL) return;
    28     porder(root->lchild);
    29     porder(root->rchild);
    30     printf("%c", root->data);
    31 }
    32 int main() {
    33     node* btnode = new node;//新建树根结点
    34     create(btnode);//创建树
    35     inorder(btnode);//先序输出
    36     printf("
    ");
    37     porder(btnode);//后序输出
    38     return 0;
    39 }
    View Code

    写法二:结构体指针typedef

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 struct Node;
     4 typedef Node *tree;
     5 struct Node {
     6     char data;
     7     tree lchild, rchild;
     8 };
     9 tree bt;
    10 int i = -1;
    11 string s;
    12 void build(tree &bt) //建树
    13 {
    14     if (s[++i] != '.') {
    15         bt = new Node;
    16         bt->data = s[i];
    17         build(bt->lchild);
    18         build(bt->rchild);
    19     } else
    20         bt = NULL;
    21 }
    22 void inorder(tree bt) //输出中序序列
    23 {
    24     if (bt) {
    25         inorder(bt->lchild);
    26         cout << bt->data;
    27         inorder(bt->rchild);
    28     }
    29 }
    30 void postorder(tree bt) //输出后序序列
    31 {
    32     if (bt) {
    33         postorder(bt->lchild);
    34         postorder(bt->rchild);
    35         cout << bt->data;
    36     }
    37 }
    38 int main() {
    39     cin >> s;
    40     build(bt);
    41     inorder(bt);
    42     cout << endl;
    43     postorder(bt);
    44     cout << endl;
    45     return 0;
    46 }
    View Code

     写法三:结构体数组实现树

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 struct  node{
     4     char data;
     5     int lchild, rchild;
     6 };
     7 node tree[1000];
     8 int  num=1;
     9 void build(int root)
    10 {
    11     char c;
    12     cin>>c;
    13     if(c!='.'){
    14         tree[root].data=c;//给根赋值 
    15         tree[root].lchild=++num;//左儿子地址赋值 
    16         build(tree[root].lchild);//进入左儿子 
    17         tree[root].rchild=++num;//右儿子地址赋值 
    18         build(tree[root].rchild);//进入右儿子 
    19     }
    20     else{
    21         tree[root].data='.';//便于后面输出时判断 
    22     }
    23 }
    24 void inorder(int root){//中序遍历 
    25     if(tree[root].data!='.'){
    26         inorder(tree[root].lchild);//1.先访问左儿子 
    27         cout<<tree[root].data;// 2.访问根并输出根的值 
    28         inorder(tree[root].rchild);//3.访问右儿子 
    29     }
    30 }
    31 void postorder(int root){//后序遍历 
    32     if(tree[root].data!='.'){
    33         postorder(tree[root].lchild);//1.先访问左儿子
    34         postorder(tree[root].rchild);//2.访问右儿子 
    35         cout<<tree[root].data;//3.访问根并输出根的值
    36     }
    37 }
    38 int main()
    39 {
    40     build(1);//结构体数组以下标1的数据为根 
    41     //测试代码 
    42 //    for(int i=1; i<=15; i++){
    43 //        cout<<i<<" "<<tree[i].data<<" "<<tree[i].lchild<<" "<<tree[i].rchild<<endl;
    44 //    }
    45     inorder(1);
    46     cout<<endl;
    47     postorder(1);
    48     return 0;
    49 }
  • 相关阅读:
    简单的签到日历
    CSS样式命名整理
    Vue 数组中更新属性值后,视图不更新,等待其他元素更新后会触发的解决办法
    linux之cp/scp命令+scp命令详解
    js 进制转换
    博主QQ
    JSP的介绍及语法详解
    通过jdbc连接MySql数据库的增删改查操作
    在JAVA中自定义连接数据库的工具类
    通过sql语句对MySql数据库的基本操作
  • 原文地址:https://www.cnblogs.com/tflsnoi/p/14093229.html
Copyright © 2020-2023  润新知