[NEFU锐格 数据结构]实验三四 二叉树常见的操作

[NEFU锐格 数据结构]实验三四 二叉树常见的操作

推荐阅读:[数据结构]NEFU 大二上 锐格实验参考 目录

知识点

题目知识点
7080森林,孩子兄弟
7079栈,非递归中序遍历
7078递归中序遍历
7077队列,层序遍历
7076统计叶子节点
7075二叉树后续遍历
7074二叉树深度

题目

7080

可以看看这篇博客

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>
#include<iostream>
using namespace std;

typedef struct BinNode{
    char data;
    struct BinNode *left;
    struct BinNode *right;
}BinNode,*BinTree;

void CreateBinTree(BinTree &T){
    char ch;cin>>ch;
    if(ch=='@')T=NULL;
    else{
        T=new BinNode;
        T->data=ch;
        CreateBinTree(T->left);
        CreateBinTree(T->right);
    }
}


int CountLeaf(BinTree T){
    int res=0;
    if(T==NULL)return 0;
    if(T->left==NULL)return 1+CountLeaf(T->right);
    else return CountLeaf(T->left)+CountLeaf(T->right);
}
int main(){
    BinTree T;
    CreateBinTree(T);
    printf("%d",CountLeaf(T));
    return 0;
}

7079

利用栈非递归中序遍历,讲起来比较复杂,建议看这篇博客讲的很好

#include<bits/stdc++.h>
using namespace std;

typedef struct BinNode{
    char data;
    struct BinNode *left,*right;
}BinNode,*BinTree;
#define MAXSIZE 1024
typedef struct {
    BinTree data[MAXSIZE];
    int top;
}SeqStack;

bool StackInit(SeqStack &S){
    S.top=-1;return 1;
}
bool StackEmpty(SeqStack S){
    if(S.top==-1)return 1;
    return 0;
}
bool StackPush(SeqStack &S,BinTree x){
    if(S.top==MAXSIZE-1){
        puts("栈满");
        return 0;
    }
    S.top++;S.data[S.top]=x;
    return 1;
}
bool StackPop(SeqStack &S,BinTree &x){
    if(S.top==-1){puts("栈空");return 0;}
    x=S.data[S.top];S.top--;
    return 0;
}
bool StackGetTop(SeqStack S,BinTree &x){
    if(S.top==-1){puts("栈空");return 0;}
    x=S.data[S.top];
    return 1;
}
void CreateBinTree(BinTree &T){
    char ch;cin>>ch;
    if(ch=='@')T=NULL;
    else{
        T=new BinNode;
        T->data=ch;
        CreateBinTree(T->left);
        CreateBinTree(T->right);
    }
}
void InOrder(BinTree T){
    SeqStack Stk;StackInit(Stk);
    do{
        while(T){
            StackPush(Stk,T); 
            //cout<<"PUSH "<<T->data<<endl;
            T=T->left;
        }
        if(!StackEmpty(Stk)){
            StackPop(Stk,T);
            cout<<T->data;
            T=T->right;
        }
    }while(!StackEmpty(Stk)||T);
}
int main(){
    BinTree T;T=new BinNode;
    CreateBinTree(T);
    InOrder(T);
    return 0;
}

7078

中序遍历,中间输出根节点(其实就是输出语句位置嘛)

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>
#include<iostream>
using namespace std;

typedef struct BinNode{
    char data;
    struct BinNode *left;
    struct BinNode *right;
}BinNode,*BinTree;

void CreateBinTree(BinTree &T){
    char ch;cin>>ch;
    if(ch=='@')T=NULL;
    else{
        T=new BinNode;
        T->data=ch;
        CreateBinTree(T->left);
        CreateBinTree(T->right);
    }
}

void InOrder(BinTree T){
    if(T){
        InOrder(T->left);
        printf("%c",T->data);
        InOrder(T->right);
    }
}

int main(){
    BinTree T;
    CreateBinTree(T);
    InOrder(T);
    return 0;
}

7077

层序遍历二叉树,把队列和二叉树的结构和API写好,然后写个树上BFS(广度优先遍历)即可

#include<bits/stdc++.h>
using namespace std;

