private void mergeNodes(HLinkedList list){
    
    /* Runs through the nodes and merges them. */
    while(list.getHeadNode().next().next() != null){
      
      /* The node of the left subtree. */
      HTreeNode left = new HTreeNode(list.getHeadNode());
      
      /* The node of the right subtree. */
      HTreeNode right = new HTreeNode(list.getHeadNode().next());
      
      /* Remove the two first nodes from the list. */
      list.removeHead();
      list.removeHead();
      
      /* The new root node with nodes of the left and right subtrees. */
      HTreeNode newRoot = new HTreeNode(left, right);
      
      /* Assign code values. */
      newRoot.getLeft().setCode(0);
      newRoot.getRight().setCode(1);
      
      /* Reference to the list head for iteration. */
      HTreeNode iterator;
      
      /* Tracks insertion of new root node into list. */
      boolean insert = false;
      
      /* Inserts the node into list in ascending order of frequency. */
      for(iterator = list.getHeadNode(); iterator != null && insert == false; iterator = iterator.next()){
        
        /* Compares the iterator with the new root, inserts new root before iterator node. */
        if (iterator.compareTo(newRoot) > 0) {
          list.insertBefore(iterator, newRoot);
          insert = true;
        }
        /* Otherwise inserts at end. */
        else if (iterator.next() == null && insert == false) {
          list.insertAtEnd(newRoot);
          insert = true;
        }
      }
      
    }
    
    /* The node of the left subtree. */
    HTreeNode left = new HTreeNode(list.getHeadNode());
    
    /* The node of the right subtree. */
    HTreeNode right = new HTreeNode(list.getHeadNode().next());
    
    /* Remove the head node from the list to attain Huffman Tree root node. */
    list.removeHead();
    
    /* The Huffman Tree root node with nodes of left and right subtrees. */
    HTreeNode newRoot = new HTreeNode(left, right);
    
    /* Assign code values. */
    newRoot.getLeft().setCode(0);
    newRoot.getRight().setCode(1);
    
    /* Set the root node code to 0. */
    newRoot.setCode(0);
    
    /* Set head node of list to the root of the H Tree. */
    list.setHead(newRoot);
    
  }