|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
void CreateBiTree(BiTree *T)
{
char c;
scanf("%c",&c);
if(c==' ')
{
*T=NULL;
}
else
{
*T=(BiTNode *)malloc(sizeof(BiTNode));
(*T)->data=c;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
横线这两行该怎么理解啊,求大佬指点
本帖最后由 jackz007 于 2019-9-24 17:32 编辑
CreateBiTree() 函数的功能是用递归函数创建二叉树型链表:
- CreateBiTree(&(*T)->lchild); // 递归创建左子节点
- CreateBiTree(&(*T)->rchild); // 递归创建右子节点
复制代码
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct erch
- {
- char c ;
- int rflat ;
- int lflat ;
- struct erch * zuo , * you ;
- } A ;
- void creation(A ** x)
- {
- char b ;
- * x = NULL ;
- scanf("%c" , & b) ;
- if(b != ' ') { // 只要键盘输入不是空格字符,就进行新节点的创建
- printf("标志1\n") ; // 开始创建新节点
- * x = (A *) malloc(sizeof(A)) ;
- (* x) -> c = b ;
- (* x) -> lflat = 0 ;
- (* x) -> rflat = 0 ;
- creation(& (* x) -> zuo) ; // 递归创建左子节点
- creation(& (* x) -> you) ; // 递归创建右子节点
- }
- }
- void show(A * p)
- {
- if(p) {
- printf("\n") ;
- printf("p -> c" = %c\n" , p -> c) ;
- printf("p -> lflat" = %d\n" , p -> lflat) ;
- printf("p -> rflat" = %d\n" , p -> rflat) ;
- show(p -> zuo) ;
- show(p -> you) ;
- }
- }
- int main(void)
- {
- A * fp ;
- creation(& fp) ;
- show(fp) ;
- }
复制代码
creation() 函数在创建新节点的同时,试图利用递归分别为左、右指针创建子节点,在递归的过程中,如果遇到键盘输入是空格,则放弃新节点创建,递归到达终点。
|
|