Advertisement
iNoobAvicena

Leaves From a Tree

Nov 27th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. class Node<T> {
  6.     T data;
  7.     LinkedList<Node> childs;
  8.    
  9.     public Node(T data) {
  10.         this.data = data;
  11.         this.childs = new LinkedList<Node>();
  12.     }
  13.    
  14.     public T getData(){
  15.         return this.data;
  16.     }
  17. }
  18.  
  19. public class Tree<T> {
  20.     Node root;
  21.    
  22.     public Tree(Node node) {
  23.         root = node;
  24.     }
  25.    
  26.     public Tree(T data) {
  27.         root = new Node(data);
  28.     }
  29.  
  30.     public boolean isEmpty() {
  31.         return root == null;
  32.     }
  33.    
  34.     public void add(T dataParent, T data) {
  35.         Node node = getNode(root, dataParent);
  36.         if (node != null)
  37.             node.childs.add(new Node(data));
  38.         else
  39.             System.out.println("Node Parent tidak ditemukan");
  40.     }
  41.    
  42.     public Node getNode(Node node, T data) {
  43.         if (node.data == data) return node;
  44.        
  45.         for(Object currNode : node.childs) {
  46.             Node returnNode = getNode((Node) currNode, data);
  47.             if (returnNode != null) return returnNode;
  48.         }
  49.         return null;
  50.     }
  51.    
  52.     public void printLeaves(Node current){
  53.         //LENGKAPI
  54.         if (!current.childs.isEmpty()) {
  55.             current.childs.forEach((child) -> {
  56.                 printLeaves((Node) child);
  57.             });
  58.         } else {
  59.             System.out.print(current.data + " ");
  60.         }
  61.     }
  62.  
  63.     public void access() {
  64.         System.out.print(root.data);
  65.         access(root, "  ");
  66.     }
  67.    
  68.     private void access(Node node, String space) {        
  69.         System.out.println("");
  70.         for(Object currNode : node.childs) {
  71.             System.out.print(space + "->" + ((Node) currNode).getData());            
  72.             access((Node) currNode, space + "  ");
  73.         }
  74.         return;
  75.     }
  76.    
  77.     public static void main(String[] args) {
  78.         Scanner in = new Scanner(System.in);
  79.         int n = in.nextInt();
  80.         int rt = in.nextInt();
  81.         Tree pohon = new Tree(new Node(rt));
  82.         for (int i = 0; i < n-1; i++) {
  83.             int p = in.nextInt();
  84.             int nd = in.nextInt();
  85.             pohon.add(p, nd);
  86.         }
  87.         pohon.access();
  88.         pohon.printLeaves(pohon.root);
  89.         System.out.println("");
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement