二叉树的创建及其相关操作

这是一个实现二叉树各种操作的程序,包括创建、显示、遍历(先序、中序、后序、层次)、计算叶子数、节点数以及树的深度。此外,它还包含了一个数据结构实验演示系统的主菜单,涵盖线性表、栈、队列、串、二叉树、图、查找和排序等多个模块。
摘要由CSDN通过智能技术生成

#include <stdio.h>
#define MAXLEN 100
typedef struct BT                //二叉树结构体
{
    char data;
    BT* lchild;
    BT*rchild;
}BT;

BT *CreateTree();
void ShowTree(BT *T);
void Preorder(BT *T);
void Postorder(BT *T);
void Levelorder(BT *T);
void Inorder(BT *T);
void Leafnum(BT *T);
void Nodenum(BT *T);
int TreeDepth(BT *T);

int count=0;                    //计算结点数的变量

void BTree()
{
    BT *T=NULL;
    char ch1,ch2,a;
    ch1='y';
    while(ch1=='y'||ch1=='Y')
    {
        printf("\n\n\n\n");
        printf("\n\t\t               二叉树子系统");
        printf("\n\t\t*****************************************");
        printf("\n\t\t*           1------建 二 叉 树          *");
        printf("\n\t\t*           2------凹 入 显 示          *");
        printf("\n\t\t*           3------先 序 遍 历          *");
        printf("\n\t\t*           4------中 序 遍 历          *");
        printf("\n\t\t*           5------后 序 遍 历          *");
        printf("\n\t\t*           6------层 次 遍 历          *");
        printf("\n\t\t*           7------求 叶 子 数          *");
        printf("\n\t\t*           8------求 结 点 数          *");
        printf("\n\t\t*           9------求 树 深 度          *");
        printf("\n\t\t*           0------返       回          *");
        printf("\n\t\t*****************************************");
        printf("\n\t\t请选择菜单号(0--9):");
        scanf("%c",&ch2);
        getchar();
        printf("\n");
        switch(ch2)
        {
        case '1':
            printf("\n\t\t请输入按先序建立二叉树的结点序列:");
            printf("\n\t\t说明:'0'代表后继结点为空,请逐个输入,按回车输入下一结点。");
            printf("\n\t\t请输入根结点:");
            T=CreateTree();
            printf("\n\t\t二叉树成功建立!\n");break;
        case '2':
            ShowTree(T);break;
        case '3':
            printf("\n\t\t该二叉树的先序遍历序列为:");
            Preorder(T);break;
        case '4':
            printf("\n\t\t该二叉树的中序遍历序列为:");
            Inorder(T);break;
        case '5':
            printf("\n\t\t该二叉树的后序遍历序列为:");
            Postorder(T);break;
        case '6':
            printf("\n\t\t该二叉树的层次遍历序列为:");
            Levelorder(T);break;
        case '7':
            count=0;Leafnum(T);
            printf("\n\t\t该二叉树有%d个叶子。\n",count);break;
        case '8':
            count=0;Nodenum(T);
            printf("\n\t\t该二叉树总共有%d个结点。\n",count);break;
        case '9':
            printf("\n\t\t该树的深度是:%d",TreeDepth(T));break;
        case '0':
            ch1='n';break;
        default:
            printf("\n\t\t***请注意:输入有误!***");
        }
        if(ch2!='0')
        {
            printf("\n\n\t\t按回车键继续,按任意键返回主菜单!\n");
            a=getchar();
            if(a!='\xA')
            {
                getchar();ch1='n';
            }
        }
    }
}

BT *CreateTree()            //建立二叉树
{
    BT *t;
    char x;
    scanf("%c",&x);
    getchar();
    if(x=='0')
        t=NULL;
    else
    {
        t=new BT;
        t->data=x;
        printf("\n\t\t请输入%c结点的左子结点:",t->data);
        t->lchild=CreateTree();
        printf("\n\t\t请输入%c结点的右子结点:",t->data);
        t->rchild=CreateTree();
    }
    return t;
}

void Preorder(BT *T)    //先序遍历
{
    if(T)
    {
        printf("%3c",T->data);
        Preorder(T->lchild);
        Preorder(T->rchild);
    }
}

