Cod sursa(job #964997)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 22 iunie 2013 21:47:30
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("algsort.in");
ofstream g("algsort.out");

struct node
{
    int value;

    node* left;
    node* right;
};

void update( node*& root, int val )
{
    if ( !root )
    {
        root = new node;
        root->value = val;

        return;
    }

    if ( val <= root->value )
        update( root->left, val );

    if ( val > root->value )
        update( root->right, val );
}

void print( node *root )
{
    if ( root->left )
        print( root->left );

    g << root->value << " ";

    if ( root->right )
        print( root->right );
}

int main()
{
    node *R = NULL;

    int n, a;

    f >> n;

    for ( int i = 0; i < n; i++ )
    {
        f >> a;
        update(R, a);
    }

    print(R);
}