Cod sursa(job #1757339)

Utilizator sulzandreiandrei sulzandrei Data 14 septembrie 2016 20:59:34
Problema Hashuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 4.59 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <set>
#include <algorithm>
#include <cstdlib>
using namespace std;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
class BST  //Binary Search Tree
{
private :
    vector<int> v;
    struct Node
    {
        int freq;
        Node *father,*left,*right;
        int value;
        Node(int x):freq(1),father(nullptr),left(nullptr),right(nullptr),value(x){};
    };
    Node*& getMax(Node* &root)
    {
        if(root->right != nullptr)
            return getMax(root->right);
        return root;
    }
    Node*& getMin(Node* &root)
    {
        if(root->left != nullptr)
            return getMin(root->left);
        return root;
    }
    Node* find(Node* &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(Node* &root, Node* &node)
    {
        if(root->value == node->value)
        {
            root->freq++;
            return;
        }
        if(node->value < root->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(Node* &root,int x)//3 cazuri
    {
        if(root->value == x)//daca am gasit nodul
        {
            if(root->freq>1)
                root->freq--;
            else
            {
                Node* father = root->father;
                if(root->left != nullptr && root->right != nullptr)    //cazul 3 cand avem ambii copii
                {
                    Node* maxFleft = getMax(root->left);              //luam maximul de pe copilul stang
                    root->value = maxFleft->value ;                   //deci ramane sa stergem nodul maxim
                    root->freq = maxFleft->freq;
                    maxFleft->freq = 0;
                    erase(maxFleft,maxFleft->value);                               //si stergem x din arborele stang deoarece se reduce sigur
                }                                                      //la cazul fara copii
                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 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 erase((root->value<x)?root->right:root->left,x);//cautam in partea corespunzatoare
    }

public:
     Node* root;
    void srd(Node* &node)
    {
        if(node != nullptr)
        {
            srd(node->left);
            out<<node->value <<" ";//v.push_back(node->value);
            srd(node->right);
        }
    }
    BST():root(nullptr){};
    void insert(int x)
    {
        Node* nod = new Node(x);
        if(root == nullptr)//case root
            root = nod;
        else
            insert( root,nod);
    }
    void erase(int x)
    {
        Node * 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;
    }
    void showSorted()
    {
        srd(root);
    }
};
class RBT       //Red Black Tree
{

};
int main()
{
    BST bst;

    int n,x,op,nr;
    in >> n;
    for(int i = 0  ; i< n ; i++)
    {
        in >> op >> x;
        switch(op)
        {
            case 1: bst.insert(x);

               break;
            case 2:
                bst.erase(x);
             break;
            case 3: nr = bst.find(x);
            out<<nr<<'\n';
            break;
        }
    }
    //bst.showSorted();

}