二叉树操作实验报告

XX学院实验报告

 

五、实验总结和思考:(填写收获和体会,分析成功或失败的原因)

收获:只有通过不断练习,才能获取经验,避免下次犯同样的错误!

成功和失败: 在实验过程中,遇到了一些细节性的问题,不好寻找,导致结果错误。

结果:找到了问题所在,发现了一些平时不曾注意的问题。

附件:(源代码)

/*          说明

 1、树的元素类型为字符形;

 2、实现对二叉树的先序遍历操作;

 3、实现对二叉树的中序遍历操作;

4、实现对二叉树的后序遍历操作;

 5、求二叉树的深度;

 6、求二叉树的叶结点数;

 7、将二叉树中所有结点的左、右子树相互交换;

 */

#include "stdio.h"

#include "conio.h"

#define  OVERFLOW   -1

typedef char  telemtype;

typedef struct bitnode

{

    telemtype data;

    struct bitnode *lchild,*rchild;

    }bitnode,*bitree;

void createbitree(bitree *t)

{

    telemtype ch;

    scanf("%c",&ch);

    if(ch==' ') *t=NULL;

    else

    {

        *t=(bitnode *)malloc(sizeof(bitnode));

        if(!(*t)) exit(OVERFLOW);

        (*t)->data=ch;

        createbitree(&((*t)->lchild));

        createbitree(&((*t)->rchild));

    }

    return;

}

void preordertraverse(bitree t)

{

    if(t)

    {

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

        preordertraverse(t->lchild);

        preordertraverse(t->rchild);

    }

}

void inordertraverse(bitree t)

{

    if(t)

    {

        inordertraverse(t->lchild);

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

        inordertraverse(t->rchild);

    }

}

void postordertraverse(bitree t)

{

    if(t)

    {

        postordertraverse(t->lchild);

        postordertraverse(t->rchild);

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

    }

}

int height(bitree t)

{

    if(t==NULL) return 0;

    else   return((height(t->lchild))>(height(t->rchild))?((height(t->lchild))+1):((height(t->rchild))+1));

}

int leafnum(bitree t)

{

    if(t==NULL) return 0;

    else{

         if((t->lchild==NULL)&&(t->rchild==NULL)) return 1;

         else return(leafnum(t->lchild)+leafnum(t->rchild));

         }

}

void changelr(bitree *t)

{

    bitnode *p;

    if(*t)

    {

        p=(*t)->lchild;(*t)->lchild=(*t)->rchild;(*t)->rchild=p;

        changelr(&((*t)->lchild));

        changelr(&((*t)->rchild));

        }

    return ;

}

int menu()

{

   printf("\t\t\t**********************************************\n");

   printf("\t\t\t*1.create the tree                           *\n");

   printf("\t\t\t*2.preorder traverse the tree                *\n");

   printf("\t\t\t*3.inorder traver the tree                   *\n");

   printf("\t\t\t*4.post traver the tree                      *\n");

   printf("\t\t\t*5.the depth of the tree                     *\n");

   printf("\t\t\t*6.the leafs of the tree                     *\n");

   printf("\t\t\t*7.change the rchild and lchild of the tree  *\n");

   printf("\t\t\t*8.help                                      *\n");

   printf("\t\t\t*9.exit                                      *\n");

   printf("\t\t\t**********************************************\n");

   printf("\n");

   printf("please input your choice:");

    }

main()

{   int a,i;

    bitree t;

    while(1){

    menu();

    scanf("%d",&a);

    switch(a)

    {

        case 1: printf("please input the elem of the tree in preorder:\n");createbitree(&t);printf("\n");break;

        case 2: printf("\n");preordertraverse(t);printf("\n");break;

        case 3: printf("\n");inordertraverse(t);printf("\n");break;

        case 4: printf("\n");postordertraverse(t);printf("\n");break;

        case 5: printf("\n the depth of the tree is:");i=height(t);printf("%d\n",&i);break;

        case 6: printf("\n the leafs of the tree is:");i=leafnum(t);printf("%d\n",&i);break;

        case 7: printf("\n the tree after change the left and right is:"); preordertraverse(t);printf("\n");break;

        case 8: printf("help");break;

        case 9: exit(0);

        }

    }

    getch();

}  

相关推荐