0
C에서 이진 검색 트리 : 주에서분할 오류, 나는이 기능을 사용하여 트리에 데이터를 삽입 할
struct treeNode{
data* val;
struct treeNode *left, *right, *parent;
};
void insert(data *d, struct treeNode **leaf, struct treeNode **leaf_par)
{
if(*leaf == 0)
{
*leaf = (struct treeNode*) malloc(sizeof(struct treeNode));
(*leaf)->val = d;
/* initialize the children to null */
(*leaf)->left = 0;
(*leaf)->right = 0;
/* initialize the parent */
(*leaf)->parent = *leaf_par; //here I receive segmentation fault
}
else if(strcmp(d->name, (*leaf)->val->name) < 0)
{
insert(d, &(*leaf)->left, &(*leaf));
}
else if(strcmp(d->name, (*leaf)->val->name) > 0)
{
insert(d, &(*leaf)->right, &(*leaf));
}
}
나는이 :
struct treeNode *root = NULL;
data d1 = {"Smith"};
insert(&d1, &root, NULL);
분할 결함이있다 :
(*leaf)->parent = *leaf_par;
처음 * leaf_par이 NULL이고 올바르게 실행되지 않는 이유를 알 수 없습니다. 삽입 기능을 어떻게 수정해야합니까? "부모"포인터가 없으면 쉽지만, "부모"와 함께해야하고 작동하지 않습니다.
분명히, 당신은'** leaf_par'를 위해'NULL'을 전달하지만'* leaf_par' 접근을 시도합니다, 따라서 오류입니다. 이 경우 부모님 께 무엇을 드시겠습니까? 당신은 그 사건을 확인하고 그에 따라 그것을 설정할 필요가 있습니다. – lurker