#include <stdio.h>
#include <iostream>
#include <string.h>
typedef struct node{
char data[4]; //结点数据是字符串
node *lchild,*rchild;
}NODE,*BITREE;
void Create(BITREE &T){
char str[4];
scanf("%s",str);
if(str[0]=='#'){
T==NULL;
}
else{
T=new NODE;
strcpy(T->data,str);
Create(T->lchild);
Create(T->rchild);
}
}
void Traverse(BITREE T){
if(T){
Traverse(T->lchild); //!!!debug在此处segmentfault
printf("%s",T->data);
Traverse(T->rchild);
}
}
int main(){
BITREE T;
Create(T);
Traverse(T);
}
Example: 4 2 1 # # 3 # # 5 # 6 # # (Enter in order)
I don’t know what went wrong. . . The code only modifies the data part of the textbook. .
lchild
andrchild
are both pointers. What they point to should be anode
structure. However, inCreate
, we do not see the initialization of the structures pointed to bylchild
andrchild
. .T==NULL should be changed to T=NULL