Pagini recente » Cod sursa (job #2514259) | Cod sursa (job #3151221) | Cod sursa (job #3154327) | Cod sursa (job #1131742) | Cod sursa (job #1757399)
#include <iostream>
#include <vector>
#include <fstream>
#include <set>
#include <algorithm>
#include <cstdlib>
using namespace std;
ifstream in("hashuri.in"),ing("grader_test5.ok");
ofstream out("hashuri.out");
const int numa =269916016;
class BST
{
private:
struct node
{
int key;
node* left,*right;
node(int x,node* lt,node* rt):key(x),left{lt},right{rt}{};
};
node * root = nullptr;
bool search(int x, node * root)
{
if(root == nullptr)
return false;
else if(x < root->key)
return search(x,root->left);
else if(x > root->key)
return search(x,root->right);
else
return true;
}
node * findMin(node * &root)
{
if(root == nullptr)
return nullptr;
else if(root->left == nullptr)
return root;
else return findMin(root->left);
}
void insert(int x, node* &root)
{
if(root == nullptr)
root = new node{x,nullptr,nullptr};
else if(x < root->key)
insert(x,root->left);
else if(x > root->key)
insert(x,root->right);
else
;//nu facem nik la duplicat
}
void remove(int x, node* &root)
{
if(root == nullptr)
return ;
else if(x < root->key)
remove(x,root->left);
else if(x > root->key)
remove(x,root->right);
else if(root->right != nullptr && root->left != nullptr)//cazul cand are 2 copii
{
root->key = findMin(root->right)->key;
remove(root->key,root->right);
}
else //celelalte cazuri cand are unu dreapta/unu stanga/niciunu
{
node * old = root;
root = (root->left != nullptr)?root->left:root->right;
delete old;
}
}
public:
void insert(int x)
{
insert(x,root);
}
void erase(int x)
{
remove(x,root);
}
bool find(int x)
{
return search(x,root);
}
};
class RBT //Red Black Tree
{
};
int main()
{
BST bst;
set<int> um;
int n,x,op,nr,nr2,j=0;
in >> n;
for(int i = 0 ; i< n ; i++)
{
in >> op >> x;
switch(op)
{
case 1: bst.insert(x);
//um.insert(x);
break;
case 2: //um.erase(x);
bst.erase(x);
break;
case 3: nr = bst.find(x);
//nr2 = (um.find(x)!=um.end())?1:0;
out<<nr<<'\n';
//ing>> nr2;
//j++;
//if(nr != nr2)
// cout<<"linia "<<j<<" "<<nr<<"!="<<nr2<<"elementul "<<x<<'\n';
break;
}
//bst.insert(x);
}
//bst.showSorted();z
}