| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1703 人关注过本帖
标题:二叉树 前面先中后续输出正常 后面计算孩子就出错
只看楼主 加入收藏
愿o圆o安
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2017-3-13
结帖率:25%
收藏
 问题点数:0 回复次数:4 
二叉树 前面先中后续输出正常 后面计算孩子就出错
#include<stdio.h>
#include<stdlib.h>

typedef char DateType;
typedef struct Node
{
    DateType date;
    struct Node *lchild;
    struct Node *rchild;
}BiTreeNode;

void PrintDate(DateType x)
{
    printf("%c",x);
}

void CreatBiTreeNode(BiTreeNode **T)
{
    char ch;
    scanf("%c",&ch);
    if(ch==' ')
    {
        *T=NULL;
    }
    else
    {
        if(!(*T=(BiTreeNode*)malloc(sizeof(BiTreeNode))))
        {
            printf("Overflow.\n");
            exit(0);
        }
        (*T)->date=ch;
        CreatBiTreeNode(&(*T)->lchild);
        CreatBiTreeNode(&(*T)->rchild);
    }
}

void PreOrder(BiTreeNode *T,void(*Visit)(DateType item))
{
    if(T!=NULL)
    {
        Visit(T->date);
        PreOrder(T->lchild,Visit);
        PreOrder(T->rchild,Visit);
    }
}

void InOrder(BiTreeNode *T,void(*Visit)(DateType item))
{
    if(T!=NULL)
    {
        InOrder(T->lchild,Visit);
        Visit(T->date);
        InOrder(T->rchild,Visit);
    }
}
void PostOrder(BiTreeNode *T,void(*Visit)(DateType item))
{
    if(T!=NULL)
    {
        PostOrder(T->lchild,Visit);
        PostOrder(T->rchild,Visit);
        Visit(T->date);
    }
}

int BTreeDepth(BiTreeNode *T)
{
    int leftdep,rightdep;
    if (T==NULL)
        return 0;
    else
    {
        leftdep=BTreeDepth(T->lchild);
        rightdep=BTreeDepth(T->rchild);
        if(leftdep>rightdep)
            return (leftdep+1);
        else
            return (rightdep+1);
    }
}

int NodeCount(BiTreeNode *T)
{
    if(T=NULL)
        return 0;
    else
        return(NodeCount(T->lchild)+NodeCount(T->rchild)+1);
}

int LeafCount(BiTreeNode *T)
{
    if(T=NULL)
        return 0;
    else if ((T->lchild==NULL)&&(T->rchild==NULL))
        return 1;
    else
        return(LeafCount(T->lchild)+LeafCount(T->rchild)+1);
}

int OneChildCount(BiTreeNode *T)
{
    if(T=NULL)
        return 0;
    else
        if((T->lchild==NULL)&&(T->rchild!=NULL)||(T->lchild!=NULL)&&(T->rchild==NULL))
            return(OneChildCount(T->lchild)+OneChildCount(T->rchild)+1);
        else
            return(OneChildCount(T->lchild)+OneChildCount(T->rchild));
}

int TwoChildCount(BiTreeNode *T)
{
    if(T=NULL)
        return 0;
    else
        if((T->lchild==NULL)&&(T->rchild==NULL))
            return(TwoChildCount(T->lchild)+TwoChildCount(T->rchild));
        else
            return(TwoChildCount(T->lchild)+TwoChildCount(T->rchild)+1);
}

