Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Java tree
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { MyTreeNode<String> root = new MyTreeNode<>("Root"); MyTreeNode<String> child1 = new MyTreeNode<>("Child1"); child1.addChild("Grandchild1"); child1.addChild("Grandchild2"); MyTreeNode<String> child2 = new MyTreeNode<>("Child2"); child2.addChild("Grandchild3"); root.addChild(child1); root.addChild(child2); root.addChild("Child3"); root.addChildren(Arrays.asList( new MyTreeNode<>("Child4"), new MyTreeNode<>("Child5"), new MyTreeNode<>("Child6") )); for(MyTreeNode node : root.getChildren()) { System.out.println(node.getData()); } } } class MyTreeNode<T>{ private T data = null; private List<MyTreeNode> children = new ArrayList<>(); private MyTreeNode parent = null; public MyTreeNode(T data) { this.data = data; } public void addChild(MyTreeNode child) { child.setParent(this); this.children.add(child); } public void addChild(T data) { MyTreeNode<T> newChild = new MyTreeNode<>(data); newChild.setParent(this); children.add(newChild); } public void addChildren(List<MyTreeNode> children) { for(MyTreeNode t : children) { t.setParent(this); } this.children.addAll(children); } public List<MyTreeNode> getChildren() { return children; } public T getData() { return data; } public void setData(T data) { this.data = data; } private void setParent(MyTreeNode parent) { this.parent = parent; } public MyTreeNode getParent() { return parent; } }
run
|
edit
|
history
|
help
0
BFS in graph
JAVA # Klavyeden Girilen cümlelerin en uzununu bulma
Item battle rolyal
Bubble Sort
Решето Эратосфена
JAVA # Rastgele 15 sayıdan oluşan bir dizinin EK-EB ve Ortalamasını bulan Java Kodu
MinMaxArray
MergeSort
Frequency
square of array element