typedef struct BinNode{
    char data;
    struct BinNode *left;
    struct BinNode *right;
}BinNode,*BinTree;
#define MAXSIZE 1024
typedef struct{
    BinTree data[MAXSIZE];
    int front,rear;
}SeQueue;
void QueueInit(SeQueue &Q){
    Q.front=0;Q.rear=0;
}
void QueuePush(SeQueue &Q,BinTree x){
    Q.data[Q.rear]=x;
    Q.rear=(Q.rear+1)%MAXSIZE;
}
void GetHead(SeQueue Q,BinTree &x){
    x=Q.data[Q.front];
}
bool QueueEmpty(SeQueue Q){
    if(Q.front==Q.rear)return 1;
    return 0;
}
void QueuePop(SeQueue &Q,BinTree &x){
    x=Q.data[Q.front];
    Q.front=(Q.front+1)%MAXSIZE;
}

void CreateBinTree(BinTree &T){
    char ch;cin>>ch;
    if(ch=='@')T=NULL;
    else{
        T=new BinNode;
        T->data=ch;
        CreateBinTree(T->left);
        CreateBinTree(T->right);
    }
}

void BfsTree(BinTree T){
    SeQueue Q;QueueInit(Q);
    QueuePush(Q,T);
    while(!QueueEmpty(Q)){
        BinTree output;
        output=new BinNode;
        QueuePop(Q,output);
        cout<<output->data;
        if(output->left!=NULL)QueuePush(Q,output->left);
        if(output->right!=NULL)QueuePush(Q,output->right);
    }
}

int main(){
    BinTree T;
    CreateBinTree(T);
    BfsTree(T);
    return 0;
}

7076

遍历统计叶子节点,叶子节点特点是左右子树均为NULL

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>
#include<iostream>
using namespace std;

typedef struct BinNode{
    char data;
    struct BinNode *left;
    struct BinNode *right;
}BinNode,*BinTree;

void CreateBinTree(BinTree &T){
    char ch;cin>>ch;
    if(ch=='@')T=NULL;
    else{
        T=new BinNode;
        T->data=ch;
        CreateBinTree(T->left);
        CreateBinTree(T->right);
    }
}

int CountLeaf(BinTree T){
    int res=0;
    if(T==NULL)return 0;
    else{
        if(T->left==NULL&&T->right==NULL)return 1;
        else return CountLeaf(T->left)+CountLeaf(T->right);
    }
}
int main(){
    BinTree T;
    CreateBinTree(T);
    printf("%d",CountLeaf(T));
    return 0;
}

7075

后序遍历,输出根节点放最后

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>
#include<iostream>
using namespace std;

typedef struct BinNode{
    char data;
    struct BinNode *left;
    struct BinNode *right;
}BinNode,*BinTree;

void CreateBinTree(BinTree &T){
    char ch;cin>>ch;
    if(ch=='@')T=NULL;
    else{
        T=new BinNode;
        T->data=ch;
        CreateBinTree(T->left);
        CreateBinTree(T->right);
    }
}

void BackOrder(BinTree T){
    if(T){
        BackOrder(T->left);
        BackOrder(T->right);
        printf("%c",T->data);
    }
}

int main(){
    BinTree T;
    CreateBinTree(T);
    BackOrder(T);
    return 0;
}

7074

求二叉树深度,递归左右子树取最大

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>
#include<iostream>
using namespace std;

typedef struct BinNode{
    char data;
    struct BinNode *left;
    struct BinNode *right;
}BinNode,*BinTree;

void CreateBinTree(BinTree &T){
    char ch;cin>>ch;
    if(ch=='@')T=NULL;
    else{
        T=new BinNode;
        T->data=ch;
        CreateBinTree(T->left);
        CreateBinTree(T->right);
    }
}

int GetDepth(BinTree T){
    int LeftDepth=0,RightDepth=0;
    if(T==NULL)return 0;
    else{
        LeftDepth=GetDepth(T->left);
        RightDepth=GetDepth(T->right);
        if(LeftDepth>RightDepth)return LeftDepth+1;
        else return RightDepth+1;
    }
}
int main(){
    BinTree T;
    CreateBinTree(T);
    printf("%d\n",GetDepth(T));
    return 0;
}
  • 10
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值