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

问题描述

@数据结构大神:递归遍历二叉树,建立树的代码 为什么错?
 //创建-输入-打印-递归
# 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 data;int flag=1;

 while(flag)
 {
 scanf("%c",&data);

 if(data!='$')
 {
     bt=(BiTNode *)malloc(sizeof(BiTNode));
     bt->data=data;
        bt-Rchild=(BiTNode *)malloc(sizeof(bt-Rchild));
     bt->Rchild=CreateBiTree(bt->Rchild); //为啥建立完右孩子之后就return了呢?
        bt-Lchild=(BiTNode *)malloc(sizeof(bt-Lchild));
     bt->Lchild=CreateBiTree(bt->Lchild);//为啥return后才建立左孩子呢?
 }
 else {flag=0;}
 }

 return bt;
}

void Visit(BiTree bt)
{
 while(bt->data!='$')
 printf("%c,",bt->data);
}

void PreOrder(BiTree bt)
{
 if(bt!=NULL)
 {
  Visit(bt);
  PreOrder(bt->Lchild);//全部遍历
  PreOrder(bt->Rchild);//全部遍历
 }
}

int main()
{
 BiTNode *bt;
 CreateBiTree(bt);
 PreOrder(bt);
 getch();
}

解决方案

数据结构-非递归遍历二叉树
数据结构-递归遍历二叉树
数据结构--第六章 遍历二叉树

解决方案二:

bt-Rchild=(BiTNode *)malloc(sizeof(bt-Rchild));

你有的没有写指针的访问符!
bt->Rchild

时间: 2024-08-31 18:24:12

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

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

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

@数据结构大神:递归实现二叉树遍历,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

递归遍历二叉树,怎样保存结点数值到数组里

问题描述 递归遍历二叉树,怎样保存结点数值到数组里 求讲解 递归遍历二叉树的时候,怎样能 在访问每个结点时,将结点的数值存到数组里.最后得到一个结点数值的数组.教材上遍历的时候都是直接输出,没有存到数组里,但是我在编程时遇到了要存到数组里的问题.求大神~~~ 追问: 追问一下,我晚上试了一下,感觉使用数组作为参数看起来可以,但是在递归的时候每次递归数组的角标i都会被重新定义.貌似全局变量或者静态局部变量在递归时都会被重新定义.这怎么处理啊 好心塞 解决方案 将数组作为一个遍历函数的一个参数,遍历

求数据结构大神指教一下

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

有关后序非递归遍历二叉树的问题

问题描述 有关后序非递归遍历二叉树的问题 void show_LRD(tree LRD) { //后序非递归遍历二叉树 int otherstack[max];//辅助栈,用于检测出栈时是否已经遍历右子树 int *othertop,*otherbottom; tree temp; othertop=otherbottom=otherstack; while(LRD||!emptystack()) { while(LRD) { while(LRD) { inputstack(LRD); *oth

内存分配-@数据结构大神,链接两个循环链表,第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(!

指针-@数据结构大神:链队列的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

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

问题描述 (有头节点)@数据结构大神:链队列的5种操作,33行判断节点为空,为啥会错?求解释! //链队列的5种操作.c include include include define Stack_Size 50 typedef struct QueueNode{ int data;//数据保持原来结构即可 struct QueueNode *next;//注意next是QueueNode里面的东西,结构为Struct QueueNode }QueueNode; QueueNode *head=N