Language: C

#include <stdio.h>
#include <stdlib.h>
#define NIL NULL

struct node {
  int key;
  struct node *right;
  struct node *left;
  struct node *parent;
};
typedef struct node * Node;
Node root;
void Insert(int );
void inorder(Node );
void preorder(Node );
Node search(Node, int);
Node treeSuccessor(Node );
void treeDelete(Node );
Node treeMinimum(Node);
int main()
{
  int i,j,k,yflag=0,nflag=0;
  char c[10];
  Node a;
  scanf("%d",&k);

  for(i=0;i<k;i++)
    {
      scanf("%s",&c[0]);
      if(c[0]=='f')
        {
          scanf("%d",&j);
          a=search(root,j);
          if(a!=NIL) printf("yes\n");
          else printf("no\n");
        }
      if(c[0]=='i')
        {
          scanf("%d",&j);
          Insert(j);
        }
else if(c[0]=='p')
        {
          inorder(root);
          printf("\n");
          preorder(root);
          printf("\n");
        }
else if(c[0]=='d')
        {
          scanf("%d",&x);
          treeDelete(search(root,x));
        }

    }
  return 0;
}

void Insert(int p)
{
  Node x=root;
  Node y=NIL;
  Node z;

  z=(Node )malloc(sizeof(struct node));
  z->key=p;
  z->left=NIL;
  z->right=NIL;

  while(x!=NIL)
    {
      y=x;
      if(z->key < x->key) x=x->left;
      else x=x->right;

z->parent=y;
    }
  if(y==NIL) root=z;
  else if(z->key < y->key) y->left=z;
  else  y->right=z;
}

void inorder(Node x)
{
  if(x==NIL) return;
  inorder(x->left);
  printf(" %d",x->key);
  inorder(x->right);
}

void preorder(Node x)
{
  if(x==NIL) return;
  printf(" %d",x->key);
  preorder(x->left);
  preorder(x->right);
}
Node search(Node u,int k)
{
  while(u!=NIL && k!=u->key)
    {
      if(k < u->key) u=u->left;
      else u=u->right;
    }
  return u;
}

Node treeSuccessor(Node *x)
{
  Node *y;
  if(x->right!=NIL) return treeMinimum(x->right);
  *y=x->parent;
  while(y!=NIL&&x==y->right)
    {
      x=y;
      y=y->parent;
    }
  return y;
}
void treeDelete(Node *z)
{
  Node *y,*x;

  if(z->left==NIL||z->right==NIL) y=z;
  else y=treeSuccessor(z);
  if(y->left!=NIL)  x=y->left;
  else  x=y->right;

  if(x!=NIL)  x->parent=y->parent;
  if(y->parent==NIL) root=x;
  else
    {
      if(y==y->parent->left)  y->parent->left=x;
      else y->parent->right=x;
    }
  if(y!=z)  z->key=y->key;

  free(y);
}

Node treeMinimum(Node *x)
{
  while(x->left!=NIL) x=x->left;
  return x;
}

Compiler output:
main.c: In function 'main':
main.c:51:23: error: 'x' undeclared (first use in this function)
   51 |           scanf("%d",&x);
      |                       ^
main.c:51:23: note: each undeclared identifier is reported only once for each function it appears in
main.c: At top level:
main.c:108:6: error: conflicting types for 'treeSuccessor'; have 'struct node *(struct node **)'
  108 | Node treeSuccessor(Node *x)
      |      ^~~~~~~~~~~~~
main.c:17:6: note: previous declaration of 'treeSuccessor' with type 'struct node *(struct node *)'
   17 | Node treeSuccessor(Node );
      |      ^~~~~~~~~~~~~
main.c: In function 'treeSuccessor':
main.c:111:7: error: 'x' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  111 |   if(x->right!=NIL) return treeMinimum(x->right);
      |       ^~
      |      -
      |      (*x)
main.c:111:41: error: 'x' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  111 |   if(x->right!=NIL) return treeMinimum(x->right);
      |                                         ^~
      |                                        -
      |                                        (*x)
main.c:112:7: error: 'x' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  112 |   *y=x->parent;
      |       ^~
      |      -
      |      (*x)
