Making the root in Huffman coding
<p>I'm trying to build a Huffman tree, and while my code combines the letters and weights correctly, it isn't building the tree correctly. When I print the tree out in order, there is no root, and the children are leaf nodes instead of parents with children of their own. I understand I'm not setting the root/parents correctly, but I'm not sure how to do it since the tree is being built from the bottom up. This is the method to build the tree:</p>

<pre><code>public void buildTree(TreeNode[] a){
    TreeNode[] x = a;
    TreeNode root = new TreeNode();
    TreeNode someNode;
    TreeNode tree = new TreeNode();
    System.out.println("building start");
    int last = x.length-1;
    String combo = "";
    int sum = 0;
    while(last &gt;= 1 ){
        combo=x[last-1].getWeight().getLetter().concat(x[last].getWeight().getLetter());
        sum = x[last-1].getWeight().getFreq() + x[last].getWeight().getFreq();
        someNode = new TreeNode(new Huffman(combo, sum));
        if (x[last-1].getWeight().getFreq() &gt;     x[last].getWeight().getFreq()){
            root=new TreeNode(x[last-1], x[last]);
        }else{
            root=new TreeNode(x[last], x[last-1]);
        }
        x[last-1] = someNode;
        Arrays.sort(x);
        last--;
    }
    System.out.println("Build complete");
    System.out.print(x[0]);
    root.inOrder();

}
</code></pre>

<p>The method to print the tree in order:</p>

<pre><code>public void inOrder(){
    if(this.getLeft()!=null)
        this.getLeft().inOrder();
    System.out.print(this.getWeight()+" ");
    if(this.getRight()!=null)
        this.getRight().inOrder();
}
</code></pre>

<p>Any help would be appreciated.</p>
