二叉树的创建及基本操作(三种递归遍历算法和非递归中序遍历算法,统计二叉树的叶子结点及求二叉树的深度)

二叉树的创建及基本操作
内容:
1.利用二叉树的二叉链表存储方法按照先序遍历序列创建二叉树,实现三种递归遍历算法。

2.编写统计二叉树的叶子结点及求二叉树深度的算法(可用递归方法)。

3.实现二叉树的非递归中序遍历算法。

二叉链表存储表示:

typedef struct BiTNode{

  TElemType  data;

  struct BiTNode  *lchild, *rchild;

} BiTNode,*BiTree;

算法设计及程序源代码

  1. 二叉树的创建算法(按先序遍历序列创建):
BiTree creatbitree(){

BiTree T;

TElemType ch;

scanf("%c",&ch);

if(ch=='#') T=NULL;//#代表空格字符

else{

T=(BiTNode*)malloc(sizeof(BiTNode));

T->data=ch;//生成根结点

T->lchild=creatbitree();//生成左子树

T->rchild=creatbitree();//生成右子树

}

return T;    }

2.统计二叉树叶子结点个数的算法:

int countleaf(BiTree T,int count) {

   if(T){

      if((T->lchild==NULL)&&(T->rchild==NULL))

      count++;

      count=countleaf(T->lchild,count);

      count=countleaf(T->rchild,count);}

   return count;}   

3.求二叉树深度的算法:

int bitreedepth(BiTree T) {

   int leftdepth,rightdepth,maxdepth;

   if(T!=NULL){

   leftdepth=bitreedepth(T->lchild) ;//左子树深度

   rightdepth=bitreedepth(T->rchild) ;//右子树深度

   maxdepth=leftdepth>rightdepth?leftdepth:rightdepth;//取最大深度

   return maxdepth+1;}

   else return 0; }

4.先序、中序、后序递归遍历算法:

先序:


void preorder(BiTree T) {

   if(T!=NULL){

      printf("%c ",T->data);

      preorder(T->lchild);

      preorder(T->rchild);}

   }

中序:

void inorder(BiTree T){

   if(T!=NULL){

   inorder(T->lchild)   ;

   printf("%c ",T->data);

   inorder(T->rchild);}

 }

后序:

void postorder(BiTree T){

   if(T!=NULL){

      postorder(T->lchild);

      postorder(T->rchild);

      printf("%c ",T->data);}

 }

5.中序非递归遍历算法:

Status inorder(BiTree T,Status (*visit)(TElemType e)){

   SqStack s;

   initstack(s);

   BiTree p=T;

   while(p||!StackEmpty(s)){

   if(p){

      push(s,p); //根指针进栈

      p=p->lchild;}//遍历左子树

   else{

      pop(s,p);//根指针出栈,访问根结点

      if(visit(p->data)) return 0;

      p=p->rchild;}//遍历右子树

      }

return OK;}

6.程序源代码:

#include<stdio.h>

#include<stdlib.h>

#define ERROR 0;

#define TElemType char

#define STACK_INIT_SIZE 100

#define STACKINCREMENT 10

#define OK 1

#define Status int

//二叉链表存储表示

typedef struct BiTNode{

   TElemType data;

   struct BiTNode *lchild,*rchild;//左右孩子指针

}BiTNode, *BiTree;

#define SElemType BiTree //栈的数据类型

//定义栈

typedef struct {

SElemType *base;

SElemType *top;

int stacksize;

}SqStack;

//构造空栈

int initstack (SqStack &s){

s.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));

if(!s.base)

exit(0);

s.top=s.base;

s.stacksize=STACK_INIT_SIZE;

return 0;}

//判断是否为空

Status StackEmpty(SqStack S){

    if(S.top==S.base) return 1;

    return 0;}

//先序遍历创建二叉树

BiTree creatbitree(){

   BiTree T;

   TElemType ch;

   scanf("%c",&ch);

   if(ch=='#') T=NULL;//#代表空格字符

   else{

      T=(BiTNode*)malloc(sizeof(BiTNode));

      T->data=ch;//生成根结点

      T->lchild=creatbitree();//生成左子树

      T->rchild=creatbitree();//生成右子树

   }

   return T;    }

//入栈

Status push(SqStack &s, SElemType e) {

if((s.top-s.base)>=s.stacksize){

s.base=(SElemType*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(int));

        if(!(s.base))

      exit(1);

      s.top=s.base+s.stacksize;

      s.stacksize+=STACKINCREMENT;

      }

      *s.top++=e;

      return 0; }

//出栈

Status pop(SqStack &s,SElemType &e){

     if(s.top==s.base)

     return 0;

     e=*--s.top;

     return OK;}

//先序遍历

void preorder(BiTree T) {

   if(T!=NULL){

      printf("%c ",T->data);

      preorder(T->lchild);

      preorder(T->rchild);}

   }

//中序递归遍历

void inorder(BiTree T){

   if(T!=NULL){

   inorder(T->lchild)   ;

   printf("%c ",T->data);

   inorder(T->rchild);}

 }

//后序遍历

void postorder(BiTree T){

   if(T!=NULL){

      postorder(T->lchild);

      postorder(T->rchild);

      printf("%c ",T->data);}

 }

//中序非递归遍历的

Status inorder(BiTree T,Status (*visit)(TElemType e)){

   SqStack s;

   initstack(s);

   BiTree p=T;

   while(p||!StackEmpty(s)){

   if(p){

      push(s,p); //根指针进栈

      p=p->lchild;}//遍历左子树

   else{

      pop(s,p);//根指针出栈,访问根结点

      if(visit(p->data)) return 0;

      p=p->rchild;}//遍历右子树

      }

return OK;}

Status PrintElement(TElemType e){

    printf("%c ",e);                            

    return 0;}

//统计二叉树叶子结点

int countleaf(BiTree T,int count) {

   if(T){

      if((T->lchild==NULL)&&(T->rchild==NULL))

      count++;

      count=countleaf(T->lchild,count);

      count=countleaf(T->rchild,count);}

   return count;}

//求二叉树深度

int bitreedepth(BiTree T) {

   int leftdepth,rightdepth,maxdepth;

   if(T!=NULL){

   leftdepth=bitreedepth(T->lchild) ;//左子树深度

   rightdepth=bitreedepth(T->rchild) ;//右子树深度

   maxdepth=leftdepth>rightdepth?leftdepth:rightdepth;//取最大深度

   return maxdepth+1;}

   else return 0; }

int main(){

   BiTree T;

   int count=0;

   printf("请按照先序遍历输入二叉树(#代表空子树):\n");

   T=creatbitree();

   printf("二叉树创建成功!\n");

   printf("先序遍历二叉树:\n");

   preorder(T) ;

   printf("\n中序递归遍历二叉树:\n") ;

   inorder(T);

   printf("\n后序遍历二叉树:\n");

   postorder(T);

   printf("\n中序非递归遍历:\n");

   inorder(T,PrintElement);

   printf("\n二叉树的叶子结点为:%d",countleaf(T,count));

   printf("\n二叉树的深度为:%d",bitreedepth(T));

   return 0;}

运行结果截图

后面是要创建的二叉树
在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值