title: Huffman tree stuck at last non-internal node
body: <p>So I've made a function to build a huffman tree which is passed a linked list in ascending order of frequencies (a value assigned within each node) but it seems to get stuck when its down to the last 'non internal node', as in a node in the linked list which was given a character to begin with.</p>
  
  <pre><code>void build_tree(pqueue *list)
  {
  
  node *temp; 
  node* parent_node;
  int min_1, min_2, ind = 0, counter = 0, length = 4;
  temp = new_node();
  
  
      while (length &gt; 2)
      {
          temp = list -&gt; start;            /* 0. point temp at the linked list start (lowest frequency/letter) */
          parent_node = new_node();              /* 1. make new node */
          min_1 = temp -&gt; frequency;    
          parent_node -&gt; left = temp;         /* 2. assign parent to point at left leaf */
          temp = temp -&gt; next;                /* 3. move to next node */
          min_2 = temp -&gt; frequency;      
          parent_node -&gt; right  = temp;        /* 4. assign parent to point at right leaf */
          parent_node -&gt; letter = '$';
          parent_node -&gt; frequency = min_1 + min_2; /* 5.assign internal node frequency */
          list -&gt; start = temp -&gt; next;/* set the list to point at the next node for the next iteration through the loop */
  
          while (ind == 0 &amp;&amp; temp -&gt; next != NULL)
          {
              temp = temp -&gt; next;
              if (temp -&gt; next -&gt; frequency &gt;= parent_node -&gt; frequency &amp;&amp; temp != NULL) /* insert parent node */
              {
                  parent_node -&gt; next = temp -&gt; next; /* parent points at higher freq node */
                  temp -&gt; next = parent_node; /* parent node is temp next */
                  ind = 1;
              }
              else if (temp -&gt; next == NULL) /* insert at end of list if parent node is biggest frequency */
              {
                  temp -&gt; next = parent_node;
                  ind = 1;
              }
          } 
          ind = 0;
          temp =  list -&gt; start; /* temp points at new start (two nodes along)*/
          while (temp -&gt; next != NULL)
          {
              temp = temp -&gt; next;
              counter++;
              printf("%c : %d\n", temp -&gt; letter, temp -&gt; frequency);
          }
          printf("----------------------------------------------\n");
          length = counter;
          counter = 0;
      }
  }
  </code></pre>
  
  <p>When passed a text file, it will print every iteration through building the tree to show the adding of two nodes, removal of those nodes, and insertion of the new node (two previous node frequencies added together), however it ends with a seg fault when it gets to only internal nodes (marked as $) and one non-internal node. i.e this after however many iterations it took to whittle it down to this number of nodes:</p>
  
  <p><code>$ : 4
  $ : 4
  $ : 4
  z: 6
  Segmentation fault (core dumped)</code></p>