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

问题描述

@数据结构大神:递归实现二叉树遍历,图示行为什么错?
 # 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();
}

解决方案

 T没有初始化
int main(BiTree T)
->
int main()

CreateBiTree(T);前面加上
BiTree T

void CreateBiTree(BiTree T)
->
void CreateBiTree(BiTree& T)

解决方案二:

数据结构----二叉树非递归实现

时间: 2024-08-03 19:47:35

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

@数据结构大神:递归遍历二叉树,建立树的代码 为什么错?

问题描述 @数据结构大神:递归遍历二叉树,建立树的代码 为什么错? //创建-输入-打印-递归 # include<stdio.h> # include<stdlib.h> # include<malloc.h> typedef struct Node{ char data; struct Node *Lchild; struct Node *Rchild; }BiTNode,*BiTree; BiTree CreateBiTree(BiTree bt) { char

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

问题描述 @数据结构大神:递归实现二叉树遍历,46.477行为什么错? # 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; void CreateBiTree(Bi

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

问题描述 @数据结构大神:递归实现二叉树遍历,38行为什么错? # include<stdio.h> # include<stdlib.h> # include<malloc.h> # define Max_Size 3 typedef struct Node{ int data; struct Node *Lchild; struct Node *Rchild; }BiTNode,*BiTree; int x,k=0; void CreateBiTree(BiTree

求数据结构大神指教一下

问题描述 求数据结构大神指教一下 数据结构在树的存储那一块有个叫链域的东西,请问什么是链域啊,是相当于指针吗? 解决方案 链域就是一个节点.一个节点由2部分构成. 链就是指向上一级.下一级或者同级另一个节点的指针.域就是这个节点本身保存的数据. 线索二叉树保存同一级下一个指针.一般二叉树保存子节点.也有双向二叉树,保存父节点的. 解决方案二: 求大神指教

指针-@数据结构大神:链队列的5种操作,33行判断节点为空,为啥会错?求解释!(没加头节点)

问题描述 @数据结构大神:链队列的5种操作,33行判断节点为空,为啥会错?求解释!(没加头节点) 解决方案 传进来的参数Q是个NULL 解决方案二: Q没有分配空间

内存分配-@数据结构大神,链接两个循环链表,第44行为何不对?求解释~

问题描述 @数据结构大神,链接两个循环链表,第44行为何不对?求解释~ include include typedef struct Node { char data; struct Node *next; }Node,*Linklist;//先定义.后使用 int len; Linklist Createlist(int *array,int len) { Linklist head,temp,ptr; int i; head=(Node*)malloc(sizeof(Node)); if(!

c语言 数据结构-这里的时间复杂度为啥不是√n?求数据结构大神给我解释一下

问题描述 这里的时间复杂度为啥不是√n?求数据结构大神给我解释一下 (4)求解释,这里的时间复杂度为啥不是√n?求数据结构大神给我解释一下 解决方案 如果循环体的开销是线性的,那么显然是sqrt(n) 这种胡乱编写的题目,抄来传去,你的老师都不当真,你也不用计较了. 解决方案二: 真确答案是多少?也许n是一个常数,所以复杂度就是O(1)? 解决方案三: ...... 什么鬼玩意~应该是根号n吧

指针-@数据结构大神:链队列的5种操作,42行判断队为空,为啥会错?求解释!`

问题描述 @数据结构大神:链队列的5种操作,42行判断队为空,为啥会错?求解释!` int Init_Queue(LinkQueue *Q) { Q=(LinkQueue*)malloc(sizeof(LinkQueue)); if(Q==NULL) return 0; Q->front=(QueueNode*)malloc(sizeof(QueueNode)); if(Q->front==NULL) return 0; Q->rear=(QueueNode*)malloc(sizeof

map-不懂,求大神教教,是遍历,然后再取类中的key和value吗

问题描述 不懂,求大神教教,是遍历,然后再取类中的key和value吗 public void browselist(Map<String, HomeworkList> map){ Set<Map.Entry<String, HomeworkList>> set = map.entrySet(); for(Iterator<Map.Entry<String, HomeworkList>> it = set.iterator(); it.hasNe