質問

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();

      }

  }
役に立ちましたか?

解決

code is right, execept for this part (which is now correct)

  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) {
            System.out.println("initializing element for index " + hd + " element " + root.key()  );
            ArrayList list = new ArrayList();
            list.add(root.key());
            hm.put(hd, list);
        } else {
            System.out.println("adding element " + root.key() + " for index " + hd);
            hm.get(hd).add(root.key());
        }
        verticalPrintTree(root.right(), hd + 1, hm);

    }

By array list doc either you specify a collection of items to be added to the list or you specify the initial capacity, so you were specifying the wrong thing. You need to create a collection and then add the element to it. Finally you can store it in your hash.

他のヒント

When you do new ArrayList(root.key()) you are constructing a list with a certain capacity, not constructing a list that already has that int in it as an element.

You could do:

new ArrayList(Collections.singleton(root.key()));
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top