Cod sursa(job #1756825)

Utilizator sulzandreiandrei sulzandrei Data 13 septembrie 2016 18:05:36
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 3.93 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream in("algsort.in");
ofstream out("algsort.out");
class BST
{
private :
    vector<int> v;
    struct Node
    {
        Node *father,*left,*right;
        int value;
        Node(int x):father(nullptr),left(nullptr),right(nullptr),value(x){};
    };
    Node* root;
    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)
            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
        {
            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
                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(Node* &node)
    {
        if(node != nullptr)
        {
            srd(node->left);
            v.push_back(node->value);
            srd(node->right);
        }
    }
public:
    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;
    }
    vector<int> getSorted()
    {
        v.clear();
        srd(root);
        return v;
    }
};
int main()
{
    BST bsttree;
    int n,x;
    in >> n;
    for(int i = 0 ; i < n ; i++)
    {
        in >> x;
        bsttree.insert(x);
    };
    vector<int> v = bsttree.getSorted();
    for(const auto& x:v)
        out<< x << " ";

    return 0;
}