锐格————-二叉树

东北林业锐格实验————二叉树

7080

在这里插入图片描述
孩子兄弟表示法.
算叶子结点个数

#include <bits/stdc++.h>

using namespace std;

typedef struct node{
char data;
node* lchild,* rchlid;
}node,*tree;

void create(tree &t)
{   char ch;
    cin>>ch;
    if(ch=='@')
        t=NULL;
    else
    {   t=new node;
        t->data=ch;
    create(t->lchild);
    create(t->rchlid);}
}


int leaf(tree &t)
{
    if(t==NULL)
        return 0;
    if (t->lchild == NULL)
    {return 1+leaf(t->rchlid);}
    else
        return leaf(t->lchild)+leaf(t->rchlid);
}

int main()
{   tree t=new node;
    create(t);
    cout << leaf(t);
    return 0;
}

7079

在这里插入图片描述

#include <bits/stdc++.h>

using namespace std;


typedef struct node{
char data;
node* lchild,* rchlid;
}node,*tree;

void create(tree &t)
{   char ch;
    cin>>ch;
    if(ch=='@')
        t=NULL;
    else
    {   t=new node;
        t->data=ch;
    create(t->lchild);
    create(t->rchlid);}
}

void midout(tree &t)
{
    if(t)
    {
        midout(t->lchild);
        cout<<t->data;
        midout(t->rchlid);
    }


}

int main()
{   tree t=new node;
  char data[10000];
    create(t);
    midout(t);
    return 0;
}

7078

在这里插入图片描述
和上个题一样,代码直接复制粘贴都可以的

7077

和上面的不一样,重点看一下

在这里插入图片描述

#include <bits/stdc++.h>

using namespace std;
#define  Maxsize 10000

typedef struct node{
char data;
node* lchild,* rchlid;
}node,*tree;

void create(tree &t)
{   char ch;
    cin>>ch;
    if(ch=='@')
        t=NULL;
    else
    {   t=new node;
        t->data=ch;
    create(t->lchild);
    create(t->rchlid);}
}


int main()
{   tree t=new node;
  char data[10000];
    create(t);

    queue<tree> q;
    q.push(t);
    while(!q.empty())
    {
        tree temp;
        temp=q.front();
        q.pop();
        cout<<temp->data;
        if(temp->lchild)
            q.push(temp->lchild);
        if(temp->rchlid)
            q.push(temp->rchlid);


    }
    return 0;
}

7076


为什么用之前那个就不行啊,是有什么差别吗,

#include <bits/stdc++.h>

using namespace std;
#define  Maxsize 10000

typedef struct node{
char data;
node* lchild,* rchlid;
}node,*tree;

void create(tree &t)
{   char ch;
    cin>>ch;
    if(ch=='@')
        t=NULL;
    else
    {   t=new node;
        t->data=ch;
    create(t->lchild);
    create(t->rchlid);}
}
void leafnum(tree &t,int &num)
{
    if(t)
   {
    if (t->lchild==NULL&&t->rchlid==NULL)
    num++;
    else{
        leafnum(t->lchild,num);
        leafnum(t->rchlid,num);
        }
   }
}

int main()
{   tree t=new node;
int num=0;
    create(t);
    leafnum(t,num);
    cout<<num;
    return 0;
}

7075

在这里插入图片描述

#include <bits/stdc++.h>

using namespace std;
#define  Maxsize 10000

typedef struct node{
char data;
node* lchild,* rchlid;
}node,*tree;

void create(tree &t)
{   char ch;
    cin>>ch;
    if(ch=='@')
        t=NULL;
    else
    {   t=new node;
        t->data=ch;
    create(t->lchild);
    create(t->rchlid);}
}
void backout(tree &t)
{
    if(t)
  {   backout(t->lchild);
      backout(t->rchlid);
      cout<<t->data;

       }
}

int main()
{   tree t=new node;
    create(t);
    backout(t);
    return 0;
}

7074

在这里插入图片描述

#include <bits/stdc++.h>

using namespace std;
#define  Maxsize 10000

typedef struct node{
char data;
node* lchild,* rchlid;
}node,*tree;

void create(tree &t)
{   char ch;
    cin>>ch;
    if(ch=='@')
        t=NULL;
    else
    {   t=new node;
        t->data=ch;
    create(t->lchild);
    create(t->rchlid);}
}




int depth(tree t)
{
    int m=0,n=0;
    if(t==NULL)
        return 0;

    else{
        m=depth(t->lchild);
        n=depth(t->rchlid);
        if(m>n)
            return m+1;
        else
            return n+1;
    }
}

int main()
{   tree t=new node;
    create(t);
    cout<<depth(t);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值