package Lacture12;
import java.util.Scanner;
import java.util.ArrayList;
public class GenericTree {
private class Node
{
int data;
ArrayList<Node> children;
Node(int data)
{
this.data=data;
}
}
private Node root;
private int size;
GenericTree()
{
Scanner s=new Scanner(System.in);
this.root=takeInput(s,null,0);
}
private Node takeInput(Scanner s,Node parent,int ithChild)
{
if(parent==null)
{
System.out.println("Enter data for root Node");
}
else
{
System.out.println("Enter the data for "+ithChild+"th child of "+parent.data);
}
int nodedata=s.nextInt();
Node node=new Node(nodedata);
this.size++;
System.out.println("Enter the no of childern for "+node.data);
int childerns=s.nextInt();
//Will be executed depending on the value of childrens.
for(int i=0;i<childerns;++i)
{
Node child=takeInput(s,node,i);
node.children.add(child);
}
return node;
}
public void display()
{
this.display(this.root);
}
private void display(Node node)
{
//node data
String str=node.data+"=>";
for(int i=0;i<node.children.size();++i)
{ //add children data of current node
str+=node.children.get(i).data+",";
}
str+="END";
//display the string for current node
System.out.println(str);
//display all nodes
for(int i=0;i<node.children.size();++i)
{
display(node.children.get(i));
}
}
}