其他分享
首页 > 其他分享> > 二叉树的应用

二叉树的应用

作者:互联网

1.树的遍历

详情:
给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

输入格式:
输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

输出格式:
在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
输出样例:
4 1 6 3 5 7 2

代码实现:

#include<malloc.h>
#include<stdio.h>
#define MaxSize 100

typedef int ElemType;
typedef struct node
{
    ElemType data;
    struct node *lchild;
    struct node * rchild;
}BTNode;



BTNode* CreateBTree(int *pre, int *in, int n)   // 根据后序和中序遍历序列确定树的结构
{
    int k;   // 记录根节点左子树的个数
    int *p;  // 指向根节点
    if (n <= 0)
        return NULL;
    BTNode *b = (BTNode*)malloc(sizeof(BTNode));
    b->data = *(pre+n-1);  // 树的根节点为后序序列的最后一个编号
    for (p = in; p < in + n; ++p)
        if (*p == *(pre+n-1))  // 在中序序列中寻找根节点的位置
            break;				// 找到后退出循环
    k = p-in;          // 记录根节点左子树节点个数
    b->lchild = CreateBTree(pre, in, k);  // 递归
    b->rchild = CreateBTree(pre+k, p+1, n-k-1);
    return b;
}
typedef struct{
	BTNode*data[MaxSize];
	int count,rear;
}SqQueue;
void InitQueue(SqQueue *&q){
	q=(SqQueue*)malloc(sizeof(SqQueue*));
	q->count=0;
	q->rear=0;
}
void Destroy(SqQueue*&q){
	free(q);
}
bool QueueEmpty(SqQueue*q){
	return q->count==0;
}
bool enQueue(SqQueue*&q,BTNode* e){
	if(q->count==MaxSize){
		return false;
	}
	q->rear=(q->rear+1)%MaxSize;
	q->data[q->rear]=e;
	q->count++;
	return true;
}
bool deQueue(SqQueue*&q,BTNode*& e){
	if(q->count==0){
		return false;
	}
	int index=(q->rear-q->count+MaxSize)%MaxSize+1;
	e=q->data[index];
	q->count--;
	return true;
}

void levelOrder(BTNode*b,int a[]){
	BTNode*p;
	SqQueue*qu;
	InitQueue(qu);
	enQueue(qu,b);
	int tt=0;
	while(!QueueEmpty(qu)){
		deQueue(qu,p);
		a[tt++]=p->data;
		//printf("%c",p->data);
		if(p->lchild!=NULL){
			enQueue(qu,p->lchild);
		}
		if(p->rchild!=NULL){
			enQueue(qu,p->rchild);
		}

	}
}

int main()
{
    int n;
    scanf("%d",&n);
    int a[100];
    int post[100],in[100];
    for(int i=0;i<n;i++)
        scanf("%d",&post[i]);
    for(int i=0;i<n;i++)
        scanf("%d",&in[i]);
    BTNode *b;
    b = CreateBTree(post,in,n);
    //TravLevel(b);
    levelOrder(b,a);
    for (int i=0;i<n-1;i++){
        printf("%d ",a[i]);
    }printf("%d",a[n-1]);
    return 0;
}

在这里插入图片描述

2.列出叶结点

详情:
对于给定的二叉树,本题要求你按从上到下、从左到右的顺序输出其所有叶节点。

输入格式:
首先第一行给出一个正整数 N(≤10),为树中结点总数。树中的结点从 0 到 N−1 编号。随后 N 行,每行给出一个对应结点左右孩子的编号。如果某个孩子不存在,则在对应位置给出 “-”。编号间以 1 个空格分隔。

输出格式:
在一行中按规定顺序输出叶节点的编号。编号间以 1 个空格分隔,行首尾不得有多余空格。

输入样例:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
4 1 5

输出样例:

代码实现:

#include <iostream>  
#include<stdio.h>  
#include<malloc.h>  
#define MaxSize 100  
using namespace std;  
  
  
typedef char ElemType;  
  
typedef struct node  
{  
    ElemType data;  
    struct node *lchild;  
    struct node * rchild;  
}BTNode;  
typedef struct{  
    BTNode*data[MaxSize];  
    int count,rear;  
}SqQueue;  
void InitQueue(SqQueue *&q){  
    q=(SqQueue*)malloc(sizeof(SqQueue*));  
    q->count=0;  
    q->rear=0;  
}  
void Destroy(SqQueue*&q){  
    free(q);  
}  
bool QueueEmpty(SqQueue*q){  
    return q->count==0;  
}  
bool enQueue(SqQueue*&q,BTNode* e){  
    if(q->count==MaxSize){  
        return false;  
    }  
    q->rear=(q->rear+1)%MaxSize;  
    q->data[q->rear]=e;  
    q->count++;  
    return true;  
}  
bool deQueue(SqQueue*&q,BTNode*& e){  
    if(q->count==0){  
        return false;  
    }  
    int index=(q->rear-q->count+MaxSize)%MaxSize+1;  
    e=q->data[index];  
    q->count--;  
    return true;  
}  
void levelOrder(BTNode*b,char cs[]){  
    BTNode*p;  
    SqQueue*qu;  
    InitQueue(qu);  
    enQueue(qu,b);  
    int t=0;  
    while(!QueueEmpty(qu)){  
        deQueue(qu,p);  
        if(p->lchild!=NULL){  
            enQueue(qu,p->lchild);  
        }  
        if(p->rchild!=NULL){  
            enQueue(qu,p->rchild);  
        }  
        if(p->lchild==NULL &&p->rchild==NULL){  
            cs[t]=p->data;  
            t++;  
        }  
  
    }  
    Destroy(qu);  
}  
void PreOrder(BTNode*b){  
    if(b!=NULL){  
        printf("%c ",b->data);  
        PreOrder(b->lchild);  
        PreOrder(b->rchild);  
    }  
}  
void InOrder(BTNode*b){  
    if(b!=NULL){  
  
        InOrder(b->lchild);  
        printf("%c",b->data);  
        InOrder(b->rchild);  
    }  
}  
int func(char c[][2],int n){  
  
    for(int i=0;i<n;i++){  
        bool flag=false;  
  
        for(int j=0;j<n;j++){  
            if(c[j][0]==(char)(i+48)){  
                flag=true;  
                break;  
            }  
            else if(c[j][1]==(char)(i+48)){  
                flag=true;  
                break;  
            }  
  
        }  
  
        if(flag==false){  
            return i;  
        }  
  
    }return -1;  
  
}  
  
void CreateBT(BTNode*b,char c[][2],int root,int &k)  
{  
    if(c[root][0]=='-'&&c[root][1]=='-'){  
        b->lchild=NULL;b->rchild=NULL;  
        k++;  
        return ;  
  
    }  
    if(c[root][0]!='-'){  
        b->lchild=(BTNode*)malloc(sizeof(BTNode));  
        b->lchild->data=c[root][0];  
        CreateBT(b->lchild,c,(int)(c[root][0])-48,k);  
    }else{  
        b->lchild=NULL;  
    }  
    if(c[root][1]!='-'){  
        b->rchild=(BTNode*)malloc(sizeof(BTNode));  
        b->rchild->data=c[root][1];  
        CreateBT(b->rchild,c,(int)(c[root][1])-48,k);  
    }else{  
        b->rchild=NULL;  
    }  
}  
  
int main()  
{  
    BTNode*b;  
    b=(BTNode*)malloc(sizeof(BTNode));  
    int n;cin>>n;  
    char c[n][2];  
    for (int i=0;i<n;i++){  
        cin>>c[i][0]>>c[i][1];  
    }  
    int k;  
    int ff=func(c,n);  
    b->data=(char)(ff+48);  
    CreateBT(b,c,ff,k);  
    char cs[10];  
    levelOrder(b,cs);  
    for(int j=0;j<k-1;j++)  
    {  
        cout<<cs[j]<<" ";  
    }  
    cout<<cs[k-1];  
    return 0;  
}  

在这里插入图片描述

3.二叉树的遍历

详情:
根据输入构造二叉树,输出该二叉树的先序序列。二叉树共有N个节点,节点编号是1到N。约定1号节点是根节点。

输入格式:
第一行输入整数N。 接下来有N行,依次给出1到N节点的左孩子和右孩子。对于这N行中的每一行,有两个整数。第i(i=1, 2, …, N)行中,第一个整数指出左孩子的编号,第二个整数指出右孩子的编号。如果整数值为0,表示没有左孩子或右孩子。

输出格式:
输出一行,内容是二叉树的先序序列。节点编号之间用空格隔开,行末有1个空格。

输入样例:

6
2 5
3 4
0 0
0 0
0 6
0 0

输出样例:

1 2 3 4 5 6 

代码实现:

#include <iostream>
#include<stdio.h>
#include<malloc.h>
using namespace std;


typedef int ElemType;

typedef struct node
{
    ElemType data;
    struct node *lchild;
    struct node * rchild;
}BTNode;


void CreateBT(BTNode*b,int c[][2],int root)
{
    if(c[root][0]==0&&c[root][1]==0){
        b->lchild=NULL;b->rchild=NULL;

        return ;
    }
    if(c[root][0]!=0){
        b->lchild=(BTNode*)malloc(sizeof(BTNode));
        b->lchild->data=c[root][0];
        CreateBT(b->lchild,c,c[root][0]);
    }else{
    	b->lchild=NULL;
	}
    if(c[root][1]!=0){
        b->rchild=(BTNode*)malloc(sizeof(BTNode));
        b->rchild->data=c[root][1];
        CreateBT(b->rchild,c,c[root][1]);
    }else{
    	b->rchild=NULL;
	}


}

void PreOrder(BTNode *b)
{
    if(b!=NULL)
    {
        cout<<b->data<<" ";
        PreOrder(b->lchild);
        PreOrder(b->rchild);
    }
}
int main()
{
    BTNode*b;
    b=(BTNode*)malloc(sizeof(BTNode));
    b->data=1;
    int n;cin>>n;
    int c[n+1][2];
    for (int i=1;i<n+1;i++){
        cin>>c[i][0]>>c[i][1];
    }

    CreateBT(b,c,1);
    PreOrder(b);
    cout<<endl;
    return 0;
}

在这里插入图片描述

标签:lchild,int,root,BTNode,二叉树,应用,rchild,data
来源: https://blog.csdn.net/XST1520203418/article/details/122157699