Cod sursa(job #1757388)

Utilizator sulzandreiandrei sulzandrei Data 14 septembrie 2016 22:16:08
Problema Hashuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 5.8 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 = 82133352;


 template <typename Object, typename Comparator=less<Object>>
 class BinarySearchTree
 {
     public:

     // Same methods, with Object replacing Comparable


     struct BinaryNode
    {
        Object element;
        BinaryNode *left;
        BinaryNode *right;

        BinaryNode( const Object & theElement, BinaryNode *lt, BinaryNode *rt )
        : element{ theElement }, left{ lt }, right{ rt } { }

        BinaryNode( Object && theElement, BinaryNode *lt, BinaryNode *rt )
        : element{ std::move( theElement ) }, left{ lt }, right{ rt } { }
    };
    BinaryNode *root = nullptr;
    Comparator isLessThan;

    // Same methods, with Object replacing Comparable

    /**
    * Internal method to test if an item is in a subtree.
     * x is item to search for.
    * t is the node that roots the subtree.
    */
    bool contains( const Object & x, BinaryNode *t ) const
    {
        if( t == nullptr )
            return false;
        else if( isLessThan( x, t->element ) )
            return contains( x, t->left );
        else if( isLessThan( t->element, x ) )
            return contains( x, t->right );
        else
            return true; // Match
    }
    /**
 * Internal method to find the smallest item in a subtree t.
 * Return node containing the smallest item.
 */
    BinaryNode * findMin( BinaryNode *t ) const
    {
        if( t == nullptr )
            return nullptr;
        if( t->left == nullptr )
            return t;
        return findMin( t->left );
    }
    /**
    * Internal method to find the largest item in a subtree t.
    * Return node containing the largest item.
    */
    BinaryNode * findMax( BinaryNode *t ) const
    {
        if( t != nullptr )
            while( t->right != nullptr )
                t = t->right;
        return t;
    }
    /**
    * Internal method to insert into a subtree.
    * x is the item to insert.
    * t is the node that roots the subtree.
    * Set the new root of the subtree.
    */
    void insert( const Object & x, BinaryNode *&t)
    {
        if( t == nullptr )
            t = new BinaryNode{ x, nullptr, nullptr };
        else if( x < t->element )
            insert( x, t->left );
        else if( t->element < x )
            insert( x, t->right );
        else
        ; // Duplicate; do nothing
    }

    /**
    * Internal method to insert into a subtree.
    * x is the item to insert by moving.
    * t is the node that roots the subtree.
    * Set the new root of the subtree.
    */
    void insert( Object && x, BinaryNode *&t)
    {
        if( t == nullptr )
            t = new BinaryNode{ std::move( x ), nullptr, nullptr };
        else if( x < t->element )
            insert( std::move( x ), t->left );
        else if( t->element < x )
            insert( std::move( x ), t->right );
        else
        ; // Duplicate; do nothing
    }
    /**
    * Internal method to remove from a subtree.
    * x is the item to remove.
    * t is the node that roots the subtree.
    * Set the new root of the subtree.
    */
    void remove( const Object & x, BinaryNode *&t)
    {
        if( t == nullptr )
            return; // Item not found; do nothing
        if( x < t->element )
            remove( x, t->left );
        else if( t->element < x )
            remove( x, t->right );
        else if( t->left != nullptr && t->right != nullptr ) // Two children
        {
            t->element = findMin( t->right )->element;
            remove( t->element, t->right );
        }
        else
        {
            BinaryNode *oldNode = t;
            t = ( t->left != nullptr ) ? t->left : t->right;
            delete oldNode;
        }
    }
    /**
    * Insert x into the tree; duplicates are ignored.
    */
    void insert( const Object & x )
    {
        insert( x, root );
    }

    /**
    * Remove x from the tree. Nothing is done if x is not found.
    */
    void remove( const Object & x )
    {
        remove( x, root );
    }
    bool find(const Object&x )
    {
        return contains(x,root);
    }
    /**
    * Destructor for the tree
    */
    ~BinarySearchTree( )
    {
        //makeEmpty( );
    }
    /**
    * Internal method to make subtree empty.
    */
    void makeEmpty( BinaryNode * & t )
    {
        if( t != nullptr )
        {
            makeEmpty( t->left );
            makeEmpty( t->right );
            delete t;
        }
        t = nullptr;
    }


    /**
    * Internal method to clone subtree.
    */
    BinaryNode * clone( BinaryNode *t ) const
    {
        if( t == nullptr )
            return nullptr;
        else
            return new BinaryNode{ t->element, clone( t->left ), clone( t->right ) };
    }
};

class RBT       //Red Black Tree
{

};
int main()
{
    BinarySearchTree<int> 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.remove(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
}