Pagini recente » Cod sursa (job #2107535) | Cod sursa (job #1685465) | Cod sursa (job #3292273) | Cod sursa (job #2073412) | Cod sursa (job #1756831)
#include <iostream>
#include <vector>
#include <fstream>
#include <memory>
using namespace std;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
class BST //Binary Search Tree
{
private :
vector<int> v;
struct Node;
typedef shared_ptr<Node> Nptr;
struct Node
{
Nptr father,left,right;
int value;
Node(int x):father(nullptr),left(nullptr),right(nullptr),value(x){};
};
Nptr root;
Nptr& getMax(Nptr &root)
{
if(root->right != nullptr)
return getMax(root->right);
return root;
}
Nptr& getMin(Nptr &root)
{
if(root->left != nullptr)
return getMin(root->left);
return root;
}
Nptr find(Nptr &root,int x)
{
if(root == nullptr)
return nullptr;
if(root->value == x)
return root;
return find((root->value <x)?root->right:root->left,x);
}
void insert(Nptr &root, Nptr &node)
{
if(root->value > node->value)
if(root->left != nullptr)
insert(root->left,node);
else
{
node->father = root;
root->left = node;
}
else
if(root->right != nullptr)
insert(root->right,node);
else
{
node->father = root;
root->right = node;
}
}
void erase(Nptr &root,int x)//3 cazuri
{
if(root->value == x)//daca am gasit nodul
{
Nptr &father = root->father;
if(root->left != nullptr && root->right != nullptr) //cazul 3 cand avem ambii copii
{
Nptr &maxFleft = getMax(root->left); //luam maximul de pe copilul stang
swap(root->value,maxFleft->value); //schimbam valorile dintre maximul si valoarea curenta
erase(root->left,x); //si stergem x din arborele stang deoarece se reduce sigur
} //la cazul cand avem un singur nod copil
else if(root->right == nullptr) //cazul 2 cand avem 1 copil (partea stanga)
(father->value < root->value)?father->right = root->left:father->left = root->left;
else if(root->left == nullptr) //cazul 2 cand avem 1 copil (partea dreapta)
(father->value < root->value)?father->right = root->right:father->left = root->right;
else if(root->left == root->right && root->right == nullptr)//cazu 1 cand nu avem copii
(father->value < root->value)?father->right = nullptr:father->left =nullptr;
}
else erase((root->value<x)?root->right:root->left,x);//cautam in partea corespunzatoare
}
void srd(Nptr &node)
{
if(node != nullptr)
{
srd(node->left);
v.push_back(node->value);
srd(node->right);
}
}
public:
BST():root(nullptr){};
void insert(int x)
{
Nptr nod = make_shared<Node>(x);
if(root == nullptr)//case root
root = nod;
else
insert( root,nod);
}
void erase(int x)
{
Nptr gasit = find(root,x);
if(gasit != nullptr)
erase(gasit,x);
}
int getMax()
{
return getMax(root)->value;
}
int getMin()
{
return getMin(root)->value;
}
bool find(int x)
{
return (find(root,x)!=nullptr)?true:false;
}
vector<int> getSorted()
{
v.clear();
srd(root);
return v;
}
};
class RBT //Red Black Tree
{
};
class AVL //Blanced BST
{
};
class B
{
};
int main()
{
BST bsttree;
int n,op,x;
in >> n;
while(n--)
{
in >> op >> x;
switch(op)
{
case 1: bsttree.insert(x); break;
case 2: bsttree.erase(x); break;
case 3: out<<(int)bsttree.find(x)<<'\n'; break;
}
}
return 0;
}