c++ - Traversing binary tree appears
怪我咯
怪我咯 2017-06-17 09:16:08
0
2
737
#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. .

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
滿天的星座

lchild and rchild are both pointers. What they point to should be a node structure. However, in Create, we do not see the initialization of the structures pointed to by lchild and rchild. .

小葫芦

T==NULL should be changed to T=NULL

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template