Cod sursa(job #1255702)

Utilizator deresurobertoFMI - Deresu Roberto deresuroberto Data 5 noiembrie 2014 01:56:32
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
//Roberto Deresu - FMI
//Re :)
#include<fstream>
using namespace std;
int n,i,val;

struct nod
{
    int val;
    nod *st,*dr;
};

nod *v = new nod;

ifstream fin("algsort.in");
ofstream fout("algsort.out");

void insert(nod *c, int val)
{
    if(val <= c->val)
    {
        if(c->st)
            insert(c->st, val);
        else
        {
            c->st = new nod;
            c->st->val = val;
            c->st->st = c->st->dr = 0;
        }
    }
    else
    {
        if(c->dr)
            insert(c->dr, val);
        else
        {
            c->dr = new nod;
            c->dr->val = val;
            c->dr->st = c->dr->dr = 0;
        }
    }
}

void SVD(nod *c)
{
    if(c->st)
        SVD(c->st);

    fout<< c->val << " ";

    if(c->dr)
        SVD(c->dr);
}

int main()
{
    fin>>n;

    v->val = -1;
    v->st = v->dr = 0;

    for(i=1;i<=n;i++)
    {
        fin>>val;
        insert(v, val);
    }

    SVD(v->dr);

	return 0;
}