void main()
{
    int choice;
    BiTreeNode *root;
    printf("下面建立一颗二叉树,结点数据输入按先次序。\n");
    printf("输入下面数据请注意,每一个非空结点的所有孩子数据都要给出。\n");
    printf("若孩子为空,请用空格表示。\n");
    printf("请输入二叉树结点的数据,这里设定为字符。\n");
    CreatBiTreeNode(&root);

    printf("******************************\n");
    printf("1:先序序列:\n");
    printf("2:中序序列:\n");
    printf("3:后序序列:\n");
    printf("4:二叉树的深(高)度:\n");
    printf("5:二叉树的结点数:\n");
    printf("6:二叉树的叶子结点数:\n");
    printf("7:二叉树度为2 的结点数:\n");
    printf("8:二叉树度为1 的结点数:\n");
    printf("******************************\n");

    do
    {
        printf("请输入对相应的操作符:");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:printf("\n先序序列为:");
            PreOrder(root,PrintDate);
            printf("\n");
            break;
        case 2:printf("\n中序序列为:");
            InOrder(root,PrintDate);
            printf("\n");
            break;
        case 3:printf("\n后序序列为:");
            PostOrder(root,PrintDate);
            printf("\n");
            break;
        case 4:printf("二叉树的深度是:%d\n",BTreeDepth(root));
            break;
        case 5:printf("二叉树的节点数是:%d\n",NodeCount(root));
            break;
        case 6:printf("二叉树的叶子树是:%d\n",LeafCount(root));
            break;
        case 7:printf("二叉树度为二的节点数是:%d\n",OneChildCount(root));
            break;
        case 8:printf("二叉树度为一的节点数是:%d\n",TwoChildCount(root));
            break;
        }
    }while(choice>0&&choice<9);
}
搜索更多相关主题的帖子: include 二叉树 
2017-05-01 22:42
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
计算孩子是哪一块?有什么报错信息?
2017-05-02 20:22
愿o圆o安
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2017-3-13
收藏
得分:0 
回复 2楼 yangfrancis
int NodeCount(BiTreeNode *T)
{
&nbsp;&nbsp;&nbsp; if(T=NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&nbsp; else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(NodeCount(T->lchild)+NodeCount(T->rchild)+1);
}

int LeafCount(BiTreeNode *T)
{
&nbsp;&nbsp;&nbsp; if(T=NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&nbsp; else if ((T->lchild==NULL)&&(T->rchild==NULL))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;
&nbsp;&nbsp;&nbsp; else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(LeafCount(T->lchild)+LeafCount(T->rchild)+1);
}

int OneChildCount(BiTreeNode *T)
{
&nbsp;&nbsp;&nbsp; if(T=NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&nbsp; else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((T->lchild==NULL)&&(T->rchild!=NULL)||(T->lchild!=NULL)&&(T->rchild==NULL))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(OneChildCount(T->lchild)+OneChildCount(T->rchild)+1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(OneChildCount(T->lchild)+OneChildCount(T->rchild));
}

int TwoChildCount(BiTreeNode *T)
{
&nbsp;&nbsp;&nbsp; if(T=NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&nbsp; else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((T->lchild==NULL)&&(T->rchild==NULL))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(TwoChildCount(T->lchild)+TwoChildCount(T->rchild));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(TwoChildCount(T->lchild)+TwoChildCount(T->rchild)+1);
}



就是程序中的5 6 7 8 功能
2017-05-02 20:47
愿o圆o安
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2017-3-13
收藏
得分:0 
回复 2楼 yangfrancis
int NodeCount(BiTreeNode *T)
{
    if(T=NULL)
        return 0;
    else
        return(NodeCount(T->lchild)+NodeCount(T->rchild)+1);
}

int LeafCount(BiTreeNode *T)
{
    if(T=NULL)
        return 0;
    else
        return(LeafCount(T->lchild)+LeafCount(T->rchild)+1);
}

int OneChildCount(BiTreeNode *T)
{
    if(T=NULL)
        return 0;
    else
        if((T->lchild==NULL)&&(T->rchild!=NULL)||(T->lchild!=NULL)&&(T->rchild==NULL))
            return(OneChildCount(T->lchild)+OneChildCount(T->rchild)+1);
        else
            return(OneChildCount(T->lchild)+OneChildCount(T->rchild));
}

int TwoChildCount(BiTreeNode *T)
{
    if(T=NULL)
        return 0;
    else
        if((T->lchild==NULL)&&(T->rchild==NULL))
            return(TwoChildCount(T->lchild)+TwoChildCount(T->rchild));
        else
            return(TwoChildCount(T->lchild)+TwoChildCount(T->rchild)+1);
}
2017-05-02 20:49
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
等号==
2017-05-03 15:46
快速回复:二叉树 前面先中后续输出正常 后面计算孩子就出错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017887 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved