Home  >  Q&A  >  body text

c++ - Traversing binary tree appears

#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. .

怪我咯怪我咯2654 days ago687

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-06-17 09:18:08

    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. .

    reply
    0
  • 高洛峰

    高洛峰2017-06-17 09:18:08

    T==NULL should be changed to T=NULL

    reply
    0
  • Cancelreply