Mai intai trebuie sa te autentifici.

Cod sursa(job #1670690)

Utilizator justmehStoenica Robert justmeh Data 31 martie 2016 22:19:38
Problema Sortare prin comparare Scor 40
Compilator c Status done
Runda Arhiva educationala Marime 1.63 kb
#include <stdio.h>
#include <stdlib.h>
FILE *in,*out;
struct node{
    int data;
    struct node *dr;
    struct node *st;
};
void heap_push(struct node *root, int val)
{
    if(val<root->data)
    {
        if(root->st==NULL)
        {
            struct node *new_node;
            new_node=(struct node*)malloc(sizeof(struct node));
            new_node->data=val;
            new_node->st=NULL;
            new_node->dr=NULL;
            root->st=new_node;
        }
        else
            heap_push (root->st , val);
    }
    else
    {
        if(root->dr==NULL)
        {
            struct node *new_node;
            new_node=(struct node*)malloc(sizeof(struct node));
            new_node->data=val;
            new_node->st=NULL;
            new_node->dr=NULL;
            root->dr=new_node;
        }
        else
            heap_push(root->dr , val);
    }
}
void heap_build(struct node *root, int n)
{
    int i,val;
    for (i=0;i<n;i++)
    {
        scanf("%d",val);
        heap_push(root , val);
    }
}
void heap_sort(struct node *root)
{
    if(root->st!=NULL)
        heap_sort(root->st);
    fprintf(out,"%d ",root->data);
    if(root->dr!=NULL)
        heap_sort(root->dr);
}
int main()
{
	in=fopen("algsort.in","r");
	out=fopen("algsort.out","w");
    int n,i,a;
    fscanf(in,"%d",&n);
    struct node *root;
    root=(struct node*)malloc(sizeof(struct node));
    root->st=NULL;
    root->dr=NULL;
    fscanf(in,"%d",&a);
    root->data=a;
    for(i=1;i<n;i++)
    {
        fscanf(in,"%d",&a);
        heap_push(root , a);
    }
    heap_sort(root);
    return 0;
}