qq_31750507 2015-12-20 13:58 采纳率: 52.6%
浏览 1467

@数据结构大神:递归实现二叉树遍历,图示行为什么错?

 # include<stdio.h>
# include<stdlib.h>
# include<malloc.h>
# define Max_Size 2

typedef struct Node{
    int data;
    struct Node *Lchild;
    struct Node *Rchild;
}BiTNode,*BiTree;

int x,k=0,n=0;


void CreateBiTree(BiTree T)//L是节点 
{
 while(k!=Max_Size)
     {
      scanf("%d",&x);++k;//注意写法
      if(!(T=(BiTree)malloc(sizeof(BiTree)))) exit(0);//已满,不能分配
      else 
      {
       T->data=x;
       CreateBiTree(T->Lchild);
       CreateBiTree(T->Rchild);
      } 
     }
}


void Visit(BiTree T)
{
    printf("%d,",x);
}


void PreOrder(BiTree T)

{
    if(NULL!=T)
    {
        printf("%d  ",T->data);
        PreOrder(T->Lchild);
        PreOrder(T->Rchild);
    }
}


/*int PreOrder(BiTree T.int (* Visit)(int e))
{
    int Print(int e)
    {
        printf("%d,",e);
        return 1;
    }//是否打印

        if(T){
              if(Visit(T->data))
               if(PreOrder(T->Lchild.visit))
                if(PreOrder(T->Lchild.visit)) return 1;
              return 0;
             }
        else return 1;

}*/


void Leaf(BiTree T)
{
    if(T!=NULL)
    {
        Leaf(T->Lchild);
        Leaf(T->Rchild);

        if((T->Lchild==NULL)&&(T->Rchild==NULL))
        n++;
    }

    printf("the leafnumber is:%d",n);
}


BiTree Transform(BiTree T)
{
    BiTree Temp;
    if(T!=NULL)
    {
        Transform(T->Lchild);
        Transform(T->Rchild);
        Temp=T->Lchild;
        T->Lchild=T->Rchild;
        T->Rchild=Temp;
    }
    return T;
}


int main(BiTree T)
{   
    CreateBiTree(T);
    PreOrder(T);
    Transform(T);
    Leaf(T);
    getch();
} 

图片说明

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-12-20 14:16
    关注
     T没有初始化
    int main(BiTree T)
    ->
    int main()
    
    CreateBiTree(T);前面加上
    BiTree T
    
    void CreateBiTree(BiTree T)
    ->
    void CreateBiTree(BiTree& T)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?