Advertisement
nocturnalmk

NodeDistance

Dec 18th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.22 KB | None | 0 0
  1. package zad4;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6. import java.util.StringTokenizer;
  7. import java.util.NoSuchElementException;
  8.  
  9.  
  10. class BNode<E> {
  11.  
  12.     public E info;
  13.     public BNode<E> left;
  14.     public BNode<E> right;
  15.  
  16.     static int LEFT = 1;
  17.     static int RIGHT = 2;
  18.  
  19.     public BNode(E info) {
  20.         this.info = info;
  21.         left = null;
  22.         right = null;
  23.     }
  24.  
  25.     public BNode() {
  26.         this.info = null;
  27.         left = null;
  28.         right = null;
  29.     }
  30.  
  31.     public BNode(E info, BNode<E> left, BNode<E> right) {
  32.         this.info = info;
  33.         this.left = left;
  34.         this.right = right;
  35.     }
  36.  
  37.     @Override
  38.     public String toString() {
  39.         return "" + info;
  40.     }
  41. }
  42.  
  43. class BTree<E> {
  44.  
  45.     public BNode<E> root;
  46.  
  47.     public BTree() {
  48.         root = null;
  49.     }
  50.  
  51.     public BTree(E info) {
  52.         root = new BNode<E>(info);
  53.     }
  54.  
  55.     public void makeRoot(E elem) {
  56.         root = new BNode(elem);
  57.     }
  58.  
  59.     public void makeRootNode(BNode<E> node) {
  60.         root = node;
  61.     }
  62.  
  63.     public BNode<E> addChild(BNode<E> node, int where, E elem) {
  64.  
  65.         BNode<E> tmp = new BNode<E>(elem);
  66.  
  67.         if (where == BNode.LEFT) {
  68.             if (node.left != null) // veke postoi element
  69.                 return null;
  70.             node.left = tmp;
  71.         } else {
  72.             if (node.right != null) // veke postoi element
  73.                 return null;
  74.             node.right = tmp;
  75.         }
  76.  
  77.         return tmp;
  78.     }
  79.  
  80.     public BNode<E> addChildNode(BNode<E> node, int where, BNode<E> tmp) {
  81.  
  82.         if (where == BNode.LEFT) {
  83.             if (node.left != null) // veke postoi element
  84.                 return null;
  85.             node.left = tmp;
  86.         } else {
  87.             if (node.right != null) // veke postoi element
  88.                 return null;
  89.             node.right = tmp;
  90.         }
  91.  
  92.         return tmp;
  93.     }
  94.    
  95.     public BNode<E> getNode(E x) {
  96.         return getNode(this.root, x);
  97.     }
  98.    
  99.     private BNode<E> getNode(BNode<E> r, E x) {
  100.         if (r == null) {
  101.             return null;
  102.         }
  103.         if (r.info.equals(x)) {
  104.             return r;
  105.         }
  106.        
  107.         BNode<E> a = getNode(r.left, x);
  108.         BNode<E> b = getNode(r.right, x);
  109.        
  110.         if (a != null) {
  111.             return a;
  112.         } else {
  113.             return b;
  114.         }
  115.     }
  116.    
  117.     public BNode<E> getParent(BNode<E> node) {
  118.         return getParent(this.root, node);
  119.     }
  120.    
  121.     private BNode<E> getParent(BNode<E> n, BNode<E> node) {
  122.        
  123.         if (n == null) {
  124.             return null;
  125.         }
  126.         if (n.left == null && n.right == null) {
  127.             return null;
  128.         }
  129.        
  130.         if (n.left == node || n.right == node) {
  131.             return n;
  132.         }
  133.        
  134.         BNode<E> a = getParent(n.left, node);
  135.         BNode<E> b = getParent(n.right, node);
  136.        
  137.         if (a != null) {
  138.             return a;
  139.         } else {
  140.             return b;
  141.         }
  142.        
  143.     }
  144.    
  145.     public int isUnder(BNode<E> node, BNode<E> gorno) {
  146.        
  147.         int l = 0;
  148.        
  149.         if (node == gorno) {
  150.             return 0;
  151.         }
  152.        
  153.         if (getParent(node) == gorno) {
  154.             return 2;
  155.         }
  156.        
  157.         while (node != gorno) {
  158.             node = getParent(node);
  159.             l += 2;
  160.            
  161.             if (node == this.root) {
  162.                 return -1;
  163.             }
  164.         }
  165.        
  166.         return l;
  167.     }
  168.    
  169.     public BNode<E> findCommonRoot(BNode<E> n1, BNode<E> n2) {
  170.         return findCommonRoot(this.root, n1, n2);
  171.     }
  172.    
  173.     private BNode<E> findCommonRoot(BNode<E> root, BNode<E> n1, BNode<E> n2) {
  174.        
  175.         if (isUnder(n1, root.left) > 0 && isUnder(n2, root.left) > 0) {
  176.             return findCommonRoot(root.left, n1, n2);
  177.         } else if (isUnder(n1, root.right) > 0 && isUnder(n2, root.right) > 0) {
  178.             return findCommonRoot(root.right, n1, n2);
  179.         } else {
  180.             return root;
  181.         }
  182.        
  183.     }
  184.  
  185. }
  186.  
  187. interface Stack<E> {
  188.  
  189.     // Elementi na stekot se objekti od proizvolen tip.
  190.  
  191.     // Metodi za pristap:
  192.  
  193.     public boolean isEmpty();
  194.  
  195.     // Vrakja true ako i samo ako stekot e prazen.
  196.  
  197.     public E peek();
  198.  
  199.     // Go vrakja elementot na vrvot od stekot.
  200.  
  201.     // Metodi za transformacija:
  202.  
  203.     public void clear();
  204.  
  205.     // Go prazni stekot.
  206.  
  207.     public void push(E x);
  208.  
  209.     // Go dodava x na vrvot na stekot.
  210.  
  211.     public E pop();
  212.     // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  213. }
  214.  
  215. class ArrayStack<E> implements Stack<E> {
  216.     private E[] elems;
  217.     private int depth;
  218.  
  219.     @SuppressWarnings("unchecked")
  220.     public ArrayStack(int maxDepth) {
  221.         // Konstrukcija na nov, prazen stek.
  222.         elems = (E[]) new Object[maxDepth];
  223.         depth = 0;
  224.     }
  225.  
  226.     public boolean isEmpty() {
  227.         // Vrakja true ako i samo ako stekot e prazen.
  228.         return (depth == 0);
  229.     }
  230.  
  231.     public E peek() {
  232.         // Go vrakja elementot na vrvot od stekot.
  233.         if (depth == 0)
  234.             throw new NoSuchElementException();
  235.         return elems[depth - 1];
  236.     }
  237.  
  238.     public void clear() {
  239.         // Go prazni stekot.
  240.         for (int i = 0; i < depth; i++)
  241.             elems[i] = null;
  242.         depth = 0;
  243.     }
  244.  
  245.     public void push(E x) {
  246.         // Go dodava x na vrvot na stekot.
  247.         elems[depth++] = x;
  248.     }
  249.  
  250.     public E pop() {
  251.         // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  252.         if (depth == 0)
  253.             throw new NoSuchElementException();
  254.         E topmost = elems[--depth];
  255.         elems[depth] = null;
  256.         return topmost;
  257.     }
  258. }
  259.  
  260. public class NodeDistance {
  261.  
  262.     public static void main(String[] args) throws Exception {
  263.         int i, j, k;
  264.         int index;
  265.         String action;
  266.  
  267.         String line;
  268.  
  269.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  270.         StringTokenizer st;
  271.  
  272.         int N = Integer.parseInt(br.readLine());
  273.  
  274.         BNode<String> nodes[] = new BNode[N];
  275.         BTree<String> tree = new BTree<String>();
  276.  
  277.         for (i = 0; i < N; i++)
  278.             nodes[i] = new BNode<String>();
  279.  
  280.         for (i = 0; i < N; i++) {
  281.             line = br.readLine();
  282.             st = new StringTokenizer(line);
  283.             index = Integer.parseInt(st.nextToken());
  284.             nodes[index].info = st.nextToken();
  285.             action = st.nextToken();
  286.             if (action.equals("LEFT")) {
  287.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())],
  288.                         BNode.LEFT, nodes[index]);
  289.             } else if (action.equals("RIGHT")) {
  290.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())],
  291.                         BNode.RIGHT, nodes[index]);
  292.             } else {
  293.                 // this node is the root
  294.                 tree.makeRootNode(nodes[index]);
  295.             }
  296.         }
  297.  
  298.         int cases = Integer.parseInt(br.readLine());
  299.         for (int l = 0; l < cases; l++) {
  300.             String split[] = br.readLine().split(" ");
  301.             String from = split[0];
  302.             String to = split[1];
  303.  
  304.             BNode<String> fromNode = tree.getNode(from);
  305.             BNode<String> toNode = tree.getNode(to);
  306.            
  307.             if (fromNode == null || toNode == null) {
  308.                 System.out.println("ERROR");
  309.                 continue;
  310.             }
  311.            
  312.            
  313.             int x = tree.isUnder(fromNode, toNode);
  314.             int y = tree.isUnder(toNode, fromNode);
  315.            
  316.            
  317.             System.out.println(x + " " + y);
  318.         }
  319.         br.close();
  320.  
  321.     }
  322.  
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement