Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. static Scanner scanner = new Scanner(System.in);
  2.     static ArrayList<Node> pontok = new ArrayList<>();
  3.     static int[][] elek;
  4.     static int ciklusDb = 0;
  5.     static int elagazasDb = 0;
  6.    
  7.     static class Node{
  8.         int parent;
  9.         int ertek;
  10.         public ArrayList<Integer> szomszedok = new ArrayList<>();
  11.         Node(int a){
  12.             this.ertek = a;
  13.         }
  14.     }
  15.    
  16.     public static void main(String[] args)
  17.     {
  18.         int pontDb = 10;
  19.         int elDb = 12;
  20.         elek = new int[][]{
  21.                 {1, 2},
  22.                 {2, 3},
  23.                 {3, 4},
  24.                 {4, 6},
  25.                 {6, 7},
  26.                 {6, 8},
  27.                 {7, 4},
  28.                 {3, 5},
  29.                 {5, 9},
  30.                 {9, 10},
  31.                 {10, 5},
  32.                 {10, 8}
  33.         };
  34.        
  35.         pontok.add(new Node(1));
  36.        
  37.         for(int i = 2; i <= pontDb; i++) {
  38.             pontok.add(new Node(i));
  39.             GetNeighbours(pontok.get(i-1));
  40.             for(int j = 0; j < pontok.get(i-1).szomszedok.size(); j++) {
  41.                
  42.             }
  43.             pontok.get(i-1).szomszedok.clear();
  44.         }
  45.        
  46.         System.out.println(ciklusDb);
  47.         System.out.println(elagazasDb);
  48.     }
  49.  
  50.     private static void GetNeighbours(Node i) {
  51.         i.parent = GetParent(i);
  52.         for(int[] a : elek) {
  53.             if(a[0] == i.ertek && a[1] != i.parent) {
  54.                 i.szomszedok.add(a[1]);
  55.             }
  56.             else if(a[1] == i.ertek && a[0] != i.parent) {
  57.                 i.szomszedok.add(a[0]);
  58.             }
  59.         }
  60.         System.out.println(i.ertek + "'s parent is: " + i.parent);
  61.         System.out.print(i.ertek + "'s children are: ");
  62.         for(int a : i.szomszedok) {
  63.             System.out.print(a + " ");
  64.         }
  65.         System.out.println();
  66.     }
  67.  
  68.    
  69.     private static int GetParent(Node i) {
  70.         for(int[] a : elek) {
  71.             if(a[0] == i.ertek) {
  72.                 return a[1];
  73.             }
  74.             else if(a[1] == i.ertek) {
  75.                 return a[0];
  76.             }
  77.         }
  78.         return 0;
  79.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement