title: Convert binary tree to array in c
body: <p>I want to convert a binary tree to an array using C. I tried but was unsuccessful.</p>
  
  <p>My binary tree contains the following elements (preorder)</p>
  
  <pre><code>4 3 5 10 8 7
  </code></pre>
  
  <p>but my array contains (after sorting)</p>
  
  <pre><code>4 4 5 7 8 10
  </code></pre>
  
  <p>Any help would be greatly appreciated. My current code look like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct tree
  {
      int data;
      struct tree *left;
      struct tree *right;
  }tree;
  
  int AddToArray(tree *node, int arr[], int i);
  tree *CreateNode(int data);
  tree *Insert(tree *node, int data);
  void PrintPreorder(tree *node);
  int count(tree *node);
  int compare(const void * a, const void * b);
  
  //---------------------------------------------------------------------------
  int main()
  {
      int i;
      int size;
      int *arr=NULL;
      tree *root=NULL;
  
      printf("***TEST PROGRAM***\n");
      root = Insert(root, 4);
      root = Insert(root, 3);
      root = Insert(root, 5);
      root = Insert(root, 10);
      root = Insert (root, 8);
      root = Insert (root, 7);
      size = count(root);
  
      printf("\n***BINARY TREE (PREORDER)***\n");
      PrintPreorder(root);
      printf("\nThe binary tree countain %d nodes", size);
  
      printf("\n\n***ARRAY***\n");
      arr = calloc(size, sizeof(int));
      AddToArray(root, arr, 0);
      qsort(arr,size,sizeof(int),compare);
  
      for (i=0; i&lt;size; i++)
      {
          printf("arr[%d]: %d\n", i, arr[i]);
      }
  }
  //---------------------------------------------------------------------------
  
  int compare(const void * a, const void * b)
  {
     return ( *(int*)a - *(int*)b );
  }
  
  int AddToArray(tree *node, int arr[], int i)
  {
      if(node == NULL)
          return i;
       arr[i] = node-&gt;data;
       i++;
       if(node-&gt;left != NULL)
            AddToArray(node-&gt;left, arr, i);
       if(node-&gt;right != NULL)
            AddToArray(node-&gt;right, arr, i);
  
       arr[i] = node-&gt;data;
       i++;
  }
  
  tree *CreateNode(int data)
  {
      tree *node = (tree *)malloc(sizeof(tree));
      node -&gt; data = data;
      node -&gt; left = node -&gt; right = NULL;
      return node;
  }
  
  tree *Insert(tree *node, int data)
  {
      if(node==NULL)
      {
          tree *temp;
          temp = CreateNode(data);
          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;
  }
  
  void PrintPreorder(tree *node)
  {
      if(node==NULL)
      {
          return;
      }
      printf("%d ",node-&gt;data);
      PrintPreorder(node-&gt;left);
      PrintPreorder(node-&gt;right);
  }
  
  int count(tree *node)
  {
      int c = 1;
  
      if (node == NULL)
          return 0;
      else
      {
          c += count(node-&gt;left);
          c += count(node-&gt;right);
          return c;
       }
  }
  </code></pre>