c - Struct causing segmentation fault -
c - Struct causing segmentation fault -
i having problem grappling error. implementing reddish black tree in c , running segmentation fault @ particular location (line 29)
 tnode *tree_add(tnode *root, const key k, const value v) {         lnode *lnode = null;         if (root == null) {                 tnode *node = talloc(k);                 lnode = lalloc(v);                 node->head = lnode;                 node->tail = lnode;                 node->is_red = true;                  homecoming node;         }         if (strcmp(k, root->key) < 0) {                  root->left = tree_add(root->left, k, v);         } else if (strcmp(k, root->key) > 0) {                   root->right = tree_add(root->right, k, v);         } else {                 if (strcmp(k, root->key) == 0) {                         lnode = lalloc(v);                         root->tail->next = lnode;                         root->tail = lnode;                         root->tail->next = null;                 }         }         if (is_red(root->right) && !is_red(root->left)) { //is_red seg faulting                 root = rotate_left(root);         }         if (is_red(root->left) && is_red(root->left->left)) {                 root = rotate_right(root);         }         if (is_red(root->left) && is_red(root->right)) {                 flip_colors(root);         }          homecoming root;  }    here is_red function: 
bool is_red(const tnode *h) {      bool is_red = h->is_red;      homecoming is_red;    }
before implementing  lastly 3 if statements convert bst rb tree, code works fine. weird when debugged is_red, variable is_red comes true. means not accessing restricted memory. however, seg fault  homecoming is_red function , intotree_add.
for farther clarification, here tnode struct:
 typedef struct tnode {   key key;             // search key binary search tree node.   struct tnode *right; // right child.   struct tnode *left;  // left child.    lnode *head; // head of linked list storing values search key.   lnode *tail; // tail of linked list storing values search key.    bool is_red; // flag  utilize in red-black trees denote redness. } tnode;       
you want create sure right kid , left kid exist before is_red check: replace
if (is_red(root->right) && !is_red(root->left)) //is_red giving me seg fault    with
if (root->right && is_red(root->right) && root->left && !is_red(root->left))     please similar check other places.
 c segmentation-fault red-black-tree 
 
  
Comments
Post a Comment