Archelon Ischyros Archelon Inc.

Programming tools for your new processor

Treesort C Code

#define  nil             0
#define  false           0
#define  true            1

    /* Bubble, Quick, Tree */
#define sortelements     5000
#define srtelements      500

   /* tree */
struct node {
        struct node *left,*right;
        int val;
    };

    /* tree */
struct node *tree;

    /* Bubble, Quick, Tree */
int    sortlist[sortelements+1],
    biggest, littlest,
    top;

    /* Sorts an array using treesort */

    void
    tInitarr()
    {
        int i, temp;
        Initrand();
        biggest = 0; littlest = 0;
        for ( i = 1; i <= sortelements; i++ )
        {
            temp = Rand();
            sortlist[i] = temp - (temp/100000)*100000 - 50000;
            if ( sortlist[i] > biggest )
                biggest = sortlist[i];
            else if ( sortlist[i] < littlest )
                littlest = sortlist[i];
        };
    }

        void
        CreateNode (t,n) struct node **t; int n;
        {
            *t = (struct node *)malloc(sizeof(struct node));
            (*t)->left = nil; (*t)->right = nil;
            (*t)->val = n;
        }

    void
    Insert(n, t) int n; struct node *t;
        /* insert n into tree */
    {
           if ( n > t->val )
                if ( t->left == nil ) CreateNode(&t->left,n);
                else Insert(n,t->left);
           else if ( n < t->val )
                if ( t->right == nil ) CreateNode(&t->right,n);
                else Insert(n,t->right);
    }

    int Checktree(p) struct node *p;
    /* check by inorder traversal */
    {
    int result;
        result = true;
        if ( p->left != nil )
           if ( p->left->val <= p->val )
               result=false;
           else
               result = Checktree(p->left) && result;
        if ( p->right != nil )
           if ( p->right->val >= p->val )
               result = false;
           else
               result = Checktree(p->right) && result;
        return( result);
    } /* checktree */

void
Trees()
{
    int i;
    tInitarr();
    tree = (struct node *)malloc(sizeof(struct node));
    tree->left = nil; tree->right=nil; tree->val=sortlist[1];
    for ( i = 2; i <= sortelements; i++ )
        Insert(sortlist[i],tree);
    if ( ! Checktree(tree) ) printf ( " Error in Tree.\n");
}


Back to Archelon's Home Page