数据结构实验报告之二叉树遍历与叶子结点

这篇实验报告详细介绍了如何使用C语言实现二叉树的先序、中序、后序、层次遍历以及计算二叉树的节点数和叶子节点数。报告中给出了创建二叉树、遍历算法的实现代码,并通过具体实例演示了算法的正确性。在实验过程中,作者遇到了参数传递和返回值设置的问题,并逐一解决了这些错误。

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

                   成绩:  

学生姓名:     学号:       时间:

  1. 实验名称:
  1. 实现二叉树的各种遍历算法
  2. 求二叉树中的结点个数,叶子结点个数,某结点的层次
  1. 实验内容:

(1)编写一个程序实现二叉树的先序遍历,中序遍历,后序遍历的递归和非递归算法,以及层次遍历的算法,并对所给二叉树b给出求解结果

(2)编写一个程序实现以下功能,并对所给二叉树进行验证

    1. 输出二叉树的结点个数
    2. 输出二叉树的叶子结点个数
    3. 求二叉树中指定结点值的结点的层次
  1. 算法及UML图:

算法:

实验一:

void  CreatTree(BTnode *&b,char *str)  //创建二叉树

{

   BTnode *St[Maxsize],*p;

   int to=-1,k,j=0;

   char ch;

   b=NULL;     //初始时二叉链为空

   ch=str[j];

   while(ch!='\0')

   {

    switch(ch)

    {

    case '(':to++;St[to]=p;k=1;break;

    case ')':to--;break;

    case ',':k=2;break;

    default:p=(BTnode *)malloc(sizeof(BTnode));

       p->data=ch;

       p->lchild=p->rchild=NULL;

       if(b==NULL)

           b=p;

       else

       {

           switch(k)

           {

           case 1:St[to]->lchild=p;break;

           case 2:St[to]->rchild=p;break;

           }

       }

    }

    j++;

    ch=str[j];

   }

}

void DispBTree(BTnode *b)    //输出二叉树

{

   if(b!=NULL)

   {

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

    if(b->lchild!=NULL ||b->rchild!=NULL)

    {

       printf("(");

       DispBTree(b->lchild);

       if(b->rchild!=NULL) printf(",");

       DispBTree(b->rchild);

       printf(")");

    }

   }

}

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);

   }

}

void Postorder(BTnode *b)   //后序遍历

{

   if(b!=NULL)

   {

    Postorder(b->lchild);

    Postorder(b->rchild);

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

   }

}

void Preorder1(BTnode* b)

{

   BTnode * s[Maxsize],*p;

   int top=-1;

   if(b!=NULL)

   {

    top++;

    s[top]=b;

    while(top>-1)

    {

       p=s[top];

       top--;

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

       if(p->rchild!=NULL)

       {

           top++;

           s[top]=p->rchild;

       }

       if(p->lchild!=NULL)

       {

           top++;

           s[top]=p->lchild;

       }

    }

   }

}

void Inorder1(BTnode* b)

{

   BTnode *s[Maxsize],*p;

   int top=-1;

   if(b!=NULL)

   {

    p=b;

    while(top>-1||p!=NULL)

    {

       while(p!=NULL)

       {

           top++;

           s[top]=p;

           p=p->lchild;

       }

       if(top>-1)

       {

           p=s[top];

           top--;

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

           p=p->rchild;

       }

    }

   }

}

void Postorder1(BTnode* b)

{

   BTnode* s[Maxsize],*p;

   int flag,top=-1;

   if(b!=NULL)

   {

    do

    {

       while(b!=NULL)

       {

           top++;

           s[top]=b;

           b=b->lchild;

       }

       p=NULL;

       flag=1;

       while(top!=-1&&flag)

       {

           b=s[top];

           if(b->rchild==p)

           {

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

              top--;

               p=b;

           }

           else

           {

              b=b->rchild;

              flag=0;

           }

       }

    }while(top!=-1);

   }

}

void all(BTnode* b)

{

BTnode *qu[Maxsize];

   BTnode *q;

   int front,rear;

   front=rear=-1;

   rear++;

   qu[rear]=b;

   if(q->lchild!=NULL)

    {

       rear++;

       qu[rear]=q->lchild;

    }

    if(q->rchild!=NULL)

    {

       rear++;

       qu[rear]=q->rchild;

    }

   }

}

实验二:

int Nodes(BTnode *b)//结点个数

{

    if(b==NULL)

    return 0;

    else

    return Nodes(b->lchild)+Nodes(b->rchild)+1;

}

