Tuesday, December 3, 2013

Randomly Built Binary Search Tree in C

#include 
#include 
#include 

struct btreenode 
{
    int key;
    struct btreenode *left;
    struct btreenode *right;
    struct btreenode *parent;
};

struct btreenode* btralloc(void)//function to allocate space for new node
{
    return (struct btreenode *)malloc(sizeof(struct btreenode)); 
}   //type coercion used because malloc returns void*

struct btreenode *addnode(struct btreenode *p, int num)
{
       if (p == NULL)
       {  
           p=btralloc();//allocate space for new node
           p->key=num;
           p->left=p->right=p->parent=NULL;
       }
       else if (p->key < num) //key < num means put num in right child 
           {
            //make current node parent of next node
            p->right=addnode(p->right, num);
            p->right->parent=p;
           }
       else {
           //make current node parent of next node
           p->left=addnode(p->left, num);
           p->left->parent=p;
        }
       return p;
}

struct btreenode *makebtree(int *A, int length)
{
    struct btreenode *temp=btralloc();
    int i;
    for(i=0; i<=length;i++)
    {
        addnode(temp,A[i]);
    }
    return temp;
}
//in order tree walk
void treeprint(struct btreenode *p)
{
    if (p != NULL)
    {  
        treeprint(p->left);
        printf("%d\n", p->key);
        treeprint(p->right);
    }
        
}
//preorder tree print
void preordertreeprint(struct btreenode *p)
{
    if (p != NULL)
    {
        printf("%d\n",p->key);
        preordertreeprint(p->left);
        preordertreeprint(p->right);
    }
}
//postorder tree  print
void postordertreeprint(struct btreenode *p)
{
    if (p != NULL)
    {
        postordertreeprint(p->left);
        postordertreeprint(p->right);
        printf("%d\n",p->key);
    }
}
//iterative tree search
struct btreenode *itertreesearch(struct btreenode *x, int k)
{
    while (x != NULL && k !=(x->key))
    {
        if (k<(x->key)) x= x->left;
        else x= x->right;
    }
    return x;
}   

int main()
{
    //init randarray
    int i,j;
    int length=20;
    int array[length];
    for(i=0;i<=length;i++)
    {
        array[i]=rand()%length;
    }
    printf("Here is the rand array\n");
    for(j=0;j<=length; j++)
    {
        printf("%d\n",array[j]);
    }
    //make a tree out of array
    struct btreenode *treearr=makebtree(array, length);
    printf("Here is the In-order walk of tree\n");
    treeprint(treearr);
    return 0;
}
Comments: my implementation of bsts follows closely the design pattern presented in Kernighan and Ritchie's classic C programming language book. The algorithms are taken from Intro to Algorithms by Cormen et al. The only thing worthy of special note is that is a randomly built bst and hence all of the standard dynamic set operations take O(log n). In the near future, I will implement variations of bsts, such as red/black trees and splay trees. I will also apply treelike structures to the common and not so common graph algorithms.

No comments:

Post a Comment