Cod sursa(job #2009701)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 10 august 2017 15:28:53
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
struct node
{
    int inf;
    node *ls;
    node *rs;
};
node *root=new node;
bool ok;
inline void add(const int &x)
{
    node *p=root,*q;
    while(p)
    {
        q=p;
        if(x<p->inf) {p=p->ls; ok=true;}
        else {p=p->rs; ok=false;}
    }
    p=new node;
    p->inf=x;
    p->ls=p->rs=NULL;
    if(ok)
        q->ls=p;
    else q->rs=p;
}
void afs(node *p)
{
    if(p)
    {
        afs(p->ls);
        g<<p->inf<<' ';
        afs(p->rs);
    }
}
int main()
{
    int i,j,x,n;
    f>>n>>x;
    root->inf=x;
    root->ls=root->rs=NULL;
    for(i=1;i<n;i++)
    {
        f>>x;
        add(x);
    }
    afs(root);

    return 0;
}