//Function for handle case3
void SiblingIsRed(Node **rootPtr){
  Node *root = *rootPtr;
  char colour = root->color; // store the original root color

  if( root->right ){
    leftRotate(&(*rootPtr));
    (*rootPtr)->left->color = 'r';
    
    if((*rootPtr)->left && ((*rootPtr)->left->right) && ((*rootPtr)->left->right->right) ){
      NephewIsRedSiblingIsBlack(&(*rootPtr)->left); 
     // printf("leftSide, enter case1\n");
    }
    else{
      NephewAndSiblingIsBlack(&(*rootPtr)->left);  
    //  printf("leftSide, enter case2\n");
    }
  }
  else if( root->left ){
    rightRotate(&(*rootPtr));
    (*rootPtr)->right->color = 'r';
    
    if((*rootPtr)->right && ((*rootPtr)->right->left) && ((*rootPtr)->right->left->left)){
      NephewIsRedSiblingIsBlack(&(*rootPtr)->right);  
     // printf("RightSide, enter case1\n");   
    }
    else{
      NephewAndSiblingIsBlack(&(*rootPtr)->right);   
     // printf("RightSide, enter case2\n"); 
    }
  }
  (*rootPtr)->color = colour;
}
Example #2
0
void caseSelect( Node **rootPtr){
  Node *root = *rootPtr;

  if(root){  
    if( checkForEnterCase1(root) ){
     // printf("enter case1\n");
      NephewIsRedSiblingIsBlack(&(*rootPtr));  
    }
    else if( checkForEnterCase2(root) ){
   //   printf("enter case2\n");
      NephewAndSiblingIsBlack(&(*rootPtr));     
    }
    else if( checkForEnterCase3(root) ){
    //  printf("enter case3\n");
      SiblingIsRed(&(*rootPtr));       
    }
  }
}
Example #3
0
void caseSelectForSucessor( Node **rootPtr){

  Node *root = (*rootPtr)->right;
  if(root == NULL )
    return;
  
  if(root ){
    if(root->color == 'b'){
      if((root->right && root->right->color =='r')||(root->left  && root->left->color == 'r')){
        NephewIsRedSiblingIsBlack(&(*rootPtr)); 
        //printf("enter Sucessor case1\n");
      }
      else if(( root->right == NULL ||  root->right->color == 'b') && (root->left == NULL || root->left->color == 'b')){
        NephewAndSiblingIsBlack(&(*rootPtr));   
        //printf("enter Sucessor case2\n");
      }
    }
   else if(root->color == 'r' && (root->left||root->right)){
      SiblingIsRed(&(*rootPtr));   
       //printf("enter Sucessor case3\n");
   }
  }
}