Pagini recente » Cod sursa (job #3168568) | Cod sursa (job #2228503) | Cod sursa (job #2792972) | Cod sursa (job #2658845) | Cod sursa (job #2897726)
#include <bits/stdc++.h>
using namespace std;
ifstream f("zeap.in");
ofstream g("zeap.out");
#define cin f
#define cout g
struct Node
{
int key;
Node *left;
Node *right;
Node *father;
};
Node *createNode(int key)
{
Node *node = new Node();
node->key = key;
node->left = NULL;
node->right = NULL;
node->father = NULL;
return node;
}
void search(Node *node)
{
if(node == NULL)
return;
queue < Node * > q;
q.push(node);
while(! q.empty())
{
Node *temp = q.front();
q.pop();
cout<<"Node: "<<temp->key<<'\n';
if(temp->left != NULL)
{
q.push(temp->left);
cout<<"Left: "<<temp->left->key<<'\n';
}
else
cout<<"Left: NULL\n";
if(temp->right != NULL)
{
q.push(temp->right);
cout<<"Right: "<<temp->right->key<<'\n';
}
else
cout<<"Right: NULL\n";
}
}
struct Tree
{
int n = 0;
Node *root = NULL;
};
Node *TreeMinimum(Node *node)
{
while(node->left != NULL)
node = node->left;
return node;
}
Node *TreeMaximum(Node *node)
{
while(node->right != NULL)
node = node->right;
return node;
}
Node *TreeSearch(Node *node, int x)
{
while(node != NULL and x != node->key)
if(x < node->key)
node = node->left;
else
node = node->right;
return node;
}
Node *TreeSuccessor(Node *node1)
{
if(node1->right != NULL)
return TreeMinimum(node1->right);
Node *node2 = node1->father;
while(node2 != NULL and node1 == node2->right)
{
node1 = node2;
node2 = node2->father;
}
return node2;
}
Node *TreePredecessor(Node *node1)
{
if(node1->left != NULL)
return TreeMaximum(node1->left);
Node *node2 = node1->father;
while(node2 != NULL and node1 == node2->left)
{
node1 = node2;
node2 = node2->father;
}
return node2;
}
void TreeInsert(Tree &T, int key)
{
T.n ++;
Node *newNode = createNode(key);
Node *father = NULL, *root = T.root;
while(root != NULL)
{
father = root;
if(newNode->key < root->key)
root = root->left;
else
root = root->right;
}
newNode->father = father;
if(father == NULL)
T.root = newNode;
else if(newNode->key < father->key)
father->left = newNode;
else
father->right = newNode;
}
void TreeDelete(Tree &T, Node *node)
{
T.n --;
Node *x, *y;
if(node->left == NULL or node->right == NULL)
y = node;
else
y = TreeSuccessor(node);
if(y->left != NULL)
x = y->left;
else
x = y->right;
if(x != NULL)
x->father = y->father;
if(y->father == NULL)
T.root = x;
else if(y == y->father->left)
y->father->left = x;
else
y->father->right = x;
if(y != node)
node->key = y->key;
delete y;
}
int main()
{
Tree t;
string s;
while(cin >> s)
{
int x;
if(s[0] == 'I')
{
cin >> x;
TreeInsert(t, x);
}
else if(s[0] == 'S')
{
cin >> x;
Node *node = TreeSearch(t.root, x);
if(node == NULL)
cout<<-1<<'\n';
else
TreeDelete(t, node);
}
else if(s[0] == 'C')
{
cin >> x;
Node *node = TreeSearch(t.root, x);
if(node == NULL)
cout<<0<<'\n';
else
cout<<1<<'\n';
}
else if(s[1] == 'A')
{
if(t.n > 1)
cout<<TreeMaximum(t.root)->key - TreeMinimum(t.root)->key<<'\n';
else
cout<<-1<<'\n';
}
else
{
if(t.n > 1)
{
int mn = 1e9 + 1;
Node *first, *second;
first = TreeMinimum(t.root);
second = TreeSuccessor(first);
while(second != NULL)
{
mn = min(mn, second->key - first->key);
first = second;
second = TreeSuccessor(first);
}
cout<<mn<<'\n';
}
else
cout<<-1<<'\n';
}
}
return 0;
}