title: Creating a filesystem implemented as a tree of singly linked lists in Java, using queues
body: <p>We are given some Tree implemented as a tree of singly-linked lists. The insertPath function constructs a subtree (or uses an existing subtree) to store a new file, represented by <code>filePathQueue</code> into <code>Tree&lt;FileNode&gt; t</code>. The queue has an order <code>IN -&gt; [ “myFile” , “mySubDir” , “myDir” ] -&gt; OUT</code>, meaning I should be able to dequeue to get parent directories in order, and then check against the current level in the tree to see if the directory exists. Each FileNode has a value which is its name, and a boolean <code>true</code> to indicate that it is a file, and <code>false</code> to indicate it is a directory. I have pasted my code for insertPath as well as the findChild code. The rest was given to us and I assume professor-provided code is working. The only code I implemented was findChild and insertPath.</p>
  
  <p>The exception gets thrown when the code creates a new FileSystem using the "sample" directory in my working folder, which contains three subdirectories, each with some files and their own subdirectories. To clarify: the constructor passes to me separate queues that represent each of the files and folders in the directory that I'm going to convert into a tree in a loop. So insertPath gets called multiple times with an updated tree being passed each time.</p>
  
  <p>I have no idea why adding to the tree would throw an exception: it's telling me that I'm trying to dequeue an empty queue, but based on my code I should return out of it if the queue is empty? Exception is at the bottom. The lines with the problems are the recursive calls in the insertPath method as well as the dequeue at the top. Any help is greatly appreciated. Thanks.</p>
  
  <pre><code>public Tree&lt;T&gt; findChild(T otherLabel) {
      if(getFirstChild() == null)
          return null;
      if(getFirstChild().getLabel() == otherLabel)
          return getFirstChild();
      Tree&lt;T&gt; test = getNextSibling();
      while(test != null){
          if(test.getLabel() == otherLabel)
              return test;
          test = test.getNextSibling();
      }
      return null;
  }
  
  public void insertPath(Tree&lt;FileNode&gt; t, QueueList&lt;String&gt; filePathQueue) {
  try{
      String check = filePathQueue.dequeue();
      if(filePathQueue.size() == 0){
          Tree&lt;FileNode&gt; file = new Tree&lt;FileNode&gt;(new FileNode(check,false));
          t.addChild(file);
          return;
      }
      Tree&lt;FileNode&gt; dir = new Tree&lt;FileNode&gt;(new FileNode(check,true));
      Tree&lt;FileNode&gt; subdir = t.findChild(dir.getLabel());
      if(subdir == null){
          t.addChild(dir);
          insertPath(t.getFirstChild(), filePathQueue);
      }
      insertPath(subdir, filePathQueue);
  
      } 
  catch(Exception e){ e.printStackTrace(); return;}
  
  InvalidOperationException: Queue empty: nothing to dequeue.
  at QueueList.dequeue(QueueList.java:39)
  at FileSystem.insertPath(FileSystem.java:38)
  at FileSystem.insertPath(FileSystem.java:50)
  at FileSystem.insertPath(FileSystem.java:48)
  </code></pre>