BST Tree using dynamic array

/******************************************************************************
                        New Era Datastructure
                      Designed by Daipayan Bhowal
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int SIZE = 64; // set default size of dynamic array as 64
enum type_t {
    INT,
    FLOAT,
    CHAR,
    STRING,
    STRUCT
};
int get_size_type(enum type_t t, int struct_size)
{
    int mulitplier;
     switch(t)
    {
      case INT:
        mulitplier = sizeof(int);
      break;
      
      case FLOAT:
        mulitplier = sizeof(float);
      break;
      
      case CHAR:
        mulitplier = sizeof(char);
      break;
      
      case STRING:
        mulitplier = 128*sizeof(char);
      break;
      
      case STRUCT:
        mulitplier = struct_size;
       break;
    }
    return mulitplier;
}
void* dyna_array_decl(enum type_t t,int size,int struct_size)
{
    void* ptr;
    int multiplier=0;
    SIZE = size;
    multiplier=get_size_type(t,struct_size);
    ptr =malloc(multiplier*size);
    return ptr;
}
void *dyna_array_expand(void *ptr, enum type_t t, int expand_size)
{
    void *expanded_array;
    expanded_array= realloc(ptr,expand_size);
    
    return expanded_array;
}
void *dyna_array_shrink(void *ptr, enum type_t t, int shrink_size)
{
    void *shrinked_array;
    shrinked_array= realloc(ptr,SIZE-shrink_size);
    
    return shrinked_array;
}
void dyna_array_set(void* ptr,enum type_t t, int index,void *data,int struct_size)
{
    void *pos;
    int multiplier=get_size_type(t,struct_size);
    if(index > SIZE)
    {   SIZE += 32; // expand the size
        ptr=dyna_array_expand(ptr,t,32); // expand the array by 32
    }
    pos = ptr + multiplier*index;
    memcpy(pos, data,multiplier );
    return;
}
void *dyna_array_get(void* ptr,enum type_t t, int index,int struct_size)
{
    void *pos;
    int multiplier=get_size_type(t,struct_size);
    pos = ptr + multiplier*index;
 
    return pos;
}

/************** Array Tree ****************/


struct Tree
{
  int *ptr_to_node;  
  enum type_t ty_of_ptr_to_node;
  int left;
  int right;
};
int max_index=0; // this is size of tree not array
#define UNOCCUPIED -1

struct Tree *create_tree()
{
    int i=0;
    struct Tree *t = (struct Tree *) dyna_array_decl(STRUCT,SIZE,sizeof(struct Tree));
    max_index=0; // no element inserted in tree that's why zero
     printf("Create Tree!\n");
    for(i=0; i< SIZE; i++)
    {
        t[i].ptr_to_node = NULL;
        t[i].ty_of_ptr_to_node = STRUCT;
        t[i].left = UNOCCUPIED;
        t[i].right = UNOCCUPIED;
        printf("%p %d %d %d\n",t[i].ptr_to_node,t[i].ty_of_ptr_to_node,t[i].left,t[i].right);
    }
   
    return t;
}
void delete_tree(struct Tree *t)
{
    int i=0;

     printf("Delete Tree!\n");
    for(i=0; i< SIZE; i++)
    {
        if(t[i].ptr_to_node)
            free(t[i].ptr_to_node);
        printf("%p %d %d %d\n",t[i].ptr_to_node,t[i].ty_of_ptr_to_node,t[i].left,t[i].right);
    }
    max_index=0;
    free(t);
    return;
}

void insert(struct Tree *t,struct Tree *data) // BST insertion
{
   int i=1,index;
   if(max_index == 0)
   {
       printf("first element insertion !\n");
       dyna_array_set(t,STRUCT,0,data,sizeof(struct Tree));
       max_index++;
       return;
   }
   while (i <= SIZE)
   {
     struct Tree *p=dyna_array_get(t,STRUCT,i-1,sizeof(struct Tree));
     if(p->ptr_to_node != NULL)
     {
       if(*(p->ptr_to_node) > *(data->ptr_to_node)) // save to left side
       {
            printf("second element insertion %d!\n",i);
            index = 2*i;
            t[i].left = index;
            max_index++;
       }
        else // save to right side
       {
            index = 2*i + 1;
            t[i].right = index;
            max_index++;
       }
     }
     else
     {
         break;
     }
    i=index;
  }
 
    if(index < SIZE)
      dyna_array_set(t,STRUCT,index-1,(void*)data,sizeof(struct Tree));
 

}
void delete(struct Tree *t,struct Tree to_bedeleted)
{
   int i=0,j;
     if(max_index == 0)
     {
         printf("Tree is empty ! for deletion!\n");
         return;
     }
     while(i <SIZE )
     {
        if(*(t[i].ptr_to_node) == *(to_bedeleted.ptr_to_node))
        {
            for(j=i; j<SIZE-1; j++)
            {
                t[j].ptr_to_node = t[j+1].ptr_to_node;
                t[j].ty_of_ptr_to_node = t[j+1].ty_of_ptr_to_node;
                t[j].left = t[j+1].left;
                t[j].right = t[j+1].right;
            }
            t=dyna_array_shrink(t,STRUCT,sizeof(struct Tree));
            max_index--;
        }
        i++;
     }
}

void traverse(struct Tree* t)
{
  int i=0;
    while(i< max_index)
    {
        if(t[i].ptr_to_node != NULL)
        printf("%d\n", *(t[i].ptr_to_node));
        i++;
    }
    
}


int main()
{
    struct Tree *t= create_tree();
    int a = 10,b=5,c=3,d=11;
     //traverse(t);
    struct Tree data1 = { &a, INT, UNOCCUPIED, UNOCCUPIED};
    struct Tree data2 = { &b, INT, UNOCCUPIED, UNOCCUPIED};
    struct Tree data3 = { &c, INT, UNOCCUPIED, UNOCCUPIED};
    struct Tree data4 = { &d, INT, UNOCCUPIED, UNOCCUPIED};
     insert(t,&data1);
     insert(t,&data2);
     insert(t,&data3);
     insert(t,&data4);
     traverse(t);
     delete(t,data1);
     delete_tree(t);
    return 0;
}


Comments