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: Time Limit Exceeded)

#include <stdio.h>
#include <string.h>
#define N 500000
int count,ans;

struct node
{
  int key;
  int pare;
  int left;
  int right;
};

struct node bi[N];

void insert(int z)
{
  int x,y;
  int num=0;

  y=-1;
  x=bi[0].key;
//  printf("while start\n");
  while(1)
  {
    if(x==-1) break;
    y = num;
    if(z<x)
    {
      num = bi[num].left;
      if(num!=-1) x = bi[num].key;
      else
      {
        x=-1;
      }
    }
    else
    {
      num = bi[num].right;
      if(num!=-1) x = bi[num].key;
      else
      {
        x=-1;
      }
    }
  }
//  printf("while end\n");
  bi[count].pare=y;
  bi[count].key=z;

  if(y==-1)
  {
    bi[0].key=z;
  }
  else if(z<bi[y].key)
  {
    bi[y].left = count;
  }
  else
  {
    bi[y].right = count;
  }

  count++;
}

void pre(int now)
{
  printf(" %d",bi[now].key);
  if(bi[now].left!=-1)  pre(bi[now].left);
  if(bi[now].right!=-1)  pre(bi[now].right);
}

void in(int now)
{
  if(bi[now].left!=-1) in(bi[now].left);
  printf(" %d",bi[now].key);
  if(bi[now].right!=-1) in(bi[now].right);
}

int doin(int now,int flag,int key)
{
  if(bi[now].left!=-1) doin(bi[now].left,flag,key);
  if(flag==1)
  {
//    printf("doin:%d\n",now);
    ans = now;
    flag = 2;
  }
  if(bi[now].key==key) flag=1;
  if(bi[now].right!=-1) doin(bi[now].right,flag,key);
}


void print()
{
  in(0);
  printf("\n");
  pre(0);
  printf("\n");
}

int find(int key,int flag)
{
  int i=0;
  while(1)
  {
    if(bi[i].key==key)
    {
      if(flag==1)printf("yes\n");
      return i;
    }
    if(bi[i].pare==-1) break;
    i = bi[i].pare;
  }
//  printf("start:%d num:%d\n",i,bi[i].key);
  while(1)
  {
    if(bi[i].key<key)
    {
      i=bi[i].right;
      if(i==-1)
      {
        printf("no\n");
        return -1;
      }
    }
    else
    {
      i=bi[i].left;
      if(i==-1)
      {
        printf("no\n");
        return -1;
      }
    }
    if(bi[i].key==key)
    {
      if(flag==1)printf("yes\n");
      return i;
    }
  }
}

void delete(int key,int flag)
{
  int i,j;
  int pare,left,right;
  if(flag==-1)
  {
    i = find(key,0);
  }
  else
  {
    i = flag;
  }
//  printf("delete:%d,%d\n",key,i);
  pare = bi[i].pare;
  left = bi[i].left;
  right = bi[i].right;
  if(left==-1 && right==-1)
  {
    if(bi[pare].key==key)
    {
      if(bi[pare].left==i) bi[pare].left=-1;
      else
      {
        bi[pare].right=-1;
      }
    }
    else if(bi[pare].key>key)
    {
      bi[pare].left=-1;
    }
    else
    {
      bi[pare].right=-1;
    }
  }
  else if(left!=-1 && right==-1)
  {
    bi[left].pare = pare;
    if(bi[pare].key>key)
    {
      bi[pare].left=left;
    }
    else
    {
      bi[pare].right=left;
    }
  }
  else if(left==-1 && right!=-1)
  {
    bi[right].pare = pare;
    if(bi[pare].key>key)
    {
      bi[pare].left=right;
    }
    else
    {
      bi[pare].right=right;
    }
  }
  else
  {
    doin(0,0,bi[i].key);
    j = ans;
    if(bi[ans].left!=-1)j = bi[ans].left;
//    printf("j:%d key:%d i:%d key:%d\n",j,bi[j].key,i,bi[i].key);
    bi[i].key=bi[j].key;
    delete(bi[j].key,j);
  }
}


int main(void)
{
  int n,i;
  int num;
  char comm[10];
  count=0;

  for(i=0;i<N;i++)
  {
    bi[i].key = -1;
    bi[i].pare = -1;
    bi[i].left = -1;
    bi[i].right = -1;
  }
//  printf("set ok\n");

  scanf("%d",&n);

  for(i=0;i<n;i++)
  {
//    printf("comm:");
    scanf("%s",comm);
//    printf("comm:%s\n",comm);
    if(strcmp(comm,"insert")==0)
    {
//      printf("scan:");
      scanf("%d",&num);
      insert(num);
    }
    else if(strcmp(comm,"find")==0)
    {
      scanf("%d",&num);
      find(num,1);
    }
    else if(strcmp(comm,"delete")==0)
    {
      scanf("%d",&num);
      delete(num,-1);
    }
    else
    {
//      printf("print\n");
      print();
    }
  }
  return 0;
}