Cod sursa(job #1757406)

Utilizator sulzandreiandrei sulzandrei Data 14 septembrie 2016 23:45:20
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 6.99 kb
#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 * findMax(node* &root)
    {
        if(root == nullptr)
            return nullptr;
        else if(root->right == nullptr)
            return root;
        else return findMax(root->right);
    }
    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
{

};
class AVL
{
public:
    struct node
    {
        int key;
        node *left,*right;
        int height;
        node(int x, node* lt,node* rt,int h= 0):key{x},left{lt},right{rt},height{h}{};
    };
    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;
    }
    int height(node * t)
    {
        return (t == nullptr)?-1:t->height;
    }
    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(root->key < x )
            insert( x, root->right );

        balance( root );
    }
    static const int ALLOWED_IMBALANCE = 1;
    void rotateWithLeftChild( node * & k2 )//right rotate
    {
        node *k1 = k2->left;
        k2->left = k1->right;
        k1->right = k2;
        k2->height = max( height( k2->left ), height( k2->right ) ) + 1;
        k1->height = max( height( k1->left ), k2->height ) + 1;
        k2 = k1;
    }
    void rotateWithRightChild(node* &k1)//left rotate
    {
        node *k2 = k1->right;
        k1->right = k2->left;
        k2->left = k1;
        k1->height = max(height(k1->left),height(k1->right))+1;
        k2->height = max(height(k2->right),k1->height)+1;
        k1 = k2;
    }
    void doubleWithLeftChild( node * & k3 )//left right rotations
    {
        rotateWithRightChild( k3->left );
        rotateWithLeftChild( k3 );
    }
    void doubleWithRightChild(node* & k3)//right left rotations
    {
        rotateWithLeftChild(k3->right);
        rotateWithRightChild(k3);
    }
    void balance( node* &root)
    {
        if( root == nullptr )
            return;

        if( height( root->left ) - height( root->right ) > ALLOWED_IMBALANCE )//left -x case
            if( height( root->left->left ) >= height( root->left->right ) )
                rotateWithLeftChild( root );//left -left case
            else
                doubleWithLeftChild( root );//left right case
        else
            if( height( root->right ) - height( root->left ) > ALLOWED_IMBALANCE )//right-x case
                if( height( root->right->right ) >= height( root->right->left ) )
                    rotateWithRightChild( root );//right right case
                else
                    doubleWithRightChild( root );//right left case

        root->height = max( height( root->left ), height( root->right ) ) + 1;
    }
    node * findMin(node * &root)
    {
        if(root == nullptr)
            return nullptr;
        else if(root->left == nullptr)
            return root;
        else return findMin(root->left);
    }
    void remove( int x, node *&root)
    {
        if( root == nullptr )
            return; // Item not found; do nothing

        if( x < root->key )
            remove( x, root->left );
        else if( root->key < x )
            remove( x, root->right );
        else if( root->left != nullptr && root->right != nullptr ) // Two children
        {
            root->key = findMin( root->right )->key;
            remove( root->key, root->right );
        }
        else
        {
            node *old = root;
            root = ( root->left != nullptr ) ? root->left : root->right;
            delete old;
        }

        balance( root );
     }
     void insert(int x)
     {
         insert(x,root);
     }
     void remove(int x)
     {
         remove(x,root);
     }
     bool find(int x)
     {
         return search(x,root);
     }
};
int main()
{
    BST bst;
    AVL avl;
    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:
                avl.insert(x);
                //bst.insert(x);
                    //um.insert(x);
               break;
            case 2: //um.erase(x);
                //bst.erase(x);
                avl.remove(x);
             break;
            case 3:
                nr = avl.find(x);
                //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
}