int Leaves(BTnode *b)//叶子结点个数

{

    static int cont=0;

    if(b!=NULL)

    {

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

            cont++;

        Leaves(b->lchild);

        Leaves(b->rchild);

    }

    return cont;

}

int Level(BTnode *b,char x,int h=1)   //求指定结点值的结点的层次

{

   int l;

   if(b==NULL)

    return(0);

   else if(b->data==x)

    return(h);

   else

   {

    l=Level(b->lchild,x,h+1);

    if(l!=0)

       return(l);

    else

       return(Level(b->rchild,x,h+1));

   }

}

  1. 实验程序:

实验一:#include <stdio.h>

#include<stdlib.h>  

#include<malloc.h>

#define Maxsize 100

typedef struct node   //链结点类型BTnode的声明

{

   char data;

   struct node *lchild;

   struct node *rchild;

} BTnode;

void  CreatTree(BTnode *&b,char *str)  //创建二叉树

{

   BTnode *St[Maxsize],*p;

   int to=-1,k,j=0;

   char ch;

   b=NULL;     //初始时二叉链为空

   ch=str[j];

   while(ch!='\0')

   {

    switch(ch)

    {

    case '(':to++;St[to]=p;k=1;break;

    case ')':to--;break;

    case ',':k=2;break;

    default:p=(BTnode *)malloc(sizeof(BTnode));

       p->data=ch;

       p->lchild=p->rchild=NULL;

       if(b==NULL)

           b=p;

       else

       {

           switch(k)

           {

           case 1:St[to]->lchild=p;break;

           case 2:St[to]->rchild=p;break;

           }

       }

    }

    j++;

    ch=str[j];

   }

}

void DispBTree(BTnode *b)    //输出二叉树

{

   if(b!=NULL)

   {

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

    if(b->lchild!=NULL ||b->rchild!=NULL)

    {

       printf("(");

       DispBTree(b->lchild);

       if(b->rchild!=NULL) printf(",");

       DispBTree(b->rchild);

       printf(")");

    }

   }

}

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);

   }

}

void Postorder(BTnode *b)   //后序遍历

{

   if(b!=NULL)

   {

    Postorder(b->lchild);

    Postorder(b->rchild);

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

   }

}

void Preorder1(BTnode* b)

{

   BTnode * s[Maxsize],*p;

   int top=-1;

   if(b!=NULL)

   {

    top++;

    s[top]=b;

    while(top>-1)

    {

       p=s[top];

       top--;

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

       if(p->rchild!=NULL)

       {

           top++;

           s[top]=p->rchild;

       }

       if(p->lchild!=NULL)

       {

           top++;

           s[top]=p->lchild;

       }

    }

   }

}

void Inorder1(BTnode* b)

{

   BTnode *s[Maxsize],*p;

   int top=-1;

   if(b!=NULL)

   {

    p=b;

    while(top>-1||p!=NULL)

    {

       while(p!=NULL)

       {

           top++;

           s[top]=p;

           p=p->lchild;

       }

       if(top>-1)

       {

           p=s[top];

           top--;

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

           p=p->rchild;

       }

    }

   }

}

void Postorder1(BTnode* b)

{

   BTnode* s[Maxsize],*p;

   int flag,top=-1;

   if(b!=NULL)

   {

    do

    {

       while(b!=NULL)

       {

           top++;

           s[top]=b;

           b=b->lchild;

       }

       p=NULL;

       flag=1;

       while(top!=-1&&flag)

       {

           b=s[top];

           if(b->rchild==p)

           {

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

              top--;

               p=b;

           }

           else

           {

              b=b->rchild;

              flag=0;

           }

       }

    }while(top!=-1);

   }

}

void all(BTnode* b)

{

   BTnode *q;

  BTnode *qu[Maxsize];

   int front,rear;

   front=rear=-1;

   rear++;

   qu[rear]=b;

   while(front!=rear)

   {

    front++;

    q=qu[front];

printf(“%c”,q->data);

   

   if(q->lchild!=NULL)

    {

       rear++;

       qu[rear]=q->lchild;

    }

    if(q->rchild!=NULL)

    {

       rear++;

       qu[rear]=q->rchild;

    }

   }

}

int main()

