Advertisement
moroz1339

NULL

Sep 26th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.01 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.*;
  3. //import java.lang.StackOverflowError;
  4. //======================================================================================================================
  5. public class ThirdEleventh {
  6.     public static class MyTreeNode<T>{
  7.         private T data = null;
  8.         private List<MyTreeNode> children = new ArrayList<>();
  9.         private MyTreeNode parent = null;
  10. //-------------------------------------------------------------
  11.         public MyTreeNode(T data) {
  12.             this.data = data; }
  13.  
  14.         public void addChild(MyTreeNode child) {
  15.             child.setParent(this);
  16.             this.children.add(child); }
  17.  
  18.        //public void addChild(T data) {
  19.        //    MyTreeNode<T> newChild = new MyTreeNode<>(data);
  20.        //    newChild.setParent(this);
  21.        //    children.add(newChild); }
  22.  
  23.         public void addChildren(List<MyTreeNode> children) {
  24.             for(MyTreeNode t : children) {
  25.                 t.setParent(this);
  26.             }
  27.             this.children.addAll(children);
  28.         }
  29.  
  30.         public T getData() {
  31.             return data; }
  32.  
  33.         public List<MyTreeNode> getChildren() {
  34.             return children; }
  35.  
  36.         //public void setData(T data) {
  37.         //    this.data = data;
  38.         //}
  39.  
  40.         private void setParent(MyTreeNode parent) {
  41.             this.parent = parent; }
  42.  
  43.         public MyTreeNode getParent() {
  44.             return parent; }
  45.     }
  46. //======================================================================================================================
  47.     public static void main(String[] args){
  48.         MyTreeNode<String> root = new MyTreeNode<>("Me");
  49. //-------------------------------------------------------------
  50.  
  51.         MyTreeNode<String> father = new MyTreeNode<>("Father");
  52.  
  53.         MyTreeNode<String> mother = new MyTreeNode<>("Mother");
  54.  
  55.         MyTreeNode<String> fGF = new MyTreeNode<>("GrandFather");
  56.         fGF.addChildren(Arrays.asList(
  57.                 new MyTreeNode<>("GrandFather's Mother"),
  58.                 new MyTreeNode<>("GrandFather's Father")
  59.         ));
  60.  
  61.         MyTreeNode<String> fGM = new MyTreeNode<>("GrandMother");
  62.         fGM.addChildren(Arrays.asList(
  63.                 new MyTreeNode<>("GrandMother's Father"),
  64.                 new MyTreeNode<>("GrandMother's Mother")
  65.         ));
  66.  
  67.         MyTreeNode<String> mGM = new MyTreeNode<>("GrandMother");
  68.         mGM.addChildren(Arrays.asList(
  69.                 new MyTreeNode<>("GrandMother's Mother"),
  70.                 new MyTreeNode<>("GrandMother's Father")
  71.         ));
  72.  
  73.         MyTreeNode<String> mGF = new MyTreeNode<>("GrandFather");
  74.         mGF.addChildren(Arrays.asList(
  75.                 new MyTreeNode<>("GrandFather's Mother"),
  76.                 new MyTreeNode<>("GrandFather's Father")
  77.         ));
  78.  
  79.         root.addChild(mother);
  80.         root.addChild(father);
  81.         mother.addChild(mGM);
  82.         mother.addChild(mGF);
  83.         father.addChild(fGM);
  84.         father.addChild(fGF);
  85.  
  86. //-------------------------------------------------------------
  87.         for(MyTreeNode node : root.getChildren()) {
  88.             System.out.print("Child: ");
  89.             System.out.print(node.getParent().getData());
  90.             System.out.print("        Parent: ");
  91.             System.out.println(node.getData());
  92.         }
  93.  
  94.         for(MyTreeNode node : father.getChildren()) {
  95.             System.out.print("Child: ");
  96.             System.out.print(node.getParent().getData());
  97.             System.out.print("        Parent: ");
  98.             System.out.println(node.getData());
  99.         }
  100.  
  101.         for(MyTreeNode node : mother.getChildren()) {
  102.             System.out.print("Child: ");
  103.             System.out.print(node.getParent().getData());
  104.             System.out.print("        Parent: ");
  105.             System.out.println(node.getData());
  106.         }
  107.  
  108.         for(MyTreeNode node : mGF.getChildren()) {
  109.             System.out.print("Child: ");
  110.             System.out.print(node.getParent().getData());
  111.             System.out.print("        Parent: ");
  112.             System.out.println(node.getData());
  113.         }
  114.  
  115.         for(MyTreeNode node : mGM.getChildren()) {
  116.             System.out.print("Child: ");
  117.             System.out.print(node.getParent().getData());
  118.             System.out.print("        Parent: ");
  119.             System.out.println(node.getData());
  120.         }
  121.  
  122.         for(MyTreeNode node : fGM.getChildren()) {
  123.             System.out.print("Child: ");
  124.             System.out.print(node.getParent().getData());
  125.             System.out.print("        Parent: ");
  126.             System.out.println(node.getData());
  127.         }
  128.  
  129.         for(MyTreeNode node : fGM.getChildren()) {
  130.             System.out.print("Child: ");
  131.             System.out.print(node.getParent().getData());
  132.             System.out.print("        Parent: ");
  133.             System.out.println(node.getData());
  134.         }
  135.  
  136.     }
  137. }
  138. //======================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement