I am trying to print a binary tree vertically. I have used Hashmap with horizontal distance of a node from the root as the key and arraylist of nodes as the values.  

As per the hardcoded input in the main module, the tree looks like this:

```
              1
            /   \
           2     3
         /  \   /  \
        4    5  6   7

```

Needed output(when i print hashmap entry) :  

`[0=[1,5,6], 1=[3], 2=[7], -2=[4], -1=[2]]` 

Output given by the below code :  

`[0=[1, 6], 1=[], 2=[], -2=[], -1=[]]`

Could anyone explain me the problem here?

```
import java.util.ArrayList;

import java.util.HashMap;

public class TreeNode {

private int key;
private TreeNode left;
private TreeNode right;

public TreeNode(int key){this.key=key;}
public int key(){return key;}
public TreeNode left(){return left;}
public TreeNode right(){return right;}

public void setKey(int key){this.key=key;}
public void setLeft(TreeNode left){this.left=left;}
public void setRight(TreeNode right){this.right=right;}

}

public class Tree {

private TreeNode root;

public Tree(TreeNode root)
{
    this.root=root;
}
public void verticalPrint() throws Exception{ 
    HashMap<Integer,ArrayList<Integer>> hm;
    hm = new HashMap<>();       
    verticalPrintTree(root,0,hm);

    System.out.println(hm.entrySet());
}

private void verticalPrintTree(TreeNode root,int hd,HashMap<Integer,ArrayList<Integer>> hm) throws Exception{

    if(root==null)
        return;
    verticalPrintTree(root.left(),hd-1,hm);
    if(hm.get(hd)==null)
        hm.put(hd,new ArrayList(root.key()));
    else
        hm.get(hd).add(root.key());
    verticalPrintTree(root.right(),hd+1,hm);

}

public void printTree()
{
    printInorder(root);
}

private void printInorder(TreeNode root) {

    if(root==null)
        return;
    printInorder(root.left());
    System.out.println(root.key());
    printInorder(root.right());

   }

  }

 public class TreeVerticalPrint {

public static void main(String args[]) throws Exception
{
    TreeNode root=new TreeNode(1);
    root.setLeft(new TreeNode(2));
    root.setRight(new TreeNode(3));
    root.left().setLeft(new TreeNode(4));
    root.left().setRight(new TreeNode(5));
    root.right().setLeft(new TreeNode(6));
    root.right().setRight(new TreeNode(7));
    Tree t= new Tree(root);
    t.printTree();
    t.verticalPrint();

      }

  }

```