Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.StringTokenizer;
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- class BNode<E> {
- public E info;
- public BNode<E> left;
- public BNode<E> right;
- static int LEFT = 1;
- static int RIGHT = 2;
- public BNode(E info) {
- this.info = info;
- left = null;
- right = null;
- }
- public BNode() {
- this.info = null;
- left = null;
- right = null;
- }
- public BNode(E info, BNode<E> left, BNode<E> right) {
- this.info = info;
- this.left = left;
- this.right = right;
- }
- @Override
- public String toString() {
- return ""+info;
- }
- }
- class BTree<E> {
- public BNode<E> search(BNode<E>node,E sodrzina)
- {
- BNode<E> tmp = null;
- if(node ==null)
- return null;
- if(node.info.equals(sodrzina))
- {
- tmp = node;
- }
- else
- {
- if(node.left!=null)
- tmp = search(node.left,sodrzina);
- if( tmp==null&&node.right!=null)
- tmp = search(node.right,sodrzina);
- }
- return tmp;
- }
- public int smallestDistance(BNode<E> node,E jazol2,int kolkuDoSega)
- {
- if(node==null || search(root,jazol2)==null )
- {
- //ako nema nekoj od niv vrati ERR
- return -1;
- }
- if(node.info.equals(jazol2))
- {
- //se raboti za istiot jazol
- if(kolkuDoSega==0)
- return 0;
- return 2*kolkuDoSega;
- }
- if(node==search(root,jazol2))
- {
- if(kolkuDoSega==0)
- return 0;
- return 2*kolkuDoSega;
- }
- else
- {
- if(node.left!=null&&node.right!=null)
- {
- if(search(node.left, jazol2)!=null)
- return smallestDistance(node.left,jazol2,kolkuDoSega+1);
- else
- return smallestDistance(node.right,jazol2,kolkuDoSega+1);
- }
- else if(node.left!=null)
- return smallestDistance(node.left,jazol2,kolkuDoSega+1);
- else if(node.right!=null)
- return smallestDistance(node.right,jazol2,kolkuDoSega+1);
- else
- return 0;
- }
- }
- public BNode<E> lowestCommonAncestor(BNode<E> root, BNode<E> p, BNode<E> q) {
- /*
- * Our base cases. How our recursion stops. When we have an answer.
- *
- * 1.) If the node we are holding is null then we can't search...just return
- * null 2.) If we find either value return ourselves to the caller
- *
- * Remember, we are "grabbing"/"holding" 'root' in this call.
- */
- if (root == null)
- return null;
- if (root.info == p.info || root.info == q.info)
- return root;
- /*
- * 'root' doesn't satisfy any of our base cases.
- *
- * Search left and then search right.
- */
- BNode<E> leftSearchResult = lowestCommonAncestor(root.left, p, q);
- BNode<E> rightSearchResult = lowestCommonAncestor(root.right, p, q);
- /*
- * If nothing turned up on the left, return whatever we got back on the right.
- */
- if (leftSearchResult == null)
- return rightSearchResult;
- /*
- * If nothing turned up on the right, return whatever we got back on the left.
- */
- if (rightSearchResult == null)
- return leftSearchResult;
- /*
- * If we reach here that means we got something back on the left AND
- * right...that means this node is the LCA...because our recursion returns from
- * bottom to up...so we return what we hold...'root'
- */
- return root;
- }
- public int findSmallest(BNode<E> node1,BNode<E> node2)
- {
- //ako i dvete se vo isto poddrvo na korenot
- if(search(root.left,node1.info)!=null && search(root.left,node2.info)!=null)
- {
- /*System.out.print("BO IFOT SUM");
- System.out.println(smallestDistance(node1,node2,0));*/
- //System.out.println("111111");
- if(smallestDistance(node1,node2.info,0)==0 && !node1.info.equals(node2.info) && smallestDistance(root,node1.info,0)==smallestDistance(root,node2.info,0))
- {
- //System.out.println("22222222");
- return 4;
- }
- else
- if(smallestDistance(node1,node2.info,0)==0 && !node1.info.equals(node2.info) && smallestDistance(root,node1.info,0)!=smallestDistance(root,node2.info,0))
- {
- //System.out.println("LCA E ");
- //System.out.println(lowestCommonAncestor(root,node1,node2).info);
- if(lowestCommonAncestor(root,node1,node2).info.equals(node1) && lowestCommonAncestor(root,node1,node2).info.equals(node2))
- {
- //System.out.println("!!!!!");
- return 2;
- }
- //System.out.println("44444");
- return smallestDistance(root,node1.info,0) + smallestDistance(root,node2.info,0)-2*smallestDistance(root,lowestCommonAncestor(root,node1,node2).info,0);
- }
- return smallestDistance(node1,node2.info,0);
- }
- else if(search(root.right,node1.info)!=null && search(root.right,node2.info)!=null)
- {
- if(lowestCommonAncestor(root,node1,node2).info.equals(node1) || lowestCommonAncestor(root,node1,node2).info.equals(node2))
- {
- System.out.println("!!!!!");
- return 2;
- }
- //System.out.println("5555555555555");
- if(lowestCommonAncestor(root,node1,node2).info.equals(node1) || lowestCommonAncestor(root,node1,node2).info.equals(node2))
- {
- System.out.println("!!!!!");
- return 2;
- }
- if(smallestDistance(node1,node2.info,0)==0 && !node1.info.equals(node2.info) )
- {
- //System.out.println("LCA E ");
- //System.out.println(lowestCommonAncestor(root,node1,node2).info);
- if(lowestCommonAncestor(root,node1,node2).info.equals(node1) || lowestCommonAncestor(root,node1,node2).info.equals(node2))
- {
- System.out.println("!!!!!");
- return 2;
- }
- //System.out.print("povikuvam so LCA");
- if(lowestCommonAncestor(root,node1,node2).info.equals(node1) || lowestCommonAncestor(root,node1,node2).info.equals(node2))
- {
- System.out.println("!!!!!");
- return 2;
- }
- if(smallestDistance(node1,node2.info,0)==0 && !node1.info.equals(node2.info)&& smallestDistance(lowestCommonAncestor(root,node1,node2),node1.info,0)==2)
- {return 4;
- }
- return smallestDistance(root,node1.info,0) + smallestDistance(root,node2.info,0)-2*smallestDistance(root,lowestCommonAncestor(root,node1,node2).info,0);
- }
- else if(smallestDistance(node1,node2.info,0)==0 && !node1.info.equals(node2.info)&& smallestDistance(lowestCommonAncestor(root,node1,node2),node1.info,0)==2)
- {
- //System.out.print("povikuvam so LCA ama pogreshno ancestor e" + lowestCommonAncestor(root,node1,node2).info );
- //System.out.println(" " + smallestDistance(lowestCommonAncestor(root,node1,node2),node1.info,0));
- return 4;
- }
- return smallestDistance(node1,node2.info,0);
- }
- //ako prvoto e vo levo vtoroto vo desno poddrvo
- else if(search(root.left,node1.info)!=null && search(root.right,node2.info)!=null)
- {
- return smallestDistance(root,node1.info,0) + smallestDistance(root,node2.info,0) ;
- }
- else
- return smallestDistance(root,node1.info,0) + smallestDistance(root,node2.info,0);
- }
- public BNode<E> root;
- public BTree() {
- root = null;
- }
- public BTree(E info) {
- root = new BNode<E>(info);
- }
- public void makeRoot(E elem) {
- root = new BNode(elem);
- }
- public void makeRootNode(BNode<E> node) {
- root = node;
- }
- public BNode<E> addChild(BNode<E> node, int where, E elem) {
- BNode<E> tmp = new BNode<E>(elem);
- if (where == BNode.LEFT) {
- if (node.left != null) // veke postoi element
- return null;
- node.left = tmp;
- } else {
- if (node.right != null) // veke postoi element
- return null;
- node.right = tmp;
- }
- return tmp;
- }
- public BNode<E> addChildNode(BNode<E> node, int where, BNode<E> tmp) {
- if (where == BNode.LEFT) {
- if (node.left != null) // veke postoi element
- return null;
- node.left = tmp;
- } else {
- if (node.right != null) // veke postoi element
- return null;
- node.right = tmp;
- }
- return tmp;
- }
- }
- interface Stack<E> {
- // Elementi na stekot se objekti od proizvolen tip.
- // Metodi za pristap:
- public boolean isEmpty ();
- // Vrakja true ako i samo ako stekot e prazen.
- public E peek ();
- // Go vrakja elementot na vrvot od stekot.
- // Metodi za transformacija:
- public void clear ();
- // Go prazni stekot.
- public void push (E x);
- // Go dodava x na vrvot na stekot.
- public E pop ();
- // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
- }
- class ArrayStack<E> implements Stack<E> {
- private E[] elems;
- private int depth;
- @SuppressWarnings("unchecked")
- public ArrayStack (int maxDepth) {
- // Konstrukcija na nov, prazen stek.
- elems = (E[]) new Object[maxDepth];
- depth = 0;
- }
- public boolean isEmpty () {
- // Vrakja true ako i samo ako stekot e prazen.
- return (depth == 0);
- }
- public E peek () {
- // Go vrakja elementot na vrvot od stekot.
- if (depth == 0)
- throw new NoSuchElementException();
- return elems[depth-1];
- }
- public void clear () {
- // Go prazni stekot.
- for (int i = 0; i < depth; i++) elems[i] = null;
- depth = 0;
- }
- public void push (E x) {
- // Go dodava x na vrvot na stekot.
- elems[depth++] = x;
- }
- public E pop () {
- // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
- if (depth == 0)
- throw new NoSuchElementException();
- E topmost = elems[--depth];
- elems[depth] = null;
- return topmost;
- }
- }
- public class NodeDistance {
- public static void main(String args[]) throws NumberFormatException, IOException
- {
- int brojJazli;
- Scanner in = new Scanner(System.in);
- brojJazli = in.nextInt();
- //declare the tree
- BTree<String> tree = new BTree<String>();
- // niza od jazlite
- BNode<String> niza[] = new BNode[brojJazli];
- for(int i=0;i<brojJazli;i++)
- {
- niza[i] = new BNode<String>();
- }
- String linija = in.nextLine();
- for(int i=0;i<brojJazli;i++)
- {
- linija = in.nextLine();
- //System.out.println("Linijata e " + linija);
- String[] linijaS = linija.split(" ");
- int index = Integer.parseInt(linijaS[0]);
- //System.out.println("Indeksot e " + index);
- String value = linijaS[1];
- String where = linijaS[2];
- //System.out.println(where.equals("LEFT")? "left": "right");
- niza[index].info = value;
- int whoseChild;
- if(linijaS.length ==4)
- {
- whoseChild = Integer.parseInt(linijaS[3]);
- //System.out.println("the child is from" + whoseChild );
- if(where.equals("LEFT"))
- {
- tree.addChildNode(niza[whoseChild],1,niza[index]);
- }
- else if(where.equals("RIGHT"))
- {
- tree.addChildNode(niza[whoseChild],2,niza[index]);
- }
- }
- else
- {
- tree.makeRootNode(niza[index]);
- }
- }
- //System.out.println(tree.depth());
- //tree.print();
- // System.out.println("Jazolot c e" + tree.search(tree.root, "c").info);
- BNode<String> node = tree.root;
- int n = in.nextInt();
- //System.out.println(n);
- String jazli;
- String[] izraz;
- jazli = in.nextLine();
- for(int k=0;k<n;k++)
- {
- jazli = in.nextLine();
- izraz = jazli.split(" ");
- //System.out.println("Izrazot e :" + jazli);
- /*System.out.println(izraz[0]);
- System.out.println(izraz[1]);*/
- System.out.println(tree.smallestDistance(tree.search(node, izraz[0]),izraz[1],0 ) != -1 ? tree.findSmallest(tree.search(node, izraz[0]),tree.search(node, izraz[1]) ) :"ERROR" );
- //System.out.println(tree.search(node, izraz[0]).info);
- //System.out.println(izraz[1]);
- //System.out.println(tree.search(node, izraz[1]).info);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement