Пример #1
0
//use predecessor instead of successor
void
Tree_Delete_v2 (
  NODE **root,
  NODE *z
  )
{
  NODE *y;

  if (!z->left) {
    //either z->right is null (no child)
    //or z->right only (replace z with z->right)
    printf ("case 1:\n");
    Transplant (root, z, z->right);
  } else if (!z->right) {
    //z->left only, replace z with z->left
    printf ("case 2:\n");
    Transplant (root, z, z->left);
  } else {
    //both z->right and z->left are valid
    y = Tree_Predecessor (z);
    printf ("Predecessor of %d is:\n", z->key);
    Print_Node (y);
    if (y != z->left) {
      //replace y with y->left
      //y has no right child
      Transplant (root, y, y->left);
      y->left = z->left;
      z->left->p = y;
    }
    //if y == z->left, replace z with y directly
    Transplant (root, z, y);
    y->right = z->right;
    z->right->p = y;
  }
}
Пример #2
0
void BST :: BSTdelete( NodePtr z )
{
   NodePtr y ;
   if(z->left==NIL)
   {
       Transplant(z,z->right);
   }
   else if(z->right==NIL)
   {
       Transplant(z,z->left);
   }
   else
   {
       y = BSTminimum(z->right);
       if(y->p != z)
       {
           Transplant(y,y->right);
           y->right = z->right;
           y->right->p=y;
       }
        Transplant(z,y);
        y->left=z->left;
        y->left->p = y;
   }

   // You write the rest of this
}
Пример #3
0
void
Tree_Delete (
  NODE **root,
  NODE *z
  )
{
  NODE *y;

  if (!z->left) {
    //either z->right is null (no child)
    //or z->right only (replace z with z->right)
    printf ("case 1:\n");
    Transplant (root, z, z->right);
  } else if (!z->right) {
    //z->left only, replace z with z->left
    printf ("case 2:\n");
    Transplant (root, z, z->left);
  } else {
    //z->left and z->right are valid,
    //find successor(z):
    y = Tree_Successor (z);  //y must in the right subtree of z
    printf ("successor of %d is:\n", z->key);
    Print_Node (y);
//    //for debug
//    if (y) {
//      printf ("successor: key = %d, p = 0x%x, left = 0x%x, right = 0x%x\n",
//              y->key, (int)(y->p), (int)(y->left), (int)(y->right));
//      if (y->p) {
//        printf ("p = %d, ", y->p->key);
//      }
//      if (y->left) {
//        printf ("left = %d, ", y->left->key);
//      }
//      if (y->right) {
//        printf ("right = %d, ", y->right->key);
//      }
//      printf ("\n");
//    } else {
//      printf ("successor: not found.\n");
//    }

    //and y has no left childs !!
    if (y->p != z) {
      printf ("case 3:\n");
      //replace y with y->right
      //update childs manually:
      //Transplant () updates parents only
      Transplant (root, y, y->right);
      y->right = z->right;
      z->right->p = y;
    }
    printf ("case 4:\n");
    //if y == z->right, replace z with y directly
    Transplant (root, z, y);
    //z has left childs, update their parents
    y->left = z->left;
    z->left->p = y;
  }
}
Пример #4
0
void RedBlackTree::Delete(uint32_t key)
{
    
    RedBlackNode::Ptr z = Search(key);
    RedBlackNode::Ptr x;
    if(z == nil)
        return;
    
    RedBlackNode::Ptr y = z;
    RedBlackNode::color_t original_color = y->Color();
    if(z->Left() == nil)
    {
        x = z->Right();
        Transplant(z,z->Right());
    }
    else if(z->Right() == nil)
    {
        x = z->Left();
        Transplant(z,z->Left());
    }
    else
    {
        y = Minimum(z->Right());
        original_color = y->Color();
        x = y->Right();
        if(y->Parent() == z)
        {
            x->Parent(y);
        }
        else
        {
            Transplant(y,y->Right());
            y->Right(z->Right());
            y->Right()->Parent(y);
        }
        Transplant(z,y);
        y->Left(z->Left());
        y->Left()->Parent(y);
        y->Color(z->Color());
    }
    if(original_color == RedBlackNode::BLACK)
    {
        Delete_Fixup(x);
    }
}