main.c:113:21: error: 'y' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  113 |   while(y!=NIL&&x==y->right)
      |                     ^~
      |                    -
      |                    (*y)
main.c:116:10: error: 'y' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  116 |       y=y->parent;
      |          ^~
      |         -
      |         (*y)
main.c:118:10: error: returning 'struct node **' from a function with incompatible return type 'Node' {aka 'struct node *'} [-Wincompatible-pointer-types]
  118 |   return y;
      |          ^
main.c: At top level:
main.c:120:6: error: conflicting types for 'treeDelete'; have 'void(struct node **)'
  120 | void treeDelete(Node *z)
      |      ^~~~~~~~~~
main.c:18:6: note: previous declaration of 'treeDelete' with type 'void(struct node *)'
   18 | void treeDelete(Node );
      |      ^~~~~~~~~~
main.c: In function 'treeDelete':
main.c:124:7: error: 'z' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  124 |   if(z->left==NIL||z->right==NIL) y=z;
      |       ^~
      |      -
      |      (*z)
main.c:124:21: error: 'z' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  124 |   if(z->left==NIL||z->right==NIL) y=z;
      |                     ^~
      |                    -
      |                    (*z)
main.c:125:9: error: assignment to 'struct node **' from incompatible pointer type 'Node' {aka 'struct node *'} [-Wincompatible-pointer-types]
  125 |   else y=treeSuccessor(z);
      |         ^
main.c:126:7: error: 'y' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  126 |   if(y->left!=NIL)  x=y->left;
      |       ^~
      |      -
      |      (*y)
main.c:126:24: error: 'y' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  126 |   if(y->left!=NIL)  x=y->left;
      |                        ^~
      |                       -
      |                       (*y)
main.c:127:12: error: 'y' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  127 |   else  x=y->right;
      |            ^~
      |           -
      |           (*y)
main.c:129:16: error: 'x' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  129 |   if(x!=NIL)  x->parent=y->parent;
      |                ^~
      |               -
      |               (*x)
main.c:129:26: error: 'y' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  129 |   if(x!=NIL)  x->parent=y->parent;
      |                          ^~
      |                         -
      |                         (*y)
main.c:130:7: error: 'y' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  130 |   if(y->parent==NIL) root=x;
      |       ^~
      |      -
      |      (*y)
main.c:130:26: error: assignment to 'Node' {aka 'struct node *'} from incompatible pointer type 'struct node **' [-Wincompatible-pointer-types]
  130 |   if(y->parent==NIL) root=x;
      |                          ^
main.c:133:14: error: 'y' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  133 |       if(y==y->parent->left)  y->parent->left=x;
      |              ^~
      |             -
      |             (*y)
main.c:133:32: error: 'y' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  133 |       if(y==y->parent->left)  y->parent->left=x;
      |                                ^~
      |                               -
      |                               (*y)
main.c:134:13: error: 'y' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  134 |       else y->parent->right=x;
      |             ^~
      |            -
      |            (*y)
main.c:136:14: error: 'z' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  136 |   if(y!=z)  z->key=y->key;
      |              ^~
      |             -
      |             (*z)
main.c:136:21: error: 'y' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  136 |   if(y!=z)  z->key=y->key;
      |                     ^~
      |                    -
      |                    (*y)
main.c: At top level:
main.c:141:6: error: conflicting types for 'treeMinimum'; have 'struct node *(struct node **)'
  141 | Node treeMinimum(Node *x)
      |      ^~~~~~~~~~~
main.c:19:6: note: previous declaration of 'treeMinimum' with type 'struct node *(struct node *)'
   19 | Node treeMinimum(Node);
      |      ^~~~~~~~~~~
main.c: In function 'treeMinimum':
main.c:143:10: error: 'x' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  143 |   while(x->left!=NIL) x=x->left;
      |          ^~
      |         -
      |         (*x)
main.c:143:26: error: 'x' is a pointer to pointer; did you mean to dereference it before applying '->' to it?
  143 |   while(x->left!=NIL) x=x->left;
      |                          ^~
      |                         -
      |                         (*x)
main.c:144:10: error: returning 'struct node **' from a function with incompatible return type 'Node' {aka 'struct node *'} [-Wincompatible-pointer-types]
  144 |   return x;
      |          ^