C 导致分段错误的结构
我很难克服这个错误。我正在用C实现一个红黑树,在某个特定位置(第29行)遇到分段错误 } 在实现最后三个if语句将BST转换为RB-tree之前,代码运行良好。奇怪的是,当我调试C 导致分段错误的结构,c,segmentation-fault,red-black-tree,C,Segmentation Fault,Red Black Tree,我很难克服这个错误。我正在用C实现一个红黑树,在某个特定位置(第29行)遇到分段错误 } 在实现最后三个if语句将BST转换为RB-tree之前,代码运行良好。奇怪的是,当我调试is_red时,变量is_red显示为true。这意味着我没有访问受限内存。但是,当我从is\u red函数返回并进入树添加时,我会得到一个seg故障 为了进一步澄清,这里是TNODE结构: typedef struct tnode { KEY key; // Search key for
is_red
时,变量is_red
显示为true
。这意味着我没有访问受限内存。但是,当我从is\u red
函数返回并进入树添加时,我会得到一个seg故障
为了进一步澄清,这里是TNODE结构:
typedef struct tnode {
KEY key; // Search key for this binary search tree node.
struct tnode *right; // Right child.
struct tnode *left; // Left child.
LNODE *head; // Head of the linked list storing the values for the search key.
LNODE *tail; // Tail of the linked list storing the values for the search key.
bool is_red; // Flag use only in red-black trees to denote redness.
} TNODE;
在执行IS_红色检查之前,要确保右子级和左子级存在:
替换
与
请也对其他地方进行类似检查。我建议删除行号。:)@萨马拉斯好的,如果它分散注意力的话,我会移除它们。
bool is_red(const TNODE *h) {
bool is_red = h->is_red;
return is_red;
typedef struct tnode {
KEY key; // Search key for this binary search tree node.
struct tnode *right; // Right child.
struct tnode *left; // Left child.
LNODE *head; // Head of the linked list storing the values for the search key.
LNODE *tail; // Tail of the linked list storing the values for the search key.
bool is_red; // Flag use only in red-black trees to denote redness.
} TNODE;
if (is_red(root->right) && !is_red(root->left)) //is_red is giving me seg fault
if (root->right && is_red(root->right) && root->left && !is_red(root->left))