Cod sursa(job #1266175)

Utilizator claudiu.gatinaFMI Claudiu Gatina claudiu.gatina Data 18 noiembrie 2014 13:52:03
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <stdio.h>

int n;

struct nod
{
    nod *st, *dr;//*t;
    long long int x;//,h;
}*rad,*r,*p,*nou;
/*
void rot(nod *asd, int a)
{
    r=asd->t;
    if(r->st==asd)
        if(a)
            r->st=r->st->st;
        else
            r->st=r->st->dr;
    else
        if(a)
            r->dr=r->dr->st;
        else
            r->dr=r->dr->dr;
}*/

void inserare(int a)
{
    if(rad)
    {
        r = rad;
        p=NULL;
        while(r)
        {
           // r->h++;
            p=r;
            if(a>r->x)
                r=r->dr;
            else
                r=r->st;
        }
        nou = new nod;
        nou->x = a;
        nou->st = NULL;
        nou->dr = NULL;
    //    nou->t=p;
        if(p->x>a)
            p->st=nou;
        else
            p->dr=nou;
        r=nou;
    }
    else
    {
        rad = new nod;
     //   rad->t=NULL;
        rad->x=a;
        rad->st=NULL;
        rad->dr=NULL;
    }
  //  while(r)
  //  {
//
   // }

}

void afisare(nod *asd)
{
    if(asd)
    {
        afisare(asd->st);
        printf("%d ", asd->x);
        afisare(asd->dr);
    }
}

int main()
{
    int i,q;
    freopen("algsort.in", "r", stdin);
    freopen("algsort.out", "w", stdout);
    scanf("%d", &n);
    for(i=0;i<n;i++)
    {
        scanf("%d", &q);
        inserare(q);
    }
    afisare(rad);
    return 0;
}