import java.util.ArrayList; import java.util.List; import java.util.*; /** * * @author User1 */ public class NaryTree { /** * @param x * @param args the command line arguments */ArrayList a1 = new ArrayList(); public void makenary(char[] x) { int i =0; while (x[i]!='\0') { if (x[i]!=')') { Node n1=new Node(); n1.setData(x[i]); a1.add(n1); } if (x[i]==')') { Node temp =new Node();Node temp2 =new Node(""); do { Object last = a1.get(a1.size()-1); temp=(Node)last; a1.remove(a1.size()-1); if (temp.getData().equals('(')) { break; } int p = a1.size(); while (p>0) { Object get2 = a1.get(--p); temp2=(Node)get2; temp2.addChild(temp); } }while(true); for (int k=0;k<(a1.size()-1);k++) { Node temp4 =new Node(""); temp4=(Node)a1.get(k); temp4.setParent(temp2); } a1.add(temp2); } i++; } } public static void main(String[] args) { NaryTree n1 = new NaryTree(); // char x[]={'S','(','V','P','(','V','B','L','T',')','(','A','D','V','P',' ','L','T',')',')','(','V','P','(','P','P','(','N','P',' ','X',')',')',')','\0'}; char x[]={'S','(','V','P','(','V','B','L','T',')','(','A','D','V','P','L','T',')',')','(','V','P','(','P','P','(','N','P','X',')',')',')','\0'}; n1.makenary(x); Object get2 = n1.a1.get(n1.a1.size()-1); Node tempn = new Node(); tempn=(Node)get2; Tree<String> tree = new Tree<>(tempn); //ArrayList<Node<String>> preOrder = tree.getLongestPathFromRootToAnyLeaf(); System.out.println(tree.getRoot()); System.out.println(tree.getRoot().getChildren()); System.out.println(tree.getRoot().getChildren().size()); //System.out.println(tree.getNumberOfNodes()); //System.out.println(tree.getNumberOfDescendants(root)); //System.out.println(tree.getPreOrderTraversal()); } }