二叉树的基本操作(小系统)

验证数据:
ab##cd###

具体代码如下

#include <stdio.h>
#include<stdlib.h>

typedef struct node{
    char data;
    struct node *rchild,*lchild;
}BiTNode,*BiTtree;


int count=0;
int leafcount=0;
int depth=0;
void CreateTree(BiTNode **root)
{

    char data;
    scanf("%c",&data);
    if(data == '#')
    {
        (*root)=NULL;
    }

    else
    {
        *root = (BiTNode *)malloc(sizeof(BiTNode));
        if((*root) == NULL)
        {
            printf("分配空间失败!\n");
            exit(0);
        }
        (*root)->data = data;
        CreateTree(&((*root)->lchild));
        CreateTree(&((*root)->rchild));
    }

}

//先序
void preBiTree(BiTtree root){
    if(root!=NULL){
        printf("%c",root->data);
        preBiTree(root->lchild);
        preBiTree(root->rchild);
    }
}
//中序
void InOrder(BiTtree root){
    if(root!=NULL){
        InOrder(root->lchild);   /*中序遍历左子树*/
        printf("%c",root->data); /*输出根节点*/
        InOrder(root->rchild);   /*中序遍历右子树*/
    }
}
//后序
void PostOrder(BiTtree root){
    if(root!=NULL){
        PostOrder(root->lchild);
        PostOrder(root->rchild);
        printf("%c",root->data);
    }
}

//二叉树的复制
BiTtree CopyBTree(BiTtree root){
    BiTtree p,lchild,rchild;
    if(root==NULL)
        return NULL;
    lchild=CopyBTree(root->lchild);
    rchild=CopyBTree(root->rchild);
    p=(BiTtree)malloc(sizeof(BiTNode));
    p->data=root->data;
    p->lchild=root->lchild;
    p->rchild=root->rchild;
    return p;
}

//统计二叉树节点总数
void CountAll(BiTtree root){
//    else
//        return CountAll(root->lchild)+CountAll(root->rchild)+1;
//}
    if(root){
        count++;
        CountAll(root->lchild);
        CountAll(root->rchild);
    }
}
//叶子节点总数
int LeafCount(BiTtree root){
    if(root==NULL)
        return 0;
    if(root->lchild==NULL&&root->rchild==NULL)
        return 1;
    else{
        return LeafCount(root->lchild)+LeafCount(root->rchild);
    }
}
//叶子结点数(法二)
void Leaf(BiTtree root){
    if(root)
    {
        if(root->lchild==NULL&&root->rchild==NULL)leafcount++;
        Leaf(root->lchild);
        Leaf(root->rchild);
        
    }
}

//高度
int PostTreeDepth(BiTtree bt){
    int hl,hr,max;
    if(bt!=NULL){
        hl=PostTreeDepth(bt->lchild);
        hr=PostTreeDepth(bt->rchild);
        max=hl>hr?hl:hr;
        return(max+1);
    }
    return 0;
}
//层次最大值求树的深度
void PreTreeDepth2(BiTtree bt,int h){
    if(bt!=NULL){
        if(h>depth) depth=h;
        PreTreeDepth2(bt->lchild, h+1);
        PreTreeDepth2(bt->rchild, h+1);
    }
}



int main(int argc, const char * argv[]) {
    int sele;

    BiTNode bt;
    BiTtree root=&bt;
    while(1){
        printf("\n1:建立二叉树\n");
        printf("2:前序输出二叉树\n");
        printf("3:中序输出二叉树\n");
        printf("4:后序输出二叉树\n");
        printf("5:二叉树节点总数\n");
        printf("6:二叉树叶子结点数\n");
        printf("7:求二叉树的高度递归算法\n");
        printf("8:层次最大值求二叉树的深度\n");
        printf("0:退出\n");
        scanf("%d",&sele);
        switch (sele) {
            case 1:
                CreateTree(&root);
                printf("创建成功!");
                break;
            case 2:
                preBiTree(root);
                break;
            case 3:
                InOrder(root);
                break;
            case 4:
                PostOrder(root);
                break;
            case 5:
                CountAll(root);
                printf("节点总数为:%d\n", count-2);
                break;
            case 6:
               printf("叶子结点数为:%d\n",LeafCount(root)-1);
                break;
            case 7:
                printf("二叉树高度为:%d\n",PostTreeDepth(root)-1);
                break;
            case 8:
                PreTreeDepth2(root,1);
                printf("二叉树高度为:%d\n",depth-1);
                break;
            default:
                exit(0);
        }
    }
    return 0;
}


//void CreateBiTree(BiTtree *root){//指向指针类型的指针
//    char ch;
//    scanf("%c",&ch);
// //   ch=getchar();
//    if(ch=='@'){
//        (*root)=NULL;
//    }
//    else{
//        *root=(BiTtree)malloc(sizeof(BiTNode));
//        (*root)->data=ch;
//
//        CreateBiTree(&(*root)->lchild);
//        CreateBiTree(&(*root)->rchild);
//
//    }
//}


//先序创建二叉树
//int CreateBiTree(BiTtree *root){
//    char ch;
//    ch=getchar();
//    if(ch=='@') root=NULL;
//    else{
//        *root=(BiTtree)malloc(sizeof(BiTNode));
//        if(!*root) exit(0);
//        (*root)->data=ch;
//        CreateBiTree(&(*root)->lchild);
//        CreateBiTree(&(*root)->rchild);
//    }
//    return 1;
//}

至于为啥测试数据最后
回车后为什么加两个##
我突然想到是不是程序把回车符当作了二叉树的一个结点,需要给出他它的左子树,右子树呢,那这样的话他的深度,结点数,都会发生变化,结点数应该多一个,为什么最后结果是共6个结点,但是之前不做主函数里的switch却可以正常输出?
所以我只能在主函数里面控制
二叉树的基本操作(小系统)
但是没在主函数里面修改count-2时候结果如下
二叉树的基本操作(小系统)

困惑了好多天的问题一直没有解决。。

上一篇:根据二叉树的遍历序列建立二叉树


下一篇:线索二叉树