{

    BTnode* T;

   printf("请输入一个二叉树:\n");

   char str[]="A(B(D,E(H(J,K(L,M(,N))),)),C(F,G(,I)))";

   CreatTree(T,str);

    DispBTree(T);

   printf("\n");

    printf("Tree Create OK!\n");

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

    Preorder(T);

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

    Inorder(T);

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

    Postorder(T);

   printf("\n");

   printf("非先序遍历:\n");

    Preorder1(T);

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

    Inorder1(T);

    printf("\n非后序遍历:\n");

    Postorder1(T);

   printf("\n");

   printf("层次遍历\n");

   all(T);

   printf("\n");

    return 0;

}

实验二:#include <stdio.h>

#include<stdlib.h>  

#include<malloc.h>

#define Maxsize 100

typedef struct node   //链结点类型BTnode的声明

{

   char data;

   struct node *lchild;

   struct node *rchild;

} BTnode;

void  CreatTree(BTnode *&b,char *str)  //创建二叉树

{

   BTnode *St[Maxsize],*p;

   int to=-1,k,j=0;

   char ch;

   b=NULL;     //初始时二叉链为空

   ch=str[j];

   while(ch!='\0')

   {

    switch(ch)

    {

    case '(':to++;St[to]=p;k=1;break;

    case ')':to--;break;

    case ',':k=2;break;

    default:p=(BTnode *)malloc(sizeof(BTnode));

       p->data=ch;

       p->lchild=p->rchild=NULL;

       if(b==NULL)

           b=p;

       else

       {

           switch(k)

           {

           case 1:St[to]->lchild=p;break;

           case 2:St[to]->rchild=p;break;

           }

       }

    }

    j++;

    ch=str[j];

   }

}

void DispBTree(BTnode *b)    //输出二叉树

{

   if(b!=NULL)

   {

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

    if(b->lchild!=NULL ||b->rchild!=NULL)

    {

       printf("(");

       DispBTree(b->lchild);

       if(b->rchild!=NULL) printf(",");

       DispBTree(b->rchild);

       printf(")");

    }

   }

}

int Nodes(BTnode *b)//结点个数

{

    if(b==NULL)

    return 0;

    else

    return Nodes(b->lchild)+Nodes(b->rchild)+1;

}

int Leaves(BTnode *b)//叶子结点个数

{

    static int cont=0;

    if(b!=NULL)

    {

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

            cont++;

        Leaves(b->lchild);

        Leaves(b->rchild);

    }

    return cont;

}

int Level(BTnode *b,char x,int h=1)   //求指定结点值的结点的层次

{

   int l;

   if(b==NULL)

    return(0);

   else if(b->data==x)

    return(h);

   else

   {

    l=Level(b->lchild,x,h+1);

    if(l!=0)

       return(l);

    else

       return(Level(b->rchild,x,h+1));

   }

}

int main()

    BTnode* T;

   int h;

   char x;

   printf("请输入一个二叉树:\n");

   char str[]="A(B(D,E(H(J,K(L,M(,N))),)),C(F,G(,I)))";

   CreatTree(T,str);

   DispBTree(T);

   printf("\n");

   int nodes=Nodes(T);

   printf("T的结点个数为:%d\n",nodes);

   int leaves=Leaves(T);

   printf("T的叶子结点个数为:%d\n",leaves);

   printf("请输入指定的结点值:");

   scanf("%c",&x);

   h=Level(T,x,1);

   if(h==0)

    printf("T中不存在%c结点\n",x);

   else

    printf("在T中%c结点的层次为%d\n",x,h);

   return 0;

}

  1. 实验结果(请截图表示或手写):

(1)                                  

(2)

6.实验总结

  1. 实验中遇到了什么问题及其解决过程。

创建二叉树的函数一直都标识错误,一是因为单词拼写上下不一致,二是参数传递上下不一致,对参数传递的类型有点不清楚。还有就是运行的时候,运行框弹出来一下子就不见了,就进入了调试的界面,在网上查询了一下后,发现时先前的程序没有关闭,影响后面的程序运行,需要把前面的程序关闭。在计算叶子结点的时候,把返回值写成了return 0;导致一直出现的结果是0,返回去查看后把0改成cont(记录叶子结点的变量值)。

  1. 实验中产生的错误及其分析原因(写出执行语句不成功的时候系统报告的错误信息。然后分析错误原因,并给出解决办法。

'Push' : cannot convert parameter 2 from 'struct node *' to 'struct node &'

       A reference that is not to 'const' cannot be bound to a non-lvalue

解决:Sqstack *st   -->Sqstack st

'Pop' : cannot convert parameter 2 from 'struct node *' to 'struct node &'

       A reference that is not to 'const' cannot be bound to a non-lvalue

解决:改变形参的类型

'CreatTree' : undeclared identifier

解决:改正单词拼写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值