0

I have data as shown as below. Here if Team 1 is parent & having 2 child Team A & Team B. Team A is again a parent & having player names as child. Team B does not have any child.

Again in another scenario, Team A is independent parent & contains some child etc..

If i give Team 1, then it should fetch records of Team A & Team B as a bundle. If i give Team A, then it should fetch records of Team A containing its child.

enter image description here

I was thinking to implement this using Map or Tree . and I tried this -

public class Node {

    private String id;
    private List<Node> children = new ArrayList<>();
    private Node parent;
    ..........
    //setters and getters
}

but here creating node dynamically is problem because we don't know the levels of parents(in this example there are 2). means "Dhoni" again contains some child like wise.

How to implements this ?. Please guide.

jay thakur
  • 151
  • 2
  • 13

1 Answers1

1

Whatever i understood from problem description i will try to summarize here.You are looking for a data structure which can take parent name(key) and it might have children, and each child also further can be extended.

public class Node {

private String id; // for each level you have key defined.
private List<Node> children = new ArrayList<>(); //using given key you can  get children list  

}

You can use map here

 Map<String, List<Node>> // here key is team name etc., and list represents children.

If you give team1 as key, you get list which contains teamA, teamB. So if you want to check further, check list size, if it is greater than zero, you can get children(Further you can get all the players defined for both teamA,teamB) otherwise you are at last child.

Bhagwati Malav
  • 3,349
  • 2
  • 20
  • 33