数据结构课程设计(三):二叉树的构造(C语言)

本文介绍了一种使用层序遍历与中序遍历或先序遍历与中序遍历来构建二叉树的方法,并实现了二叉树的层序、先序、中序及后序遍历。通过具体的输入输出示例展示了算法的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目要求

任务:已知二叉树的层序和中序遍历序列,或已知二叉树的先序序列、中序序列,试编写算法建立该二叉树( 用递归或非递归的方法都可以)。
要求:能够输入树的各个结点,并能够输出用不同方法遍历的遍历序列;分别建立建立二叉树存储结构的的输入函数、输出层序遍历序列的函数、输出先序遍历序列的函数;

算法实现

主函数 main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "BiTree.h"
int main()
{
    int com;
    introduce();
    while(scanf("%d",&com)!=EOF)
    {
        switch(com)
        {
            case 1:CZ();break;//已知层序和中序建立二叉树;
            case 2:XZ();break;//已知先序和中序建立二叉树;
            case 3:C();break;//层序遍历
            case 4:
                printf("先序遍历结果为:\n");
                X(T);printf("\n");
                break;//先序遍历
            case 5:
                printf("中序遍历结果为:\n");
                Z(T);printf("\n");
                break;//中序遍历
            case 6:
                printf("后序遍历结果为:\n");
                H(T);printf("\n");
                break;//后序遍历
            case 7:system("cls");break;//清屏
            default :printf("输入操作错误,请重新输入\n");break;
        }
        introduce();
    }
    return 0;
}

头文件 BiTree.h

#ifndef BITREE_H_INCLUDED
#define BITREE_H_INCLUDED
typedef struct node
{
    int data;
    struct node *lchild,*rchild;
}node;

node *T;
int lev[100];//层序序列
int in[100];//中序序列
int pre[100];//先序序列
int n;//记录节点的个数

void CZ();

void XZ();

void C();

void X(node *tree);

void Z(node *tree);

void H(node *tree);

void introduce();
#endif // BITREE_H_INCLUDED

功能介绍 Introduce.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "BiTree.h"

void introduce()
{
    printf("************1表示已知层序和中序建立二叉树************\n");
    printf("************2表示已知先序和中序建立二叉树************\n");
    printf("************3表示层序遍历二叉树**********************\n");
    printf("************4表示先序遍历二叉树**********************\n");
    printf("************5表示中序遍历二叉树**********************\n");
    printf("************6表示后序遍历二叉树**********************\n");
    printf("************7表示清屏********************************\n");
    printf("************请输入您的操作:*************************\n");
}

先序中序建立二叉树 XZ.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "BiTree.h"

node *deal1(int *preOrder,int *midOrder,int n)
{
    int midPtr,leftLength,rightLength;
    node *tree=(node *)malloc(sizeof(node));
    tree->data=preOrder[0];
    tree->lchild=tree->rchild=NULL;
    if(n==1)//只剩一个结点时,直接返回
    {
        return tree;
    }
    midPtr=-1;
    while(midOrder[++midPtr]!=preOrder[0]);//找出中序遍历中根节点的位置
    leftLength=midPtr;//左子树结点数
    rightLength=n-leftLength-1;//右子树结点数
    tree->lchild=deal1(&preOrder[1],midOrder,leftLength);
    tree->rchild=deal1(&preOrder[leftLength+1],&midOrder[leftLength+1],rightLength);
    return tree;
}

void XZ()
{
    int i;
    T=(node *)malloc(sizeof(node));
    printf("请输入节点的个数:\n");
    scanf("%d",&n);
    printf("请输入先序遍历的结果:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&pre[i]);
    }
    printf("请输入中序遍历的结果:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&in[i]);
    }
    T=deal1(pre,in,n);
    printf("二叉树创建成功!!!!!!\n");
}
//节点个数
//7
//先序遍历
//1 2 4 5 3 6 7
//中序遍历
//4 2 5 1 6 3 7
//后序遍历结果
//4 5 2 6 7 3 1
//层序遍历结果
//1 2 3 4 5 6 7


