How to get data from datalist?

My red black tree algo is correct for a data but if I change the input sequence of same data, I get segmentation failed (core dump)?

  • #include<stdio.h> #include<stdlib.h> struct node{ int data; int colr; struct node* left; struct node* right; struct node* father; }; typedef struct node* Node; Node getnode(int x){ Node temp=(Node)malloc(sizeof(struct node)); temp->data=x;temp->colr=0;temp->left=NULL;//0 RED temp->right=NULL;temp->father=NULL; return(temp); } void intrav(Node tree) { if(tree!=NULL) { intrav(tree->left); printf("%d   %d\n",tree->data,tree->colr); intrav(tree->right); } } void LeftRotation(Node* ref,Node x){ Node y=x->right; x->right=y->left;if(y->left!=NULL)y->left->father=x; y->father=x->father;if(x->father==NULL)*ref=y;                     else if(x==x->father->left)x->father->left=y;                            else x->father->right=y; y->left=x;x->father=y; } void RightRotation(Node* ref,Node y){ Node x=y->left;y->left=x->right; if(x->right!=NULL)x->right->father=y; x->father=y->father; if(y->father==NULL)*ref=x; else if(y->father->left==y)y->father->left=x; else y->father->right=x;    x->right=y;y->father=x; } void InsertFix(Node* ref,Node x){ Node y;//intrav(*ref);printf("\n\n"); while(x!=*ref && x->father->colr==0) { if(x->father==x->father->father->left) { y=x->father->father->right; if(y!=NULL && y->colr==0) { x->father->colr=1;y->colr=1; x->father->father=0;x=x->father->father; } else { if(x==x->father->right){ x=x->father;LeftRotation(ref,x);} x->father->colr=1; x->father->father->colr=0; RightRotation(ref,x->father->father); } } else { y=x->father->father->left;//printf("\n%d %d ",x->data,y->data); if(y!=NULL && y->colr==0){ x->father->colr=1;y->colr=1; x->father->father->colr=0; x=x->father->father; } else { if(x==x->father->left){ x=x->father;RightRotation(ref,x);} x->father->colr=1; x->father->father->colr=0; LeftRotation(ref,x->father->father); } } } (*ref)->colr=1; } void InsertTree(Node* ref,Node z){ Node y=NULL; Node x=*ref; while(x!=NULL){ y=x; if(x->data>z->data)x=x->left;     else x=x->right;     }     z->father=y;     if(y==NULL)*ref=z;     else if(y->data>z->data)y->left=z;          else y->right=z;          InsertFix(ref,z); } int main(){ Node head=NULL; InsertTree(&head,getnode(7)); InsertTree(&head,getnode(3)); InsertTree(&head,getnode(18)); InsertTree(&head,getnode(10)); InsertTree(&head,getnode(8)); InsertTree(&head,getnode(22)); InsertTree(&head,getnode(11)); InsertTree(&head,getnode(26)); intrav(head); //printf("\n%d %d",head->data,head->right->data); return 0; }

  • Answer:

    Atleast give a link to the code, you cannot expect someone to discover the bug in your code just by seeing this question. A correctly implemented Red-Black tree should not give coredump for any input sequence. Maybe your original sequence does not use some operations, for example rotations.

Saurav Shekhar at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.