Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.99 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.util.StringTokenizer;
  5. import java.util.NoSuchElementException;
  6. import java.util.Scanner;
  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. public BNode<E> search(BNode<E>node,E sodrzina)
  43. {
  44. BNode<E> tmp = null;
  45. if(node ==null)
  46. return null;
  47. if(node.info.equals(sodrzina))
  48. {
  49. tmp = node;
  50. }
  51. else
  52. {
  53. if(node.left!=null)
  54. tmp = search(node.left,sodrzina);
  55. if( tmp==null&&node.right!=null)
  56. tmp = search(node.right,sodrzina);
  57.  
  58.  
  59. }
  60.  
  61. return tmp;
  62. }
  63.  
  64. public int smallestDistance(BNode<E> node,E jazol2,int kolkuDoSega)
  65. {
  66.  
  67.  
  68. if(node==null || search(root,jazol2)==null )
  69. {
  70. //ako nema nekoj od niv vrati ERR
  71. return -1;
  72. }
  73. if(node.info.equals(jazol2))
  74. {
  75. //se raboti za istiot jazol
  76. if(kolkuDoSega==0)
  77. return 0;
  78. return 2*kolkuDoSega;
  79. }
  80. if(node==search(root,jazol2))
  81. {
  82. if(kolkuDoSega==0)
  83. return 0;
  84. return 2*kolkuDoSega;
  85. }
  86. else
  87. {
  88. if(node.left!=null&&node.right!=null)
  89. {
  90. if(search(node.left, jazol2)!=null)
  91. return smallestDistance(node.left,jazol2,kolkuDoSega+1);
  92. else
  93. return smallestDistance(node.right,jazol2,kolkuDoSega+1);
  94. }
  95. else if(node.left!=null)
  96. return smallestDistance(node.left,jazol2,kolkuDoSega+1);
  97. else if(node.right!=null)
  98. return smallestDistance(node.right,jazol2,kolkuDoSega+1);
  99. else
  100. return 0;
  101.  
  102. }
  103. }
  104.  
  105. public BNode<E> lowestCommonAncestor(BNode<E> root, BNode<E> p, BNode<E> q) {
  106.  
  107. /*
  108. * Our base cases. How our recursion stops. When we have an answer.
  109. *
  110. * 1.) If the node we are holding is null then we can't search...just return
  111. * null 2.) If we find either value return ourselves to the caller
  112. *
  113. * Remember, we are "grabbing"/"holding" 'root' in this call.
  114. */
  115. if (root == null)
  116. return null;
  117. if (root.info == p.info || root.info == q.info)
  118. return root;
  119.  
  120. /*
  121. * 'root' doesn't satisfy any of our base cases.
  122. *
  123. * Search left and then search right.
  124. */
  125. BNode<E> leftSearchResult = lowestCommonAncestor(root.left, p, q);
  126. BNode<E> rightSearchResult = lowestCommonAncestor(root.right, p, q);
  127.  
  128. /*
  129. * If nothing turned up on the left, return whatever we got back on the right.
  130. */
  131. if (leftSearchResult == null)
  132. return rightSearchResult;
  133.  
  134. /*
  135. * If nothing turned up on the right, return whatever we got back on the left.
  136. */
  137. if (rightSearchResult == null)
  138. return leftSearchResult;
  139.  
  140. /*
  141. * If we reach here that means we got something back on the left AND
  142. * right...that means this node is the LCA...because our recursion returns from
  143. * bottom to up...so we return what we hold...'root'
  144. */
  145. return root;
  146. }
  147.  
  148.  
  149.  
  150. public int findSmallest(BNode<E> node1,BNode<E> node2)
  151. {
  152. //ako i dvete se vo isto poddrvo na korenot
  153. if(search(root.left,node1.info)!=null && search(root.left,node2.info)!=null)
  154. {
  155. /*System.out.print("BO IFOT SUM");
  156.  
  157. System.out.println(smallestDistance(node1,node2,0));*/
  158. //System.out.println("111111");
  159. if(smallestDistance(node1,node2.info,0)==0 && !node1.info.equals(node2.info) && smallestDistance(root,node1.info,0)==smallestDistance(root,node2.info,0))
  160. {
  161. //System.out.println("22222222");
  162. return 4;
  163. }
  164. else
  165. if(smallestDistance(node1,node2.info,0)==0 && !node1.info.equals(node2.info) && smallestDistance(root,node1.info,0)!=smallestDistance(root,node2.info,0))
  166. {
  167. //System.out.println("LCA E ");
  168.  
  169. //System.out.println(lowestCommonAncestor(root,node1,node2).info);
  170. if(lowestCommonAncestor(root,node1,node2).info.equals(node1) && lowestCommonAncestor(root,node1,node2).info.equals(node2))
  171. {
  172. //System.out.println("!!!!!");
  173. return 2;
  174. }
  175. //System.out.println("44444");
  176. return smallestDistance(root,node1.info,0) + smallestDistance(root,node2.info,0)-2*smallestDistance(root,lowestCommonAncestor(root,node1,node2).info,0);
  177. }
  178.  
  179. return smallestDistance(node1,node2.info,0);
  180. }
  181. else if(search(root.right,node1.info)!=null && search(root.right,node2.info)!=null)
  182. {
  183. if(lowestCommonAncestor(root,node1,node2).info.equals(node1) || lowestCommonAncestor(root,node1,node2).info.equals(node2))
  184. {
  185. System.out.println("!!!!!");
  186. return 2;
  187. }
  188.  
  189. //System.out.println("5555555555555");
  190. if(lowestCommonAncestor(root,node1,node2).info.equals(node1) || lowestCommonAncestor(root,node1,node2).info.equals(node2))
  191. {
  192. System.out.println("!!!!!");
  193. return 2;
  194. }
  195.  
  196. if(smallestDistance(node1,node2.info,0)==0 && !node1.info.equals(node2.info) )
  197. {
  198. //System.out.println("LCA E ");
  199. //System.out.println(lowestCommonAncestor(root,node1,node2).info);
  200.  
  201. if(lowestCommonAncestor(root,node1,node2).info.equals(node1) || lowestCommonAncestor(root,node1,node2).info.equals(node2))
  202. {
  203. System.out.println("!!!!!");
  204. return 2;
  205. }
  206. //System.out.print("povikuvam so LCA");
  207. if(lowestCommonAncestor(root,node1,node2).info.equals(node1) || lowestCommonAncestor(root,node1,node2).info.equals(node2))
  208. {
  209. System.out.println("!!!!!");
  210. return 2;
  211. }
  212.  
  213. if(smallestDistance(node1,node2.info,0)==0 && !node1.info.equals(node2.info)&& smallestDistance(lowestCommonAncestor(root,node1,node2),node1.info,0)==2)
  214. {return 4;
  215. }
  216. return smallestDistance(root,node1.info,0) + smallestDistance(root,node2.info,0)-2*smallestDistance(root,lowestCommonAncestor(root,node1,node2).info,0);
  217. }
  218. else if(smallestDistance(node1,node2.info,0)==0 && !node1.info.equals(node2.info)&& smallestDistance(lowestCommonAncestor(root,node1,node2),node1.info,0)==2)
  219. {
  220. //System.out.print("povikuvam so LCA ama pogreshno ancestor e" + lowestCommonAncestor(root,node1,node2).info );
  221. //System.out.println(" " + smallestDistance(lowestCommonAncestor(root,node1,node2),node1.info,0));
  222. return 4;
  223. }
  224.  
  225. return smallestDistance(node1,node2.info,0);
  226. }
  227. //ako prvoto e vo levo vtoroto vo desno poddrvo
  228. else if(search(root.left,node1.info)!=null && search(root.right,node2.info)!=null)
  229. {
  230. return smallestDistance(root,node1.info,0) + smallestDistance(root,node2.info,0) ;
  231. }
  232. else
  233. return smallestDistance(root,node1.info,0) + smallestDistance(root,node2.info,0);
  234.  
  235. }
  236.  
  237. public BNode<E> root;
  238.  
  239. public BTree() {
  240. root = null;
  241. }
  242.  
  243. public BTree(E info) {
  244. root = new BNode<E>(info);
  245. }
  246.  
  247. public void makeRoot(E elem) {
  248. root = new BNode(elem);
  249. }
  250.  
  251. public void makeRootNode(BNode<E> node) {
  252. root = node;
  253. }
  254.  
  255. public BNode<E> addChild(BNode<E> node, int where, E elem) {
  256.  
  257. BNode<E> tmp = new BNode<E>(elem);
  258.  
  259. if (where == BNode.LEFT) {
  260. if (node.left != null) // veke postoi element
  261. return null;
  262. node.left = tmp;
  263. } else {
  264. if (node.right != null) // veke postoi element
  265. return null;
  266. node.right = tmp;
  267. }
  268.  
  269. return tmp;
  270. }
  271.  
  272. public BNode<E> addChildNode(BNode<E> node, int where, BNode<E> tmp) {
  273.  
  274. if (where == BNode.LEFT) {
  275. if (node.left != null) // veke postoi element
  276. return null;
  277. node.left = tmp;
  278. } else {
  279. if (node.right != null) // veke postoi element
  280. return null;
  281. node.right = tmp;
  282. }
  283.  
  284. return tmp;
  285. }
  286.  
  287. }
  288.  
  289.  
  290.  
  291. interface Stack<E> {
  292.  
  293. // Elementi na stekot se objekti od proizvolen tip.
  294.  
  295. // Metodi za pristap:
  296.  
  297. public boolean isEmpty ();
  298. // Vrakja true ako i samo ako stekot e prazen.
  299.  
  300. public E peek ();
  301. // Go vrakja elementot na vrvot od stekot.
  302.  
  303. // Metodi za transformacija:
  304.  
  305. public void clear ();
  306. // Go prazni stekot.
  307.  
  308. public void push (E x);
  309. // Go dodava x na vrvot na stekot.
  310.  
  311. public E pop ();
  312. // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  313. }
  314.  
  315. class ArrayStack<E> implements Stack<E> {
  316. private E[] elems;
  317. private int depth;
  318.  
  319. @SuppressWarnings("unchecked")
  320. public ArrayStack (int maxDepth) {
  321. // Konstrukcija na nov, prazen stek.
  322. elems = (E[]) new Object[maxDepth];
  323. depth = 0;
  324. }
  325.  
  326.  
  327. public boolean isEmpty () {
  328. // Vrakja true ako i samo ako stekot e prazen.
  329. return (depth == 0);
  330. }
  331.  
  332.  
  333. public E peek () {
  334. // Go vrakja elementot na vrvot od stekot.
  335. if (depth == 0)
  336. throw new NoSuchElementException();
  337. return elems[depth-1];
  338. }
  339.  
  340.  
  341. public void clear () {
  342. // Go prazni stekot.
  343. for (int i = 0; i < depth; i++) elems[i] = null;
  344. depth = 0;
  345. }
  346.  
  347.  
  348. public void push (E x) {
  349. // Go dodava x na vrvot na stekot.
  350. elems[depth++] = x;
  351. }
  352.  
  353.  
  354. public E pop () {
  355. // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  356. if (depth == 0)
  357. throw new NoSuchElementException();
  358. E topmost = elems[--depth];
  359. elems[depth] = null;
  360. return topmost;
  361. }
  362. }
  363.  
  364. public class NodeDistance {
  365. public static void main(String args[]) throws NumberFormatException, IOException
  366. {
  367. int brojJazli;
  368. Scanner in = new Scanner(System.in);
  369. brojJazli = in.nextInt();
  370. //declare the tree
  371. BTree<String> tree = new BTree<String>();
  372. // niza od jazlite
  373.  
  374. BNode<String> niza[] = new BNode[brojJazli];
  375.  
  376. for(int i=0;i<brojJazli;i++)
  377. {
  378. niza[i] = new BNode<String>();
  379. }
  380. String linija = in.nextLine();
  381. for(int i=0;i<brojJazli;i++)
  382. {
  383. linija = in.nextLine();
  384. //System.out.println("Linijata e " + linija);
  385. String[] linijaS = linija.split(" ");
  386. int index = Integer.parseInt(linijaS[0]);
  387. //System.out.println("Indeksot e " + index);
  388. String value = linijaS[1];
  389. String where = linijaS[2];
  390. //System.out.println(where.equals("LEFT")? "left": "right");
  391. niza[index].info = value;
  392. int whoseChild;
  393. if(linijaS.length ==4)
  394. {
  395. whoseChild = Integer.parseInt(linijaS[3]);
  396. //System.out.println("the child is from" + whoseChild );
  397. if(where.equals("LEFT"))
  398. {
  399. tree.addChildNode(niza[whoseChild],1,niza[index]);
  400. }
  401. else if(where.equals("RIGHT"))
  402. {
  403. tree.addChildNode(niza[whoseChild],2,niza[index]);
  404. }
  405. }
  406. else
  407. {
  408. tree.makeRootNode(niza[index]);
  409. }
  410.  
  411. }
  412. //System.out.println(tree.depth());
  413. //tree.print();
  414. // System.out.println("Jazolot c e" + tree.search(tree.root, "c").info);
  415. BNode<String> node = tree.root;
  416. int n = in.nextInt();
  417. //System.out.println(n);
  418. String jazli;
  419. String[] izraz;
  420. jazli = in.nextLine();
  421. for(int k=0;k<n;k++)
  422. {
  423. jazli = in.nextLine();
  424. izraz = jazli.split(" ");
  425. //System.out.println("Izrazot e :" + jazli);
  426. /*System.out.println(izraz[0]);
  427. System.out.println(izraz[1]);*/
  428. 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" );
  429. //System.out.println(tree.search(node, izraz[0]).info);
  430. //System.out.println(izraz[1]);
  431. //System.out.println(tree.search(node, izraz[1]).info);
  432. }
  433. }
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement