Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 导致分段错误的结构_C_Segmentation Fault_Red Black Tree - Fatal编程技术网

C 导致分段错误的结构

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

我很难克服这个错误。我正在用C实现一个红黑树,在某个特定位置(第29行)遇到分段错误

}

在实现最后三个if语句将BST转换为RB-tree之前,代码运行良好。奇怪的是,当我调试
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))