Cod sursa(job #1992734)

Utilizator 1475369147896537415369Andrei Udriste 1475369147896537415369 Data 21 iunie 2017 11:45:57
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>

using namespace std;

template <class t>

class T
{
    int s;
    enum {a, b};
    struct N{t k;int x[2];N(const t &_k = t()){k = _k;x[a] = x[b] = 0;}}*r;
    void d(N *p, N *c){if(c == 0) return;d(c, (N*)((c -> x[a]) ^ (int)p));cout << c -> k << " ";d(c, (N*)((c -> x[b]) ^ (int)p));}

public:

    T(){r = 0;s = 0;}
    void p(const t &k){
        N *n = new N(k);if(s++ == 0){r = n;return;}for(N *p = 0, *c = r, *o;;){bool i = k > c -> k;if(((c -> x[i]) ^ (int)p) != 0){o = c; c = (N*)((c -> x[i]) ^ (int)p); p = o;}else{c -> x[i] ^= (int)n; n -> x[a] = n -> x[b] = (int)c; return;}}}
    void i(){d(0, r);if(s > 0) printf("\n");}
};

int main()
{

    T<int> t; int NN, x;

    freopen("algsort.in", "r", stdin);
    freopen("algsort.out", "w", stdout);

    cin >> NN;

    for(int i = 1; i <= NN; i++){
        cin >> x;
        t.p(x);
    }t.i();

    return 0;
}