Deletion in a Binary search Tree


The teacher explained to us this algorithm for deleting a node in a binary search tree, but I can’t understand how it works when the node to be deleted has only one child (I already know how it works theoretically).

Algorithm:

abc_delete(T, z) // z is the node that must be eliminated  {         if((z.left == NULL) && (z.right == NULL))                 y = z;         else                 y = abr_successor(z);          if(y.left != NULL)                     x = y.left;         else                     x = y.right;          if(x != NULL)                 x.p = y.p;          if(y.p == NULL)                 T.root = x;         else         {                 if(y == (y.p).left)                         (y.p).left = x;                 else                         (y.p).right = x;         }          if(y != z)                 z.key = y.key;         return y; }  abr_successor(x) {         if(x == NULL)                 return NULL;         if(x.right != NULL)                 return abr_min(x.right)         y = x.p;         while(y != NULL && x = y.right)         {                 x = y;                 y = y.p;         }         return y; } 

For example, I want to delete the node number $ 7$ : enter image description here

But, shouldn’t the end result be this? enter image description here