package project2; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class Exercise { String krikor = "lolig"; public Exercise() { List<Node> nodelist = new ArrayList(); String[] jo = new String[4]; jo[0] = "Elie>2>4"; jo[1] = "Elie>3>1>2"; jo[2] = "Krikor>Elie>7"; jo[3] = "Krikor>Elie>6"; int f = jo.length; int p1 = -1; while(f > 0) { f = f - 1; String[] split = jo[f].split(">"); int x = split.length; int l = x; int o = l; while (x > 0) { x = x - 1; Node krikor = new Node(); krikor.setName(split[x]); krikor.setLevel(x); if(o!=l) { krikor.getChilds().add(nodelist.get(p1)); } o = o - 1; nodelist.add(krikor); p1 = p1 + 1; } } fix(nodelist); int size = nodelist.size(); int sizes =0; while(sizes < size) { if(nodelist.get(sizes).getLevel() == 0) print(nodelist.get(sizes),""); sizes = sizes + 1; } } public void fix(List<Node> nodelist ) { int i = 0; while(i < nodelist.size()) { int o= 0; while(o < nodelist.size()) { if(nodelist.get(i).getName().equals(nodelist.get(o).getName())) { move(nodelist.get(i),nodelist.get(o),nodelist); nodelist.remove(o); System.out.println("entered here fix "); o = o - 1; } o = o + 1; } i = i + 1; } } public void move(Node i, Node o,List<Node> nodelist ) { int po = 0; int f = 0; System.out.println("entered here move "); while(f < i.getChilds().size()) { int o1= 0; while(o1 < o.getChilds().size()) { if(nodelist.get(f).getName().equals(nodelist.get(o1).getName())) { move(nodelist.get(f),nodelist.get(o1),nodelist); // nodelist.remove(o1); // o1 = o1 - 1; po = 1; } o1 = o1 + 1; } f = f +1; } if(po == 0) { i.getChilds().add(copy(o)); } } public Node copy(Node m) { Node o = new Node(); o.setName(m.getName()); o.setParent(m.getParent()); int op = m.getChilds().size(); while(op > 0) { op = op - 1; o.getChilds().add(copy(m.getChilds().get(op))); } return o; } public void print(Node node,String m) { System.out.println(m+""+node.getName()); String o = m + "-------"; if(node.getChilds().size() != 0) { int i = node.getChilds().size(); while(i > 0) { i = i - 1; print(node.getChilds().get(i),o); } } } public void setKrikor(String krikor) { this.krikor = krikor; } public String getKrikor() { return krikor; } }
package project2; import java.util.ArrayList; import java.util.List; public class Node { String name; int level; public ArrayList<Node> childs = new ArrayList<Node>(); String parent=""; boolean flag = false; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setChilds(ArrayList<Node> childs) { this.childs = childs; } public ArrayList<Node> getChilds() { return childs; } public void setLevel(int level) { this.level = level; } public int getLevel() { return level; } public void setParent(String parent) { this.parent = parent; } public String getParent() { return parent; } public void setFlag(boolean flag) { this.flag = flag; } public boolean isFlag() { return flag; } }