atanasovetr

TreeImplementationDFS

Mar 25th, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. import java.util.*;
  2. public class TreeImplementationDFS {
  3.  
  4.     public static void main(String[] args) {
  5.         Node<String> root = createTree();
  6.         printTree(root, " ");
  7.     }
  8.  
  9.     private static Node<String> createTree() {
  10.         Node<String> root = new Node<>("Роза и Иван");
  11.  
  12.         Node<String> Father = root.addChild(new Node<String>("Атанас"));
  13.         Node<String> Aunt1 = root.addChild(new Node<String>("Стефка"));
  14.         Node<String> Aunt2 = root.addChild(new Node<String>("Надка"));
  15.  
  16.         Node<String> Brother = Father.addChild(new Node<String>("Росен"));
  17.         Node<String> Sister = Father.addChild(new Node<String>("Вяра"));
  18.  
  19.         Node<String> Aunt1Cousin1 = Aunt1.addChild(new Node<String>("Иван"));
  20.  
  21.         Node<String> Aunt2Cousin1 = Aunt2.addChild(new Node<String>("Ивайло"));
  22.         Node<String> Aunt2Cousin2 = Aunt2.addChild(new Node<String>("Веселин"));
  23.  
  24.         Node<String> Aunt2Cousin1Child1 = Aunt2Cousin1.addChild(new Node<String>("Преслава"));
  25.         Node<String> Aunt2Cousin1Child2 = Aunt2Cousin1.addChild(new Node<String>("Радостин"));
  26.  
  27.         Node<String> Aunt2Cousin2Child1 = Aunt2Cousin2.addChild(new Node<String>("Теодора"));
  28.         Node<String> Aunt2Cousin2Child2 = Aunt2Cousin2.addChild(new Node<String>("Христяна"));
  29.         Node<String> Aunt2Cousin2Child3 = Aunt2Cousin2.addChild(new Node<String>("Никол"));
  30.  
  31.         return root;
  32.     }
  33.  
  34.     private static <T> void printTree(Node<T> node, String appender) {
  35.         System.out.println(appender + node.getData());
  36.         node.getChildren().forEach(each ->  printTree(each, appender + appender));
  37.     }
  38. }
  39.  class Node<T> {
  40.  
  41.     private T data = null;
  42.  
  43.     private List<Node<T>> children = new ArrayList<>();
  44.  
  45.     private Node<T> parent = null;
  46.  
  47.     public Node(T data) {
  48.         this.data = data;
  49.     }
  50.  
  51.     public Node<T> addChild(Node<T> child) {
  52.         child.setParent(this);
  53.         this.children.add(child);
  54.         return child;
  55.     }
  56.  
  57.     public void addChildren(List<Node<T>> children) {
  58.         children.forEach(each -> each.setParent(this));
  59.         this.children.addAll(children);
  60.     }
  61.  
  62.     public List<Node<T>> getChildren() {
  63.         return children;
  64.     }
  65.  
  66.     public T getData() {
  67.         return data;
  68.     }
  69.  
  70.     public void setData(T data) {
  71.         this.data = data;
  72.     }
  73.  
  74.     private void setParent(Node<T> parent) {
  75.         this.parent = parent;
  76.     }
  77.  
  78.     public Node<T> getParent() {
  79.         return parent;
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment