class Bintree
{
class Node
{
int value;
Node left;
Node right;
public Node(final int v)
{
value = v;
}
public void addNode(final Node n)
{
if (n.value < value)
{
if (left == null)
{
left = n;
}
else
{
left.addNode(n);
}
}
else
{
if (right == null)
{
right = n;
}
else
{
right.addNode(n);
}
}
}
public Node delNode(final int v)
{
if (v < value)
{
if (left == null)
{
return this;
}
else
{
left = left.delNode(v);
return this;
}
}
else if (value < v)
{
if (right == null)
{
return this;
}
else
{
right = right.delNode(v);
return this;
}
}
else
{
if (left == null)
{
return right;
}
else if (right == null)
{
return left;
}
else
{
value = right.minValue();
right = right.delMin();
return this;
}
}
}
int minValue()
{
if (left == null)
{
return value;
}
else
{
return left.minValue();
}
}
Node delMin()
{
if (left == null)
{
return right;
}
else
{
left = left.delMin();
return this;
}
}
}
Node root;
public void insert(final int v)
{
final Node n = new Node(v);
if (root == null)
{
root = n;
}
else
{
root.addNode(n);
}
}
public void remove(final int v)
{
if (root == null)
{
return;
}
else
{
root = root.delNode(v);
}
}
}
Y2xhc3MgQmludHJlZQp7CiAgICBjbGFzcyBOb2RlCiAgICB7CiAgICAgICAgaW50IHZhbHVlOwogICAgICAgIE5vZGUgbGVmdDsKICAgICAgICBOb2RlIHJpZ2h0OwoKICAgICAgICBwdWJsaWMgTm9kZShmaW5hbCBpbnQgdikKICAgICAgICB7CiAgICAgICAgICAgIHZhbHVlID0gdjsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyB2b2lkIGFkZE5vZGUoZmluYWwgTm9kZSBuKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKG4udmFsdWUgPCB2YWx1ZSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYgKGxlZnQgPT0gbnVsbCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBsZWZ0ID0gbjsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBsZWZ0LmFkZE5vZGUobik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZiAocmlnaHQgPT0gbnVsbCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICByaWdodCA9IG47CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgcmlnaHQuYWRkTm9kZShuKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgcHVibGljIE5vZGUgZGVsTm9kZShmaW5hbCBpbnQgdikKICAgICAgICB7CiAgICAgICAgICAgIGlmICh2IDwgdmFsdWUpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmIChsZWZ0ID09IG51bGwpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHRoaXM7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgbGVmdCA9IGxlZnQuZGVsTm9kZSh2KTsKICAgICAgICAgICAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmICh2YWx1ZSA8IHYpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmIChyaWdodCA9PSBudWxsKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHJldHVybiB0aGlzOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHJpZ2h0ID0gcmlnaHQuZGVsTm9kZSh2KTsKICAgICAgICAgICAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmIChsZWZ0ID09IG51bGwpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHJpZ2h0OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZSBpZiAocmlnaHQgPT0gbnVsbCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICByZXR1cm4gbGVmdDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICB2YWx1ZSA9IHJpZ2h0Lm1pblZhbHVlKCk7CiAgICAgICAgICAgICAgICAgICAgcmlnaHQgPSByaWdodC5kZWxNaW4oKTsKICAgICAgICAgICAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgaW50IG1pblZhbHVlKCkKICAgICAgICB7CiAgICAgICAgICAgIGlmIChsZWZ0ID09IG51bGwpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHJldHVybiB2YWx1ZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHJldHVybiBsZWZ0Lm1pblZhbHVlKCk7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIE5vZGUgZGVsTWluKCkKICAgICAgICB7CiAgICAgICAgICAgIGlmIChsZWZ0ID09IG51bGwpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHJldHVybiByaWdodDsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGxlZnQgPSBsZWZ0LmRlbE1pbigpOwogICAgICAgICAgICAgICAgcmV0dXJuIHRoaXM7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgfQoKICAgIE5vZGUgcm9vdDsKCiAgICBwdWJsaWMgdm9pZCBpbnNlcnQoZmluYWwgaW50IHYpCiAgICB7CiAgICAgICAgZmluYWwgTm9kZSBuID0gbmV3IE5vZGUodik7CgogICAgICAgIGlmIChyb290ID09IG51bGwpCiAgICAgICAgewogICAgICAgICAgICByb290ID0gbjsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgcm9vdC5hZGROb2RlKG4pOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgdm9pZCByZW1vdmUoZmluYWwgaW50IHYpCiAgICB7CiAgICAgICAgaWYgKHJvb3QgPT0gbnVsbCkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgcm9vdCA9IHJvb3QuZGVsTm9kZSh2KTsKICAgICAgICB9CiAgICB9Cn0K