Filip_Markoski

[ADSx] - Distance In A Tree

Jan 8th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.66 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.ArrayDeque;
  4. import java.util.Queue;
  5. import java.util.StringTokenizer;
  6. import java.util.NoSuchElementException;
  7.  
  8. class BNode<E> {
  9.  
  10.     public E info;
  11.     public BNode<E> left;
  12.     public BNode<E> right;
  13.  
  14.     static int LEFT = 1;
  15.     static int RIGHT = 2;
  16.  
  17.     public BNode(E info) {
  18.         this.info = info;
  19.         left = null;
  20.         right = null;
  21.     }
  22.  
  23.     public BNode() {
  24.         this.info = null;
  25.         left = null;
  26.         right = null;
  27.     }
  28.  
  29.     public BNode(E info, BNode<E> left, BNode<E> right) {
  30.         this.info = info;
  31.         this.left = left;
  32.         this.right = right;
  33.     }
  34.  
  35.     @Override
  36.     public String toString() {
  37.         return "" + info;
  38.     }
  39. }
  40.  
  41. class BTree<E> {
  42.  
  43.     public BNode<E> root;
  44.  
  45.     public BTree() {
  46.         root = null;
  47.     }
  48.  
  49.     public BTree(E info) {
  50.         root = new BNode<E>(info);
  51.     }
  52.  
  53.     public void makeRootNode(BNode<E> node) {
  54.         root = node;
  55.     }
  56.    
  57.     public BNode<E> addChildNode(BNode<E> node, int where, BNode<E> tmp) {
  58.  
  59.         if (where == BNode.LEFT) {
  60.             if (node.left != null)  // veke postoi element
  61.                 return null;
  62.             node.left = tmp;
  63.         } else {
  64.             if (node.right != null) // veke postoi element
  65.                 return null;
  66.             node.right = tmp;
  67.         }
  68.  
  69.         return tmp;
  70.     }
  71.  
  72.     public void inOrder(BNode<E> node) {
  73.         if (node == null) return;
  74.         if (node.left != null) inOrder(node.left);
  75.         System.out.print(node.info.toString() + " ");
  76.         if (node.right != null) inOrder(node.right);
  77.     }
  78.  
  79.     public void levelOrderQueue(BNode<E> node) {
  80.         Queue<BNode<E>> queue = new ArrayDeque<>();
  81.         int levelNodes = 0;
  82.         if (node == null) return;
  83.         queue.add(node);
  84.         while (!queue.isEmpty()) {
  85.             levelNodes = queue.size();
  86.             while (levelNodes > 0) {
  87.                 BNode<E> poll = queue.poll();
  88.                 System.out.print(" " + poll.info.toString());
  89.                 if (poll.left != null) queue.add(poll.left);
  90.                 if (poll.right != null) queue.add(poll.right);
  91.                 levelNodes--;
  92.             }
  93.             System.out.print(System.lineSeparator());
  94.         }
  95.     }
  96.  
  97.     public BNode<E> findNodeByData(BNode<E> node, E info) {
  98.         if (node == null) return null;
  99.         if (node.info.equals(info)) return node;
  100.         BNode<E> left = findNodeByData(node.left, info);
  101.         return left == null ? findNodeByData(node.right, info) : left;
  102.     }
  103.  
  104.     public BNode<E> LeastCommonAncestor(BNode<E> current, BNode<E> one, BNode<E> two) {
  105.         if (current == null) return null;
  106.         if (current == one || current == two) return current;
  107.         BNode<E> left = LeastCommonAncestor(current.left, one, two);
  108.         BNode<E> right = LeastCommonAncestor(current.right, one, two);
  109.         if (left != null && right != null) return current;
  110.         return left == null ? right : left;
  111.     }
  112.  
  113.     public int depth(BNode<E> current, BNode<E> target) {
  114.         if (current == null) return -1;
  115.         if (current == target) return 0;
  116.         int left = depth(current.left, target);
  117.         int right = depth(current.right, target);
  118.         if (left == -1 && right == -1) return -1;
  119.         return left == -1 ? right + 1 : left + 1;
  120.     }
  121.  
  122.     public int distanceBetween(BNode<E> root, BNode<E> one, BNode<E> two) {
  123.         if (root == null || one == null || two == null) return -1;
  124.         BNode<E> ancestor = LeastCommonAncestor(root, one, two);
  125.         int depth1 = depth(root, ancestor);
  126.         int depth2 = depth(root, one);
  127.         int depth3 = depth(root, two);
  128.         return depth2 + depth3 - 2 * depth1;
  129.     }
  130.  
  131.     public void distanceBetweenByInfo(BNode<E> root, E infoOne, E infoTwo) {
  132.         BNode<E> one = findNodeByData(root, infoOne);
  133.         BNode<E> two = findNodeByData(root, infoTwo);
  134.         int distanceBetween = distanceBetween(root, one, two);
  135.         if (distanceBetween == -1) System.out.println("ERROR");
  136.         else System.out.println(distanceBetween * 2);
  137.     }
  138. }
  139.  
  140. public class NodeDistance {
  141.  
  142.     public static void main(String[] args) throws Exception {
  143.         int i, j, k;
  144.         int index;
  145.         String action;
  146.  
  147.         String line;
  148.  
  149.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  150.         StringTokenizer st;
  151.  
  152.         int N = Integer.parseInt(br.readLine());
  153.  
  154.         BNode<String> nodes[] = new BNode[N];
  155.         BTree<String> tree = new BTree<String>();
  156.  
  157.         for (i = 0; i < N; i++)
  158.             nodes[i] = new BNode<String>();
  159.  
  160.         for (i = 0; i < N; i++) {
  161.             line = br.readLine();
  162.             st = new StringTokenizer(line);
  163.             index = Integer.parseInt(st.nextToken());
  164.             nodes[index].info = st.nextToken();
  165.             action = st.nextToken();
  166.             if (action.equals("LEFT")) {
  167.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.LEFT, nodes[index]);
  168.             } else if (action.equals("RIGHT")) {
  169.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.RIGHT, nodes[index]);
  170.             } else {
  171.                 // this node is the root
  172.                 tree.makeRootNode(nodes[index]);
  173.             }
  174.         }
  175.  
  176.         /*tree.inOrder(tree.root);
  177.         System.out.println();
  178.         tree.levelOrderQueue(tree.root);*/
  179.  
  180.         int cases = Integer.parseInt(br.readLine());
  181.         for (int l = 0; l < cases; l++) {
  182.             String split[] = br.readLine().split(" +");
  183.             String from = split[0];
  184.             String to = split[1];
  185.  
  186.             tree.distanceBetweenByInfo(tree.root, from, to);
  187.         }
  188.         br.close();
  189.  
  190.  
  191.     }
  192.  
  193. }
  194. /**
  195.  * You are given a filled tree, where each edge has a weight of 2. For each pair of nodes,
  196.  * find the shortest distance between them. If there is no solution, print "Error".
  197.  * <p>
  198.  * Name of the class (Java): NodeDistance.
  199.  */
  200.  
  201.  
  202.  
  203. interface Stack<E> {
  204.  
  205.     // Elementi na stekot se objekti od proizvolen tip.
  206.  
  207.     // Metodi za pristap:
  208.  
  209.     public boolean isEmpty();
  210.     // Vrakja true ako i samo ako stekot e prazen.
  211.  
  212.     public E peek();
  213.     // Go vrakja elementot na vrvot od stekot.
  214.  
  215.     // Metodi za transformacija:
  216.  
  217.     public void clear();
  218.     // Go prazni stekot.
  219.  
  220.     public void push(E x);
  221.     // Go dodava x na vrvot na stekot.
  222.  
  223.     public E pop();
  224.     // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  225. }
  226.  
  227. class ArrayStack<E> implements Stack<E> {
  228.     private E[] elems;
  229.     private int depth;
  230.  
  231.     @SuppressWarnings("unchecked")
  232.     public ArrayStack(int maxDepth) {
  233.         // Konstrukcija na nov, prazen stek.
  234.         elems = (E[]) new Object[maxDepth];
  235.         depth = 0;
  236.     }
  237.  
  238.  
  239.     public boolean isEmpty() {
  240.         // Vrakja true ako i samo ako stekot e prazen.
  241.         return (depth == 0);
  242.     }
  243.  
  244.  
  245.     public E peek() {
  246.         // Go vrakja elementot na vrvot od stekot.
  247.         if (depth == 0)
  248.             throw new NoSuchElementException();
  249.         return elems[depth - 1];
  250.     }
  251.  
  252.  
  253.     public void clear() {
  254.         // Go prazni stekot.
  255.         for (int i = 0; i < depth; i++) elems[i] = null;
  256.         depth = 0;
  257.     }
  258.  
  259.  
  260.     public void push(E x) {
  261.         // Go dodava x na vrvot na stekot.
  262.         elems[depth++] = x;
  263.     }
  264.  
  265.  
  266.     public E pop() {
  267.         // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  268.         if (depth == 0)
  269.             throw new NoSuchElementException();
  270.         E topmost = elems[--depth];
  271.         elems[depth] = null;
  272.         return topmost;
  273.     }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment