Advertisement
nocturnalmk

NodeDistance.java

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