void Inorder(BT *T)        //中序遍历
{
    if(T)
    {
        Inorder(T->lchild);
        printf("%3c",T->data);
        Inorder(T->rchild);
    }
}

void Postorder(BT *T)    //后序遍历
{
    if(T)
    {
        Postorder(T->lchild);
        Postorder(T->rchild);
        printf("%3c",T->data);
    }
}

void Levelorder(BT *T)    //层次遍历
{
    int i,j;
    BT *q[100],*p;
    p=T;
    if(p!=NULL)
    {
        i=1;q[i]=p;j=2;
    }
    while(i!=j)
    {
        p=q[i];printf("%3c",p->data);
        if(p->lchild!=NULL)
        {
            q[j]=p->lchild;j++;}
            if(p->rchild!=NULL)
            {
                q[j]=p->rchild;j++;
            }
            i++;
    }
}

void Leafnum(BT *T)        //求叶子数
{
    if(T)
    {
        if(T->lchild==NULL&&T->rchild==NULL)
            count++;
        Leafnum(T->lchild);
        Leafnum(T->rchild);
    }
}

void Nodenum(BT *T)        //求结点数
{
    if(T)
    {
        count++;
        Nodenum(T->lchild);
        Nodenum(T->rchild);
    }
}

int TreeDepth(BT *T)    //求树深度
{
    int ldep,rdep;
    if(T==NULL)
        return 0;
    else
    {
        ldep=TreeDepth(T->lchild);
        rdep=TreeDepth(T->rchild);
        if(ldep>rdep)
            return ldep+1;
        else
            return rdep+1;
    }
}

void ShowTree(BT *T)    //凹入法显示二叉树
{
    BT *stack[MAXLEN],*p;
    int level[MAXLEN][2],top,n,i,width=4;
    if(T!=NULL)
    {
        printf("\n\t\t凹入表示法:\n\t\t");
        top=1;
        stack[top]=T;
        level[top][0]=width;
        while(top>0)
        {
            p=stack[top];
            n=level[top][0];
            for(i=1;i<=n;i++)
                printf(" ");
            printf("%c",p->data);
            for(i=n+1;i<50;i+=2)
                printf("▃");
            printf("\n\t\t");
            top--;
            if(p->rchild!=NULL)
            {
                top++;
                stack[top]=p->rchild;
                level[top][0]=n+width;
                level[top][1]=2;
            }
            if(p->lchild!=NULL)
            {
                top++;
                stack[top]=p->lchild;
                level[top][0]=n+width;
                level[top][1]=1;
            }
        }
    }
}
#include "线性表.h"
#include "栈.h"
#include "队列.h"
#include "串.h"
#include "树.h"
#include "图.h"
#include "查找.h"
#include "排序.h"

void main(void)
{
    int choice;
    char ch;
    ch='y';
    while(ch=='y'||ch=='Y')
    {
        printf("\n\n\n\n\t\t   数 据 结 构 实 验 演 示 系 统\n\n\n");
        printf("\t\t                 主 菜 单");
        printf("\n\t\t*************************************");
        printf("\n\t\t*          1-------线 性 表         *");
        printf("\n\t\t*          2-------   栈            *");
        printf("\n\t\t*          3-------队    列         *");
        printf("\n\t\t*          4-------   串            *");
        printf("\n\t\t*          5-------二 叉 树         *");
        printf("\n\t\t*          6-------   图            *");
        printf("\n\t\t*          7-------查    找         *");
        printf("\n\t\t*          8-------排    序         *");
        printf("\n\t\t*          0-------退    出         *");
        printf("\n\t\t*************************************");
        
        printf("\n\n\t\t请选择菜单号(0--8):");
        scanf("%d",&choice);
        getchar();
        switch(choice)
        {
        case 1:LineList();;break;
        case 2:Stack();;break;
        case 3:Queue();;break;
        case 4:String();;break;
        case 5:BTree();;break;
        case 6:Graph();;break;
        case 7:Search();;break;
        case 8:Sort();;break;
        case 0:ch='n';break;
        default : printf("菜单选择错误!请重输");
        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值