Cod sursa(job #2417843)

Utilizator mihai2003LLL LLL mihai2003 Data 1 mai 2019 20:48:46
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.2 kb
#include <iostream>
#include <fstream>
#include <vector>

struct node{
    node *st,*dr;
    int val;
}*bst;

node * find(int val,node * nod){
    if(nod==NULL)
        return NULL;
    if(nod->val==val)
        return nod;
    if(val>nod->val)
        return find(val,nod->dr);
    return find(val,nod->st);
}
node* newNode(int data)
{
    node* temp = new node;

    temp->val = data;

    temp->st = NULL;
    temp->dr = NULL;

    return temp;
}
node* insert(node* root, int val)
{
    node* newnode = newNode(val);
    node* x = root;
    node* y = NULL;
    while (x != NULL) {
        y = x;
        if (val < x->val)
            x = x->st;
        else
            x = x->dr;
    }

    if (y == NULL)
        y = newnode;
    else if (val < y->val)
        y->st = newnode;
    else
        y->dr = newnode;
    return y;
}

node * minb(node *start){
    while(start->st!=NULL)
        start=start->st;
    return start;
}

node * erase(int val,node *&nod){
    if(nod==NULL)
        return nod;
    if(val<nod->val)
        nod->st=erase(val,nod->st);
    else
        if(val>nod->val)
            nod->dr=erase(val,nod->dr);
        else{
            if(nod->st==NULL){
                node * tmp=nod->dr;
                delete nod;
                return tmp;
            }
            if(nod->dr==NULL){
                node * tmp=nod->st;
                delete nod;
                return tmp;
            }
            node *tmp=minb(nod->dr);
            nod->val=tmp->val;
            nod->dr=erase(tmp->val,nod->dr);
        }
    return nod;
}
std::ifstream in("heapuri.in");
std::ofstream out("heapuri.out");
std::vector<int>v;
int main(){
    int n,t,aux;
    in>>n;
    bst=NULL;
    for(int i=1;i<=n;i++){
        in>>t;
        if(t==3)
            out<<minb(bst)->val<<'\n';
        else{
            in>>aux;
            if(t==1){
                if(bst==NULL)
                    bst=insert(bst,aux);
                else
                    insert(bst,aux);
                v.push_back(aux);
            }
            else{
                bst=erase(v[aux-1],bst);

            }
        }
    }
    return 0;
}