• 数据结构实验之 二叉树的建立与遍历


    题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2136&cid=1184

    自己重新写的:

    下面的代码如果建树 不想要返回值的话,也可以这些写 void build(tree *&p) { },这样用引用可以自动 改变L和R的指向。

    如果不加引用的话,就要用返回值了。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <cmath>
     6 #include <queue>
     7 #include <vector>
     8 #include <algorithm>
     9 const int maxn = 100 + 10;
    10 using namespace std;
    11 int n, num, cnt;
    12 char s[maxn];
    13 typedef struct node
    14 {
    15     char data;
    16     struct node *l, *r;
    17 }tree;
    18 
    19 tree *build(tree *p)
    20 {
    21     if(num>=n || s[num]==',')
    22         return NULL;
    23     p = new tree;
    24     p->data = s[num];
    25     num ++;
    26     p->l = build(p->l);
    27     num ++;
    28     p->r = build(p->r);
    29     return p;
    30 }
    31 void inorder(tree *p)
    32 {
    33     if(p)
    34     {
    35         inorder(p->l);
    36         cout<<p->data;
    37         inorder(p->r);
    38     }
    39 }
    40 void postorder(tree *p)
    41 {
    42     if(p)
    43     {
    44         postorder(p->l);
    45         postorder(p->r);
    46         cout<<p->data;
    47     }
    48 }
    49 void leaf(tree *p)
    50 {
    51    if(p)
    52    {
    53       if(p->l==NULL && p->r==NULL)
    54       {
    55          cnt++;
    56          return;
    57       }
    58       leaf(p->l);
    59       leaf(p->r);
    60    }
    61 }
    62 int deep(tree *p)
    63 {
    64     if(p)
    65     {
    66        int a, b;
    67        a = deep(p->l); b = deep(p->r);
    68        return a>b?a+1:b+1;
    69     }
    70     else
    71      return 0;
    72 }
    73 
    74 int main()
    75 {
    76     while(cin>>s)
    77     {
    78         n = strlen(s);
    79         num = cnt = 0;
    80         tree *head, *p;
    81         head = build(p);
    82 
    83         inorder(head);
    84         cout<<endl;
    85         postorder(head);
    86         cout<<endl;
    87         leaf(head);
    88         cout<<cnt<<endl;
    89         cout<<deep(head)<<endl;
    90     }
    91     return 0;
    92 }
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<stdlib.h>
     5 #include<queue>
     6 
     7 using namespace std;
     8 
     9 int sum=0;
    10 struct tree *p;
    11 struct tree
    12 {
    13     char data;
    14     struct tree *l,*r;
    15 };
    16 
    17 struct tree* build(struct tree *p)//建树
    18 {
    19     char c;
    20     c=getchar();
    21     p=(struct tree*)malloc(sizeof(struct tree));
    22 
    23     if(c==',')
    24         p=NULL;
    25     else
    26     {
    27         p->data=c;
    28         p->l=build(p->l);
    29         p->r=build(p->r);
    30     }
    31     return p;
    32 };
    33 
    34 void inorder(struct tree *p)//中序遍历
    35 {
    36     if(p)
    37     {
    38         inorder(p->l);
    39         printf("%c",p->data);
    40         inorder(p->r);
    41     }
    42 };
    43 
    44 void postorder(struct tree *p)//后序遍历
    45 {
    46     if(p)
    47     {
    48         postorder(p->l);
    49         postorder(p->r);
    50         printf("%c",p->data);
    51     }
    52 };
    53 
    54 int leaf(struct tree *p)//叶子个数
    55 {
    56     if(p)
    57     {
    58         if(p->l==NULL&&p->r==NULL)
    59             sum++;
    60         leaf(p->l);
    61         leaf(p->r);
    62     }
    63     return sum;
    64 };
    65 
    66 int deep(struct tree *p)//树的深度
    67 {
    68      int left,right;
    69     if(!p) return 0;
    70     else
    71     {
    72         left=deep(p->l);
    73         right=deep(p->r);
    74         return 1+(left>right?left:right);//取左右子树深度的最大值加一返回 
    75     }
    76     return 1;
    77 };
    78 
    79 int main()
    80 {
    81     struct tree *root;
    82     root=build(p);
    83     inorder(root);
    84     printf("
    ");
    85     postorder(root);
    86     printf("
    ");
    87     sum=leaf(root);
    88     printf("%d
    ",sum);
    89     printf("%d
    ",deep(root));
    90 }

    其他操作:

    int CountNode(BiTree T) //统计二叉树的节点个数

    {

    if(!T)

    return 0;

    else

    return 1+CountNode(T->lchild)+CountNode(T->rchild);

    }

  • 相关阅读:
    第三次作业
    Mysql show variables系统变量详解
    利用正则检测文中敏感词并标红
    Android 连接大疆SDK
    Android 热修复方案Tinker
    Hander消息机制
    .net MemCache 入门到简单使用链接汇总
    用socket连接pop3服务器遇到的小问题
    我是如何取得Reflector的真实程序的
    google卫星地图的url计算
  • 原文地址:https://www.cnblogs.com/bfshm/p/3164750.html
Copyright © 2020-2023  润新知