bool is_bst(const vector<Node>& tree, int i, long long min_key, long long max_key) {
if (i == -1) {
return true;
}
if (tree[i].key < min_key || tree[i].key >= max_key) {
return false;
}
if (!is_bst(tree, tree[i].left, min_key, tree[i].key)) {
return false;
}
if (tree[i].key == min_key) {
int right = tree[i].right;
while (tree[right].left != -1) {
right = tree[right].left;
}
if (tree[right].key <= tree[i].key) {
return false;
}
}
return is_bst(tree, tree[i].right, tree[i].key, max_key);
}
bool IsBinarySearchTree(const vector<Node>& tree) {
if (tree.empty()) {
return true;
}
return is_bst(tree, 0, numeric_limits<long long>::min(), numeric_limits<long long>::max());
}
#include <iostream>
using namespace std;
struct Node {
int val;
Node* left;
Node* right;
Node(int val) {
this->val = val;
left = right = NULL;
}
};
void insert(Node*& root, int val) {
if (root == NULL) {
root = new Node(val);
}
else if (val < root->val) {
insert(root->left, val);
}
else if (val > root->val) {
insert(root->right, val);
}
}
bool find(Node* root, int val) {
if (root == NULL) {
return false;
}
else if (val == root->val) {
return true;
}
else if (val < root->val) {
return find(root->left, val);
}
else {
return find(root->right, val);
}
}
Node* findMin(Node* root) {
while (root->left != NULL) {
root = root->left;
}
return root;
}
void remove(Node*& root, int val) {
if (root == NULL) {
return;
}
else if (val < root->val) {
remove(root->left, val);
}
else if (val > root->val) {
remove(root->right, val);
}
else if (root->left != NULL && root->right != NULL) {
root->val = findMin(root->right)->val;
remove(root->right, root->val);
}
else {
Node* oldNode = root;
root = (root->left != NULL) ? root->left : root->right;
delete oldNode;
}
}
int main() {
int n;
cin >> n;
Node* root = NULL;
for (int i = 0; i < n; i++) {
char op;
int val;
cin >> op >> val;
if (op == '+') {
insert(root, val);
}
else if (op == '-') {
remove(root, val);
}
else if (op == '?') {
if (find(root, val)) {
cout << "Found" << endl;
}
else {
cout << "Not found" << endl;
}
}
}
return 0;
}