[NEFU锐格 数据结构]实验三四 二叉树常见的操作
Posted 鱼竿钓鱼干
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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;
}
以上是关于[NEFU锐格 数据结构]实验三四 二叉树常见的操作的主要内容,如果未能解决你的问题,请参考以下文章