层序中序建立二叉树 CZ.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "BiTree.h"

node* deal(int inL,int inR,int head)//head表示,当前子树的根
{
    if(inL==inR) return NULL;//注意inR是取不到的,开区间,即[inL,inR)
    node* T=(node *)malloc(sizeof(node));
    T->data=head;
    int i,lhead,rhead,k,u,v;
    for(i=inL;in[i]!=head&&i<inR;i++); //在中序序列找到根的位置i,i是根节点的位置也就是左子树节点的个数
    int flag1=0,flag2=0;//分别记录左根和右根是否找到
    for(k=0;k<n;k++)//从中序序列的左子树中,通过层序遍历找到左子树的根
    {
        for(u=inL;u<i;u++)
        {
            if(in[u]==lev[k])//找到左子树的根
            {
                lhead=lev[k];
                flag1=1;
                break;
            }
        }
        if(flag1==1) break;
    }

    for(k=0;k<n;k++)//从中序序列的右子树中,通过层序遍历找到右子树的根
    {
        for(v=i+1;v<inR;v++)
        {
            if(in[v]==lev[k])//找到右子树的根
            {
                rhead=lev[k];
                flag2=1;
                break;
            }
        }
        if(flag2==1) break;
    }
    T->lchild=deal(inL,i,lhead);
    T->rchild=deal(i + 1,inR,rhead);
    return T;
}
void CZ()
{
    printf("请输入结点个数:\n");
    scanf("%d",&n);
    int i;
    printf("请输入层序遍历的结果:\n");
    for(i=0;i<n;i++) scanf("%d",&lev[i]);
    printf("请输入中序遍历的结果:\n");
    for(i=0;i<n;i++) scanf("%d",&in[i]);
    T= deal(0,n,lev[0]);
    printf("二叉树创建成功!!!!!\n");
}
//输入
// 5
//层序:1 2 3 4 5
//中序:4 2 5 1 3

//输出
//后序:4 5 2 3 1
//先序:1 2 4 5 3

层序输出二叉树 C.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "BiTree.h"

struct duilie
{
    node *aaa[100];
    int rear,front;
};
typedef struct duilie duilie;

duilie duiliechushihua(duilie a)
{
    a.front=a.rear=0;
    return a;
}

int emptyy(duilie a)
{
    if(a.front==a.rear)
        return 1;
    else
        return 0;
}

void C()
{
    printf("层序输出序列为:\n");
    duilie a;
    a=duiliechushihua(a);
    node *temp;
    temp=T;
    a.aaa[a.rear]=temp;
    a.rear++;
    while(!emptyy(a))
    {
        temp=a.aaa[a.front];
        a.front++;
        printf("%d ",temp->data);
        if(temp->lchild!=NULL)
        {
            a.aaa[a.rear]=temp->lchild;
            a.rear++;
        }
        if(temp->rchild!=NULL)
        {
            a.aaa[a.rear]=temp->rchild;
            a.rear++;
        }
    }
    printf("\n");
}

先序输出二叉树 X.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "BiTree.h"

void X(node *tree)
{
    if(tree==NULL)
    return;
    else
    {
        printf("%d ",tree->data);
        X(tree->lchild);
        X(tree->rchild);
    }
}

中序输出二叉树 Z.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "BiTree.h"

void Z(node *tree)
{
    if(tree==NULL)
    return;
    else
    {
        Z(tree->lchild);
        printf("%d ",tree->data);
        Z(tree->rchild);
    }
}

后序输出二叉树 H.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "BiTree.h"

void H(node *tree)
{
    if(tree==NULL)
    return;
    else
    {
        H(tree->lchild);
        H(tree->rchild);
        printf("%d ",tree->data);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值