title: Setting parents in tree
body: <p>I have a huffman binary tree that starts with an empty node a.</p>
  
  <p>A points to a left node and a right node, which also point to left and right nodes. Is it possible to set the parent nodes for each node recursively after having this tree?</p>
  
  <p>This is the code I am thinking:</p>
  
  <pre><code>public Node setParents(Node n)
  {
      if(n.getZero() == null &amp;&amp; n.getOne() == null)
      {
          return n;
      }
      Node a = setParents(n.getZero()); // Zero being left
      a.setParent(n);
      Node b = setParents(n.getOne()); // One being right.
      b.setParent(n);
  }
  </code></pre>
  
  <p>Here is how I am currently creating the tree by using a priority queue with values sorted least to greatest.</p>
  
  <pre><code>public Node createTree(PriorityQueue&lt;Node&gt; pq)
  {
  
      while(pq.size() &gt; 1)
      {
          Node n = new Node();
  
          Node a = pq.poll();
          if(pq.size() &gt; 0)
          {
              Node b = pq.poll();
              n = new Node(a.getFrequency() + b.getFrequency());
              n.setZero(a);
              a.setWhich(0);
              a.setParent(n);
              n.setOne(b);
              b.setWhich(1);
              b.setParent(n);
          }
          else
          {
              n = new Node(a.getFrequency());
              n.setZero(a);
              a.setWhich(0);
              n.setParent(n);
              n.setOne(null);
          }
          pq.add(n);
      }
      Node n = pq.poll();
      n.setParent(null);
      setParents(n.getZero());
      setParents(n.getOne());
      return n;
  }
  </code></pre>
  
  <p>I just need to make sure each node has a parent, which I don't know where to start from here.. Any help?</p>