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
Related Q & A:
- How do I change the cursor from a blinking black block to a blinking line?Best solution by Server Fault
- How can I change the font for e-mail that I compose?Best solution by Yahoo! Answers
- How do I find out if my red maple tree is a male or female?Best solution by maple-trees.com
- What is Red-Black Tree DataStructure?Best solution by cs.auckland.ac.nz
- If i have a medical degree and i want to practice in Canada, what do i have to do?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.