MY name is ruhul amin

ISLAMIC UNIVERSITY OF KUSHTIA

Saturday, April 6, 2013

preorder tree coding of c program

#include<stdio.h>
#define max 10000
long a[max],n,i,j,large,l,r;
void swap(long j,long large)
{

    int p;
    p=a[j];
    a[j]=a[large];
    a[large]=p;
}
void show(long n)
{
for(i=1;i<=n;i++)
scanf("%ld",&a[i]);
for(j=n/2;j>=1;j--)
{
 function(j);
}
for(j=1;j<=n;j++)
printf("%ld ",a[j]);
printf("\n");
}
void function(long j)
{
    l=2*j;
    r=l+1;
    if(l<=n&&a[j]<a[l])
    large=l;
    else
    large=j;
    if(r<=n&&a[j]<a[r])
    large=r;
    swap(j,large);
    if(j!=large)
    {
    function(large);
    }

}
int main()
{
    while(scanf("%ld",&n)!=EOF)
    {
        show(n);
    }
    return 0;
}

No comments:

Post a Comment