import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
{
BTree<Integer> btree = new BTree<Integer>();
// ①二分木にノードを追加する
// ②二分木からノードを削除する
}
}
class BTree<T extends Comparable>
{
private class Node<T extends Comparable>
{
T item = null;
Node<T> left = null;
Node<T> right = null;
void add(T item) {
if (this.item == null) {
this.item = item;
} else {
if (this.item.compareTo(item) < 0) {
if (left == null) {
left = new Node<T>();
}
left.add(item);
} else {
if (right == null) {
right = new Node<T>();
}
right.add(item);
}
}
}
boolean delete(T item) {
if (this.item != null) {
if (this.item.compareTo(item) == 0) {
this.item = null;
return true;
}
}
if (left != null) {
if (left.delete(item)) {
return true;
}
}
if (right != null) {
if (right.delete(item)) {
return true;
}
}
return false;
}
}
private Node<T> root = new Node<T>();
public void add(T item) {
root.add(item);
}
public boolean delete(T item) {
return root.delete(item);
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBJZGVvbmUKewoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4gKFN0cmluZ1tdIGFyZ3MpIHRocm93cyBqYXZhLmxhbmcuRXhjZXB0aW9uCgl7CgkJQlRyZWU8SW50ZWdlcj4gYnRyZWUgPSBuZXcgQlRyZWU8SW50ZWdlcj4oKTsKCQkKCQkvLyDikaDkuozliIbmnKjjgavjg47jg7zjg4njgpLov73liqDjgZnjgosKCQlidHJlZS5hZGQoSW50ZWdlci52YWx1ZU9mKDIpKTsKCQkKCQkvLyDikaHkuozliIbmnKjjgYvjgonjg47jg7zjg4njgpLliYrpmaTjgZnjgosKCQlidHJlZS5kZWxldGUoSW50ZWdlci52YWx1ZU9mKDIpKTsKCX0KfQoKCmNsYXNzIEJUcmVlPFQgZXh0ZW5kcyBDb21wYXJhYmxlPgp7Cglwcml2YXRlIGNsYXNzIE5vZGU8VCBleHRlbmRzIENvbXBhcmFibGU+Cgl7CgkJVCBpdGVtID0gbnVsbDsKCQlOb2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