title: Generic binary search tree in C
body: <p>I have the implemented a binary search tree but I also want to make it generic. The code is the following:</p>
  
  <pre><code>typedef struct treeNode {
    int data;
    struct treeNode *left;
    struct treeNode *right;
  } treeNode;
  </code></pre>
  
  <p>and the functions:</p>
  
  <pre><code>treeNode* FindMin(treeNode *node) {
    if(node==NULL) {
      /* There is no element in the tree */
      return NULL;
    }
    if(node-&gt;left) /* Go to the left sub tree to find the min element */
      return FindMin(node-&gt;left);
    else 
      return node;
  }
  
  treeNode * Insert(treeNode *node,int data) {
    if(node==NULL) {
      treeNode *temp;
      temp = (treeNode *)malloc(sizeof(treeNode));
      temp -&gt; data = data;
      temp -&gt; left = temp -&gt; right = NULL;
      return temp;
    }
  
    if(data &gt; (node-&gt;data)) {
      node-&gt;right = Insert(node-&gt;right,data);
    }
    else if(data &lt;= (node-&gt;data)) {
      node-&gt;left = Insert(node-&gt;left,data);
    }
  /* Else there is nothing to do as the data is already in the tree. */
    return node;
  }
  
  treeNode * Delete(treeNode *node, int data) {
    treeNode *temp;
    if(node==NULL) {
      printf("Element Not Found");
    }
    else if(data &lt; node-&gt;data) {
      node-&gt;left = Delete(node-&gt;left, data);
    }
    else if(data &gt; node-&gt;data) {
      node-&gt;right = Delete(node-&gt;right, data);
    }
    else {
      /* Now We can delete this node and replace with either minimum element 
         in the right sub tree or maximum element in the left subtree */
      if(node-&gt;right &amp;&amp; node-&gt;left) {
          /* Here we will replace with minimum element in the right sub tree */
          temp = FindMin(node-&gt;right);
          node -&gt; data = temp-&gt;data; 
          /* As we replaced it with some other node, we have to delete that node */
          node -&gt; right = Delete(node-&gt;right,temp-&gt;data);
      }
      else {
          /* If there is only one or zero children then we can directly 
              remove it from the tree and connect its parent to its child */
          temp = node;
          if(node-&gt;left == NULL)
              node = node-&gt;right;
          else if(node-&gt;right == NULL)
              node = node-&gt;left;
          free(temp); /* temp is longer required */ 
      }
  }
    return node;
  
  }
  
  void PrintInorder(treeNode *node) {
    if (node != NULL) {
      PrintInorder(node-&gt;left);
      printf("%d ",node-&gt;data);
      PrintInorder(node-&gt;right);
    }
  }
  </code></pre>
  
  <p>First thing is to change in the struct</p>
  
  <pre><code>int data;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>void *data;
  </code></pre>
  
  <p>Edited with more code: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  typedef struct treeNode {
    void *data;
    struct treeNode *left;
    struct treeNode *right;
  }treeNode;
  
  treeNode * Insert(treeNode *node, void *data, int sizeOfType, int (*compare) (void *arg1, void *arg2)) { 
    if(node==NULL) {
      treeNode *temp;
      temp = malloc(sizeof(*temp));
      temp-&gt;data = malloc(sizeOfType);
      memcpy(temp-&gt;data, data, sizeOfType);
      temp -&gt; left = temp -&gt; right = NULL;
      return temp;
    }
  
    if(compare(data, node-&gt;data) == 1) {
      node-&gt;right = Insert(node-&gt;right, data, sizeof(int), compare(data, node-&gt;data));
    }
    else if(compare(data, node-&gt;data) == -1 || compare(data, node-&gt;data) == 0) {
      node-&gt;left = Insert(node-&gt;left, data, sizeof(int), compare(data, node-&gt;data));
    }
    return node;
  }
  
  void print(void* a) { 
  printf("%d ",*(int*)a); 
  }
  
  void InorderGeneric(treeNode *node, void(*p)(void *)) { 
    if (node != NULL) {                                
      InorderGeneric(node-&gt;left, p);
      p(node-&gt;data);  
      InorderGeneric(node-&gt;right, p); 
    }
  }
  
  int int_sorter( void *first_arg, void *second_arg ) {
    int first = *(int*)first_arg;
    int second = *(int*)second_arg;
    if ( first &lt; second ) {
      return -1;
    }
    else if ( first == second ) {
      return 0;
    }
    else {
      return 1;
    }
  }
  
  int main(void) {
    treeNode *root = NULL;
    int item;
    void *v;
  
    printf("Add nodes in binary tree:\n");
    while (scanf("%d ", &amp;item) == 1) {
      v = &amp;item;
      root = Insert(root, v, sizeof(int), int_sorter);
    }
  
    printf("\n---Initial tree---\n");
    printf("IN-order walk of tree:\n");
    InorderGeneric(root, print);
    printf("\n");
  
    return 0;
   }
  </code></pre>