title: Binary Tree Bin Packing Algorithm in C
body: <h1>What I'm trying to do:</h1>
  
  <p>I saw <a href="https://codeincomplete.com/posts/bin-packing/" rel="nofollow noreferrer">this</a> Algorithm, it's made with javascript and I'm trying to do  <code>Packing Blocks into a Fixed Rectangle</code> in C.
  In my code, shown below, I read data from a <code>.txt</code>. This part have no problem, I'm just making a array of pointers to <code>struct Blocks</code> and after I sort it.
  After that I'm doing exactly like the code in the article, same logic, and that is where the errors are happening.</p>
  
  <h2>My Code:</h2>
  
  <pre><code>#include "stdio.h"
  #include "stdlib.h"
  
  typedef struct Block
  {
      struct Node* fit;
      int width;
      int height;
      int x;
      int y;
      int id;
  } Block;
  
  typedef struct Node {
      struct Node* down;
      struct Node* right;
      int used;   
      int width;
      int height;
      int x;
      int y;
  } Node;
  
  Node *findNode(Node *root, int w, int h);
  Node *splitNode(Node **root, int w, int h);
  
  int main()
  {
      FILE *file;
      char line[80];
      Block **blocks; 
      int totalBoards, boardWidth, boardHeight, totalBlocks;
      int i = 1, j;
  
      Node *root = malloc(sizeof(Node));
      root-&gt;x = 0;
      root-&gt;y = 0;
      root-&gt;used = 0;
      root-&gt;id = 0;
  
      fopen_s(&amp;file, "blocks.txt", "r");
  
      // Reading the file
      while (fgets(line, 80, file)) {
          if (i == 1) {
              sscanf_s(line, "%d\n", &amp;totalBoards);
          } else if (i == 2) {
              sscanf_s(line, "%d\n", &amp;boardWidth);
              root-&gt;width = boardWidth;
          } else if (i == 3) {
              sscanf_s(line, "%d\n", &amp;boardHeight);
              root-&gt;height = boardHeight;
          } else if (i == 4) {
              sscanf_s(line, "%d\n", &amp;totalBlocks);
              blocks = malloc(totalBlocks * sizeof(Block *));
  
          } else {            
              int w, h;
              blocks[i - 5] = malloc(sizeof(Block));
              sscanf_s(line, "%d %d", &amp;w, &amp;h);
              blocks[i - 5]-&gt;width = w;
              blocks[i - 5]-&gt;height = h;
              blocks[i - 5]-&gt;id = i - 5;
          }
  
          i++;
      }
  
      //Bubble sort
      for (i = 0; i &lt; totalBlocks; i++) {
  
          for (j = 0; j &lt; totalBlocks - i - 1; j++)
  
              if (blocks[j]-&gt;height &lt; blocks[j + 1]-&gt;height) {                
                  Block *b = blocks[j];
                  blocks[j] = blocks[j + 1];
                  blocks[j + 1] = b;
              }
      }
  
      // THE IMPORTANT PART
      // The logic used by the algorithm
      // fit function
      for (i = 0; i &lt; totalBlocks; i++) {
          Block *block = blocks[i];   
          Node *node;
          if (node = findNode(root, block-&gt;width, block-&gt;height)) {
              block-&gt;fit = splitNode(&amp;node, block-&gt;width, block-&gt;height);
          }
      }
  
      //Print the blocks
      for (i = 0; i &lt; totalBlocks; i++) {
          Block *block = blocks[i];
          if (block-&gt;fit) {           
              printf("x %d y %d\n", blocks[i]-&gt;fit-&gt;x, blocks[i]-&gt;fit-&gt;y);
          }
      }
  
      return 0;
  }
  
  Node *findNode(Node *root, int w, int h) {
      printf("%d", root-&gt;id);
      if (root-&gt;used == 1) {
          //Error Here
          return findNode(root-&gt;down, w, h) || findNode(root-&gt;right, w, h);
      }
      else if ((w &lt;= root-&gt;width) &amp;&amp; (h &lt;= root-&gt;height)) {
          return root;
      }
      else {
          return NULL;
      }
  }
  
  Node *splitNode(Node **root, int w, int h) {
  
      (*root)-&gt;used = 1;
      (*root)-&gt;down = malloc(sizeof(Node));
      (*root)-&gt;down-&gt;right = malloc(sizeof(Node));
      (*root)-&gt;down-&gt;down = malloc(sizeof(Node));
      (*root)-&gt;down-&gt;x = (*root)-&gt;x;
      (*root)-&gt;down-&gt;y = (*root)-&gt;y + h;
      (*root)-&gt;down-&gt;width = (*root)-&gt;width;
      (*root)-&gt;down-&gt;height = (*root)-&gt;height - h;
      (*root)-&gt;down-&gt;used = 0;
      (*root)-&gt;down-&gt;id = idCount;
      idCount++;
  
  
      (*root)-&gt;right = malloc(sizeof(Node));
      (*root)-&gt;right-&gt;right = malloc(sizeof(Node));
      (*root)-&gt;right-&gt;down = malloc(sizeof(Node));
      (*root)-&gt;right-&gt;x = (*root)-&gt;x + w;
      (*root)-&gt;right-&gt;y = (*root)-&gt;y;
      (*root)-&gt;right-&gt;width = (*root)-&gt;width - w;
      (*root)-&gt;right-&gt;height = (*root)-&gt;height;
      (*root)-&gt;right-&gt;used = 0;
      (*root)-&gt;right-&gt;id = idCount;
      idCount++;
  
      return *root;
  }
  </code></pre>
  
  <h1>The Error:</h1>
  
  <p>In this part, the returned <code>Node</code> from <code>findNode</code> is going wrong</p>
  
  <pre><code>if (node = findNode(root, block-&gt;width, block-&gt;height)) {
      block-&gt;fit = splitNode(&amp;node, block-&gt;width, block-&gt;height);
  }
  </code></pre>
  
  <p>when it's returned by </p>
  
  <pre><code>return findNode(root-&gt;down, w, h) || findNode(root-&gt;right, w, h);
  </code></pre>
  
  <p>because when I use the varible <code>node</code> in <code>splitNode</code></p>
  
  <pre><code>block-&gt;fit = splitNode(&amp;node, block-&gt;width, block-&gt;height);
  </code></pre>
  
  <p>all atributes of the <code>node</code> are NULL and it's causing a error.</p>
  
  <h1>The Problem:</h1>
  
  <p>In the code of this <a href="https://codeincomplete.com/posts/bin-packing/" rel="nofollow noreferrer">article</a>, it's returned the following</p>
  
  <pre><code>return this.findNode(root.right, w, h) || this.findNode(root.down, w, h);
  </code></pre>
  
  <p>and in C I'm returning </p>
  
  <pre><code>return findNode(root-&gt;right, w, h) || findNode(root-&gt;down, w, h);
  </code></pre>
  
  <p>And I thinks the error is here.</p>
  
  <h1>Solutions?</h1>
  
  <p>I think that <code>return ___ || ___</code> is not doing the same for both languages. So I have two questions:</p>
  
  <ol>
  <li><code>return ___ || ___</code> is doing the same ins both languages? If not, what is the difference and what is the C code equivalent for that code in javascript?</li>
  <li>What is wrong with my code? Why is that error happening and not returning the correct node like in the javascript code?</li>
  </ol>