mport java.util.*;
public class GenricTree{
public class Node{
int data ;
ArrayList<Node> children;
Node(int data){
this.data = data;
this.children = new ArrayList<>();
}
}
private Node root;
private int size;
GenricTree(){
Scanner s = new Scanner(System.in);
this.root = takeInput(s,null,0);
}
Private Node takeInput(Scanner sc,Node parent,int ithChild) {
if(parent == null){
System.out.println("enter the parent Node data");
}else{
System.out.println("enter data"+ithChild+"th child of the "+parent.data);
}
int nodeData = sc.nextInt();
Node node = new Node(nodeData);
size++;
System.out.println("enter the No of childrn for"+node.data);
int childrenLen = sc.nextInt();
for(int i =0 ; i < childrenLen;i++ ){
Node child = this.takeInput(sc,node,i);
node.children.add(child);
}
return node;
}
public void display(){
this.display(this.root);
}
private void display(Node node){
String str = node.data+"=>";
for(int i = 0; i< node.children.size();i++){
str = str+node.children.get(i).data+",";
}
str = str+"end";
System.out.println(str);
for(int i = 0 ; i < node.children.size();i++ ){
this.display(node.children.get(i));
}
}
public static void main(String[] args) {
GenricTree tree = new GenricTree();
tree.display();
}
}
