春秋; 2017-03-30 13:44 采纳率: 0%
浏览 856

先序遍历建立树,算法有了,源代码不知道错在哪里

#include
#include

typedef struct BiTNode
{
char data;//节点数据
struct BiTNode * lchild;//左孩子
struct BiTNode * rchild;//右孩子
}BiTNode, * BiTree;
int CreateBiTree(BiTree T)/*先序构造二叉树*/
{
char ch;
scanf("%c",&ch);
if(ch=='#')
T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(T==NULL)
{
printf("节点动态分配失败");
return 0;
}
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return 1;
}
int InOrderTraveler( BiTree T )
{
if( NULL!= T )
{
printf( "%c", T -> data );
InOrderTraveler( T -> lchild );
InOrderTraveler( T -> rchild );
}
return 1;
}
int leafcount(BiTree T)/*叶子结点个数*/
{
static int LeafCount= 0;
if(T!=NULL)
{
leafcount(T->lchild);
leafcount(T->rchild);
if(T->lchild==NULL&&T->rchild==NULL)
LeafCount++;
}
return LeafCount;
}
int PostTreeDepth(BiTree T)
{
int hl,hr,max,depth;
if(T!=NULL)
{
hl=PostTreeDepth(T->lchild);/*求左子树深度*/
hr=PostTreeDepth(T->rchild);/*求右子树深度*/
max=hl>hr?hl:hr;/*得到左,右子树深度较大者*/
depth=max+1;
return(depth);/*返回树的深度*/
}
else return 0;/*如果是空树,则返回0*/
}
int BinTreeNodeCount(BiTNode T )/统计结点个数*/
{
int N, num1, num2;

if( T==NULL )
{
    return 0;
}
else
{
    num1 = BinTreeNodeCount( T->lchild );/*统计左子树节点个数*/
    num2 = BinTreeNodeCount( T->rchild );/*统计右子树节点个数*/
    N = num1 + num2 + 1;
    printf("%d",N);
    return N;
}

}

int countDegreeTwo(BiTNode T)/统计度为2的结点个数*/
{
if (T == NULL)
return 0;
if (T->lchild != NULL&& T->rchild != NULL)
return 1 + countDegreeTwo(T->lchild) + countDegreeTwo(T->rchild);
return countDegreeTwo(T->lchild) + countDegreeTwo(T->rchild);
}

int main()
{
BiTree T=NULL;
printf("按先序顺序输入第一个二叉树:");
CreateBiTree(T);
printf("%d",leafcount(T));
printf("%d",BinTreeNodeCount(T));
printf("%d",countDegreeTwo(T));
InOrderTraveler(T);
return 1;
}

  • 写回答

1条回答

  • 小灸舞 2017-03-31 08:09
    关注

    你这个错误在于,你想在CreateBiTree函数里改变你main函数的T指针的指向。
    要在函数内改变指针的指向必须传二级指针或者一级指针的引用才行。
    图片说明

     #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct BiTNode
    {
        char data;//节点数据
        struct BiTNode * lchild;//左孩子
        struct BiTNode * rchild;//右孩子
    }BiTNode, *BiTree;
    int CreateBiTree(BiTree *T)/*先序构造二叉树*/
    {
        char ch;
        scanf("%c", &ch);
        if (ch == '#')
            *T = NULL;
        else
        {
            *T = (BiTree)malloc(sizeof(BiTNode));
            if (*T == NULL)
            {
                printf("节点动态分配失败");
                return 0;
            }
            (*T)->data = ch;
            CreateBiTree(&(*T)->lchild);
            CreateBiTree(&(*T)->rchild);
        }
        return 1;
    }
    int InOrderTraveler(BiTree T)
    {
        if (NULL != T)
        {
            printf("%c", T->data);
            InOrderTraveler(T->lchild);
            InOrderTraveler(T->rchild);
        }
        return 1;
    }
    int leafcount(BiTree T)/*叶子结点个数*/
    {
        static int LeafCount = 0;
        if (T != NULL)
        {
            leafcount(T->lchild);
            leafcount(T->rchild);
            if (T->lchild == NULL&&T->rchild == NULL)
                LeafCount++;
        }
        return LeafCount;
    }
    int PostTreeDepth(BiTree T)
    {
        int hl, hr, max, depth;
        if (T != NULL)
        {
            hl = PostTreeDepth(T->lchild);/*求左子树深度*/
            hr = PostTreeDepth(T->rchild);/*求右子树深度*/
            max = hl>hr ? hl : hr;/*得到左,右子树深度较大者*/
            depth = max + 1;
            return(depth);/*返回树的深度*/
        }
        else return 0;/*如果是空树,则返回0*/
    }
    int   BinTreeNodeCount(BiTNode *T)/*统计结点个数*/
    {
        int  N, num1, num2;
    
        if (T == NULL)
        {
            return 0;
        }
        else
        {
            num1 = BinTreeNodeCount(T->lchild);/*统计左子树节点个数*/
            num2 = BinTreeNodeCount(T->rchild);/*统计右子树节点个数*/
            N = num1 + num2 + 1;
            printf("%d", N);
            return N;
        }
    }
    
    int countDegreeTwo(BiTNode *T)/*统计度为2的结点个数*/
    {
        if (T == NULL)
            return 0;
        if (T->lchild != NULL&& T->rchild != NULL)
            return 1 + countDegreeTwo(T->lchild) + countDegreeTwo(T->rchild);
        return countDegreeTwo(T->lchild) + countDegreeTwo(T->rchild);
    }
    
    int main()
    {
        BiTree T = NULL;
        printf("按先序顺序输入第一个二叉树:");
        CreateBiTree(&T);
        printf("%d", leafcount(T));
        printf("%d", BinTreeNodeCount(T));
        printf("%d", countDegreeTwo(T));
        InOrderTraveler(T);
        return 1;
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