gpt4 book ai didi

c - 红黑树书实现(SIGSEGV发生)

转载 作者:行者123 更新时间:2023-11-30 16:49:49 25 4
gpt4 key购买 nike

我正在尝试根据我的大学书籍编写标题算法的实现。我已经编写了最重要的函数,但最终程序崩溃了。 GDB在线调试器指向第91行程序收到信号 SIGSEGV,段错误。 0x0000000000400ac4 in RB_Insert (T=0x0, k=5) at main.c:90 90 while((X != root) && (X->up->color == 'R')){

5 是我尝试插入的第一个值,所以我想知道为什么语句 X != root 不会阻止它并且打印 sigsegv

我将在此处粘贴整个代码:

#include<stdio.h>
#include<stdlib.h>

struct node{
int key;
char color;
struct node *up;
struct node *left;
struct node *right;
}*root;


void in_order_tree_walk(struct node *X){
if(X!=NULL){
in_order_tree_walk(X->left);
printf("%d %c\n", X->key, X->color);
in_order_tree_walk(X->right);
}
}

struct node* tree_search(struct node *X, int k){
if(X==NULL || k==X->key)
return X;
if(k<X->key)
return tree_search(X->left, k);
else
return tree_search(X->right, k);
}

struct node* tmp_node(int k){
struct node *tmp = (struct node*)malloc(sizeof *root);
tmp->up = NULL;
tmp->left = NULL;
tmp->right = NULL;
tmp->key = k;
tmp->color = 'R';
return tmp;
}

void tree_insert(struct node *T, struct node *Z){
if (T == NULL){
T = Z;
return;
}
else{
if (Z->key < T->key){
tree_insert(T->left, Z);
T->left->up = T;
}
else if (Z->key > T->key){
tree_insert(T->right,Z);
T->right->up = T;
}
}
}

void left_rotate(struct node *X){
struct node *Y = X->right;
X->right = Y->left;
if(Y->left != NULL)
Y->left->up = X;
Y->up = X->up;
if(X->up == NULL)
root = Y;
else if( X == X->up->left)
X->up->left = Y;
else X->up->right = Y;
Y->left = X;
X->up = Y;
}

void right_rotate(struct node *X){
struct node *Y = X->left;
X->left = Y->right;
if(Y->right != NULL)
Y->right->up = X;
Y->up = X->up;
if(X->up == NULL)
root = Y;
else if( X == X->up->left)
X->up->left = Y;
else X->up->right = Y;
Y->right = X;
X->up = Y;
}

void RB_Insert(struct node *T, int k){
struct node *X = tmp_node(k);
tree_insert(T, X);
X->color = 'R';
while(X != root && X->up->color == 'R'){
if(X->up = X->up->up->left){
struct node *Y = X->up->up->right;
if(Y->color == 'R'){
X->up->color = 'B';
Y->color = 'B';
X->up->up->color = 'R';
X = X->up->up;
}
else{
if (X == X->up->right){
X = X->up;
left_rotate(X);

}
X->up->color = 'B';
X->up->up->color = 'R';
right_rotate(X->up->up);
}
}
else{
struct node *Y = X->up->up->left;
if(Y->color == 'R'){
X->up->color = 'B';
Y->color = 'B';
X->up->up->color = 'R';
X = X->up->up;
}
else{
if (X == X->up->left){
X = X->up;
right_rotate(X);
}
X->up->color = 'B';
X->up->up->color = 'R';
left_rotate(X->up->up);
}
}
}
root->color = 'B';
}

int main(){

root = NULL;
int T[] = {5,26,17,8,9,30,10,1,23};
int i;
for(i=0; i<9; i++){
printf("%d", T[i]);
RB_Insert(root, T[i]);
}
printf("\n");
in_order_tree_walk(root);

return 0;
}

最佳答案

我将tree_insert更改为

struct node *Y = NULL;
struct node *X = root;
while(X != NULL)
{
Y = X;
if(Z->key < X->key) X=X->left;
else X=X->right;
}
Z->up = Y;
if( Y == NULL)
root = Z;
else if(Z->key < Y->key) Y->left = Z;
else Y->right = Z;

并在第 114 行添加了 if 语句 if(Y != NULL && Y->color == 'R'){ 现在工作正常

关于c - 红黑树书实现(SIGSEGV发生),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42456064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com