Butani Vijay
Butani Vijay

Reputation: 4239

Display ArrayList data in tree structure in java

I have an arrayList with following values:

static ArrayList<DTONodeDetail> tree;
public static void main(String[] args) {
    // TODO Auto-generated method stub
    tree=new ArrayList<DTONodeDetail>();

     //first argument->NodeId
     //second->NodeName
     // third -> ParentNodeId

    tree.add(getDTO(1,"Root",0));
    tree.add(getDTO(239,"Node-1",1));
    tree.add(getDTO(242,"Node-2",239));
    tree.add(getDTO(243,"Node-3",239));
    tree.add(getDTO(244,"Node-4",242));
    tree.add(getDTO(245,"Node-5",243));
    displayTree(tree.get(0));       

}

public static DTONodeDetail getDTO(int nodeId,String nodeName,int parentID)
{
    DTONodeDetail dto=new DTONodeDetail();
    dto.setNodeId(nodeId);
    dto.setNodeDisplayName(nodeName);
    dto.setParentID(parentID);

    return dto;
}

Now i want to display above data in tree structure as below using simple java code:

Root
-----Node-1
------------Node-2
------------------Node-4
------------Node-3
------------------Node-5

I have tried following but unable to get desire result:

public static void displayTree(DTONodeDetail dto){

    ArrayList<DTONodeDetail> childs = selectChild(dto.getNodeId());
    System.out.println(dto.getNodeDisplayName());
    for(DTONodeDetail obj:childs){

        displayTree(obj);
    }

}

public static ArrayList<DTOWorkSpaceNodeDetail>  selectChild(int nodeID){
        ArrayList<DTOWorkSpaceNodeDetail> list=new ArrayList<DTOWorkSpaceNodeDetail>();

        for(int i=0;i<tree.size();i++)
        {
            if(tree.get(i).getParentID()==nodeID){

                list.add(tree.get(i));

            }       
        }
        return list;

    }

Please Provide some guide or code.

Upvotes: 0

Views: 3014

Answers (3)

Liu guanghua
Liu guanghua

Reputation: 991

Sorry, I misunderstood the question. I updated them.

    public class DTONodeDetail {
        private int nodeId;
        private String nodeName;
        private int parentId;       
        public DTONodeDetail() {
        }

        public DTONodeDetail(int nodeId, String nodeName, int parentId) {
            this.nodeId = nodeId;
            this.nodeName = nodeName;
            this.parentId = parentId;
        }

        public int getNodeId() {
            return nodeId;
        }

        public void setNodeId(int nodeId) {
            this.nodeId = nodeId;
        }

        public String getNodeName() {
            return nodeName;
        }

        public void setNodeName(String nodeName) {
            this.nodeName = nodeName;
        }

        public int getParentId() {
            return parentId;
        }

        public void setParentId(int parentId) {
            this.parentId = parentId;
        }


        private static List<DTONodeDetail> tree;


        public static DTONodeDetail getDTO(int nodeId, String nodeName, int parentID) {
            DTONodeDetail dto = new DTONodeDetail();
            dto.setNodeId(nodeId);
            dto.setNodeName(nodeName);
            dto.setParentId(parentID);

            return dto;
        }

        private static List<DTONodeDetail> selectChildren(int parentId) {
            List<DTONodeDetail> result = new ArrayList<DTONodeDetail>();
            for (DTONodeDetail d : tree) {
                if (d.getParentId() == parentId) {
                    result.add(d);
                }
            }
            return result;
        }

        public static void displayTree(DTONodeDetail dto, int level) {
            List<DTONodeDetail> childs = selectChildren(dto.getNodeId());
            String space = "";
            for (int i = 0; i < level; i++) {
                space += "\t";
            }
            System.out.println(space + dto.getNodeName());
            if(childs.size()>0){
                level ++;
            }
            for (DTONodeDetail obj : childs) {
                displayTree(obj, level);
            }
        }

        public static void main(String[] args) {
            tree = new ArrayList<DTONodeDetail>();

            tree.add(getDTO(1, "Root", 0));
            tree.add(getDTO(239, "Node_1", 1));
            tree.add(getDTO(242, "Node_2", 239));
            tree.add(getDTO(243, "Node_3", 239));
            tree.add(getDTO(244, "Node_4", 242));
            tree.add(getDTO(245, "Node_5", 243));
            displayTree(tree.get(0), 0);
        }
    }

Result:

    Root
        Node_1
            Node_2
                Node_4
            Node_3
                Node_5

Upvotes: 1

farmer1992
farmer1992

Reputation: 8156

You should do like this

static void displayTree(DTONodeDetail root ,int level){

    System.out.print(prefix(level));
    System.out.println(root.name);

    ArrayList<DTONodeDetail> children = selectChild(dto.getNodeId());

    for(DTONodeDetail child : children){
       displayTree(child, level + 1);
    }

}

the prefix is a func to build enough '----'

static String prefix(int level){
    StringBuilder s = new StringBuilder();
    for(int i = 0; i < level; i++){
        s.append("----");
    }

    return s.toString();
}

Result

displayTree(node1, 0);

Node-1
----Node-2
--------Node-4
----Node-3
--------Node-5

Upvotes: 1

Soana
Soana

Reputation: 711

The only problem with your implementation that I see, is that the output is as expected, but flat (i.e. no ---- at the beginning of child lines). This is because displayTree() currently has no way of knowing at which level the node it is printing is.

I propose this:

public static void displayTree(DTONodeDetail dto, int charsBeforeNodename){

    ArrayList<DTONodeDetail> childs = selectChild(dto.getNodeId());
    for(int i = 0; i <= charsBeforeNodename; i++){
        System.out.println("-");
    }
    System.out.println(dto.getNodeDisplayName());
    for(DTONodeDetail obj:childs){

        displayTree(obj, charsBeforeNodename + dto.getNodeDisplayName().length());
    }

}

Upvotes: 1

Related Questions