title: How to do a Level Order Traversal?
body: <p>I'm trying to do a linear order traversal on a binary tree but having trouble getting the correct output.  Basically I have created a queue and start by enqueuing the root, then until the queue is empty I dequeue the first element and add its children to the end of the queue.  When dequeuing it returns a generic element ().  I have a problem in converting this element to a tree node so that I can enqueue its children to the end of the queue in the next step.  Here's what I have done so far:</p>
  
  <pre><code>public void levelOrderTraversal()
  {
      NodeQueue&lt;E&gt; queue = new NodeQueue&lt;E&gt;();
      BTPosition&lt;E&gt; current = root;
      queue.enqueue(current.element());
      E temp = null;
  
      while(!queue.isEmpty())
      {
          temp = queue.dequeue();
          System.out.println(temp.toString());
          current.setElement(temp);
  
          if (hasLeft(current))
          {
              queue.enqueue(left(current).element());
          }
          if (hasRight(current))
          {
              queue.enqueue(right(current).element());
          }
      }
  }
  </code></pre>
  
  <p>API for BTPosition and NodeQueue can be found in <a href="http://net3.datastructures.net/doc4/index.html?net/datastructures/" rel="nofollow">http://net3.datastructures.net/doc4/index.html?net/datastructures/</a></p>
  
  <p>Any suggestions are really appreciated..</p>