0

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.

2
  • What is selectChild()? Also what is the output you get with this implementation? Commented Sep 25, 2014 at 9:43
  • I have added in code for selectChild() method please check. Commented Sep 25, 2014 at 9:45

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

how to calculate level
@ButaniVijay it is auto, what you need is to start from displayTree(node1, 0)
1

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

}

2 Comments

I Know it but how can i calculate level
@ButaniVijay See my edit. I accidantly hit "Save Edits" before I finished typing. Either do it like this or just calculate the level in each step (wich is probably way more CPU-intensive depending on the size and height of your real tree).
1

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

2 Comments

What if there is more than 5 node ..mean n number of nodes
in my case node-2 and node-3 at same level

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.