Problem:
Binary Search Tree III

Write a program which performs the following operations to a binary search tree $T$ by adding delete operation to B: Binary Search Tree II.

insert $k$: Insert a node containing $k$ as key into $T$.

find $k$: Report whether $T$ has a node containing $k$.

delete $k$: Delete a node containing $k$.

print: Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively.

The operation delete $k$ for deleting a given node $z$ containing key $k$ from $T$ can be implemented by an algorithm which considers the following cases:

If $z$ has no children, we modify its parent $z.p$ to replace $z$ with NIL as its child (delete $z$).

If $z$ has only a single child, we "splice out" $z$ by making a new link between its child and its parent.

If $z$ has two children, we splice out $z$'s successor $y$ and replace $z$'s key with $y$'s key.

Input

In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by insert $k$, find $k$, delete $k$ or print are given.

Output

For each find $k$ operation, print "yes" if $T$ has a node containing $k$, "no" if not.

In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key

Constraints

The number of operations $\leq 500,000$

The number of print operations $\leq 10$.

$-2,000,000,000 \leq key \leq 2,000,000,000$

The height of the binary tree does not exceed 100 if you employ the above pseudo code.

The keys in the binary search tree are all different.

Sample Input 1

18
insert 8
insert 2
insert 3
insert 7
insert 22
insert 1
find 1
find 2
find 3
find 4
find 5
find 6
find 7
find 8
print
delete 3
delete 7
print

Sample Output 1

yes
yes
yes
no
no
no
yes
yes
1 2 3 7 8 22
8 2 1 3 7 22
1 2 8 22
8 2 1 22

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in C (judge verdict: Accepted)

#include <stdio.h>
#include <string.h>
  
#define Max 500000
  
void insert(int); 
void find(int, int);
int successor(int);
void delete(int);
void Preorder(int);    /* ??????????????? */
void Inorder(int);     /* ??????????????? */
  
  
typedef struct{    /* p = parent, l = child, s = brother */
  int key;
  int p;
  int l[2];
} Node;
  
Node T[Max];
  
int main(){
  int i, n, k;
  char s[7];
  
  scanf("%d", &n);
  
  T[0].key = -1;
  
   
  for(i = 0; i < n; i++){
    T[i].l[0] = -1;
    T[i].l[1] = -1;
  
    scanf("%s", s);
  
    if(strcmp(s, "insert") == 0){
      scanf("%d", &T[i].key);
      insert(i);
    }
    else if(strcmp(s, "print") == 0){
      Inorder(0);
      printf("\n");
      Preorder(0);
      printf("\n");
    }
    else if(strcmp(s, "find") == 0){
      scanf("%d", &k);
      find(0, k);
    }
    else if(strcmp(s, "delete") == 0){
      scanf("%d", &k);
      delete(k);
    }
  }
    
  return 0;
}
  
  
void insert(int z){
  int x, y;
  
  y = -1;
  x = 0;
  
  if(z != 0){
    while(x != -1){
      y = x;
      if(T[z].key < T[x].key) x = T[x].l[0];
      else x = T[x].l[1];
    }
  }
  
  T[z].p = y;
  
  if(y != -1){
    if(T[z].key < T[y].key) T[y].l[0] = z;
    else T[y].l[1] = z;
  }
}
  
void find(int x, int k){
  
  if(x == -1) printf("no\n");
  else if(T[x].key == k) printf("yes\n");
  else if(k < T[x].key) find(T[x].l[0], k);
  else find(T[x].l[1], k);
}
  
int successor(int x){
  int y;
  
  if(T[x].l[1] != -1){
    x = T[x].l[1];
    while(T[x].l[0] != -1) x = T[x].l[0];
    return x;
  }
    
  else {
    y = T[x].p;
    while(y != -1 && x == T[y].l[1]){
      x = y;
      y = T[y].p;
    }
    return y;
  }
}
  
void delete(k){
  int x, y, z = 0;
  
  while(1){
    if(T[z].key == k) break;
    else if(k < T[z].key) z = T[z].l[0];
    else z = T[z].l[1];
  }
  
  if(T[z].l[0] == -1 || T[z].l[1] == -1) y = z;
  else y = successor(z);
  
  if(T[y].l[0] != -1) x = T[y].l[0];
  else x = T[y].l[1];
  
  if(x != -1) T[x].p = T[y].p;
  
  if(T[y].p != -1){
    if(y == T[T[y].p].l[0]) T[T[y].p].l[0] = x;
    else T[T[y].p].l[1] = x;
  }
  
  if(y !=  z) T[z].key = T[y].key;
  
}
  
  
void Preorder(int a){
  printf(" %d", T[a].key);
  
  if(T[a].l[0] != -1) Preorder(T[a].l[0]);
  if(T[a].l[1] != -1) Preorder(T[a].l[1]);
}
  
  
void Inorder(int a){
  if(T[a].l[0] == -1){
    printf(" %d", T[a].key);
    if(T[a].l[1] != -1) Inorder(T[a].l[1]);
  }
  else{
    Inorder(T[a].l[0]);
    printf(" %d", T[a].key);
    if(T[a].l[1] != -1) Inorder(T[a].l[1]);
  }
}