Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.*;
- //import java.lang.StackOverflowError;
- //======================================================================================================================
- public class ThirdEleventh {
- public static 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 T getData() {
- return data; }
- public List<MyTreeNode> getChildren() {
- return children; }
- //public void setData(T data) {
- // this.data = data;
- //}
- private void setParent(MyTreeNode parent) {
- this.parent = parent; }
- public MyTreeNode getParent() {
- return parent; }
- }
- //======================================================================================================================
- public static void main(String[] args){
- MyTreeNode<String> root = new MyTreeNode<>("Me");
- //-------------------------------------------------------------
- MyTreeNode<String> father = new MyTreeNode<>("Father");
- MyTreeNode<String> mother = new MyTreeNode<>("Mother");
- MyTreeNode<String> fGF = new MyTreeNode<>("GrandFather");
- fGF.addChildren(Arrays.asList(
- new MyTreeNode<>("GrandFather's Mother"),
- new MyTreeNode<>("GrandFather's Father")
- ));
- MyTreeNode<String> fGM = new MyTreeNode<>("GrandMother");
- fGM.addChildren(Arrays.asList(
- new MyTreeNode<>("GrandMother's Father"),
- new MyTreeNode<>("GrandMother's Mother")
- ));
- MyTreeNode<String> mGM = new MyTreeNode<>("GrandMother");
- mGM.addChildren(Arrays.asList(
- new MyTreeNode<>("GrandMother's Mother"),
- new MyTreeNode<>("GrandMother's Father")
- ));
- MyTreeNode<String> mGF = new MyTreeNode<>("GrandFather");
- mGF.addChildren(Arrays.asList(
- new MyTreeNode<>("GrandFather's Mother"),
- new MyTreeNode<>("GrandFather's Father")
- ));
- root.addChild(mother);
- root.addChild(father);
- mother.addChild(mGM);
- mother.addChild(mGF);
- father.addChild(fGM);
- father.addChild(fGF);
- //-------------------------------------------------------------
- for(MyTreeNode node : root.getChildren()) {
- System.out.print("Child: ");
- System.out.print(node.getParent().getData());
- System.out.print(" Parent: ");
- System.out.println(node.getData());
- }
- for(MyTreeNode node : father.getChildren()) {
- System.out.print("Child: ");
- System.out.print(node.getParent().getData());
- System.out.print(" Parent: ");
- System.out.println(node.getData());
- }
- for(MyTreeNode node : mother.getChildren()) {
- System.out.print("Child: ");
- System.out.print(node.getParent().getData());
- System.out.print(" Parent: ");
- System.out.println(node.getData());
- }
- for(MyTreeNode node : mGF.getChildren()) {
- System.out.print("Child: ");
- System.out.print(node.getParent().getData());
- System.out.print(" Parent: ");
- System.out.println(node.getData());
- }
- for(MyTreeNode node : mGM.getChildren()) {
- System.out.print("Child: ");
- System.out.print(node.getParent().getData());
- System.out.print(" Parent: ");
- System.out.println(node.getData());
- }
- for(MyTreeNode node : fGM.getChildren()) {
- System.out.print("Child: ");
- System.out.print(node.getParent().getData());
- System.out.print(" Parent: ");
- System.out.println(node.getData());
- }
- for(MyTreeNode node : fGM.getChildren()) {
- System.out.print("Child: ");
- System.out.print(node.getParent().getData());
- System.out.print(" Parent: ");
- System.out.println(node.getData());
- }
- }
- }
- //======================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement