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 Go (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

type Tree struct {
	key   int
	left  *Tree
	right *Tree
}

func (t *Tree) insert(n int) *Tree {
	if t == nil {
		return &Tree{key: n}
	}

	if n < t.key {
		t.left = t.left.insert(n)
	} else {
		t.right = t.right.insert(n)
	}
	return t
}

func (t *Tree) find(n int) bool {
	switch {
	case t == nil:
		return false
	case t.key == n:
		return true
	case n < t.key:
		return t.left.find(n)
	default:
		return t.right.find(n)
	}
}

func (t *Tree) delete(n int) *Tree {
	if t == nil {
		return nil
	}

	if t.key == n {
		switch {
		case t.left == nil && t.right == nil:
			return nil
		case t.left != nil && t.right != nil:
			t.right, t.key = t.right.deleteFirst()
			return t
		case t.left == nil:
			return t.right
		default:
			return t.left
		}
	}

	if n < t.key {
		t.left = t.left.delete(n)
	} else {
		t.right = t.right.delete(n)
	}
	return t
}

// deleteFirst deletes the first node as in-ordered.
// t should not be nil.
func (t *Tree) deleteFirst() (*Tree, int) {
	if t.left == nil {
		return nil, t.key
	}
	left, key := t.left.deleteFirst()
	t.left = left
	return t, key
}

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	m := nextInt()
	var t *Tree

	for i := 0; i < m; i++ {
		switch scn.Scan(); scn.Text() {
		case "insert":
			t = t.insert(nextInt())
		case "find":
			if t.find(nextInt()) {
				fmt.Fprintln(w, "yes")
			} else {
				fmt.Fprintln(w, "no")
			}
		case "delete":
			t = t.delete(nextInt())
		case "print":
			inorder(w, t)
			fmt.Fprintln(w)
			preorder(w, t)
			fmt.Fprintln(w)
		}
	}

	w.Flush()
}

func preorder(w io.Writer, t *Tree) {
	if t == nil {
		return
	}
	print(w, t.key)
	preorder(w, t.left)
	preorder(w, t.right)
}

func inorder(w io.Writer, t *Tree) {
	if t == nil {
		return
	}
	inorder(w, t.left)
	print(w, t.key)
	inorder(w, t.right)
}

func print(w io.Writer, n int) {
	fmt.Fprintf(w, " %d", n)
}