Cod sursa(job #2323539)

Utilizator 3DwArDPauliuc Edward 3DwArD Data 19 ianuarie 2019 12:13:15
Problema Sortare prin comparare Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <bits/stdc++.h>

using namespace std;
int n;
ifstream f("algsort.in");
ofstream g("algsort.out");
struct nod{
    int val;
    nod *st, *dr;
    nod(){st=NULL, dr=NULL;}
} *rad;
nod * cautare(nod *&a, int x){

    if( a != NULL){
        if(a->val == x)return a;
        if(a->val > x)return cautare(a->st,x);
        if(a->val < x)return cautare(a->dr,x);
    }
    return a;
}

void add(nod *&a, int x){
    if(a == NULL){
        a= new nod;
        a->val=x;
    }
    else{
        if(x > a->val)
            add(a->dr, x);
        else
            add(a->st, x);
    }
}
void SRD(nod *node){
    if(node != NULL){
        SRD(node->st);
        g<<node->val<<" ";
        SRD(node->dr);
    }
}
int main()
{
    int n;
    f>>n;
    for(int i=1,x;i<=n;i++){
        f>>x;
        add(rad, x);
    }
    SRD(rad);
    return 0;
}