Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class Main
  7. {
  8. private static class Node
  9. {
  10. private Node left;
  11. private Node right;
  12. private int value;
  13.  
  14. public Node(int value)
  15. {
  16. this.value = value;
  17. }
  18.  
  19. public String toString()
  20. {
  21. return " Value: " + value + "\n";
  22. }
  23. }
  24.  
  25. private static void insert(int value, Node root)
  26. {
  27. if( value < root.value )
  28. {
  29. if( root.left != null )
  30. {
  31. insert(value,root.left);
  32. }
  33. else
  34. {
  35. root.left = new Node(value);
  36. }
  37. }
  38. else
  39. {
  40. if( root.right != null )
  41. {
  42. insert(value,root.right);
  43. }
  44. else
  45. {
  46. root.right = new Node(value);
  47. }
  48. }
  49. }
  50. private static Node populate(int[] values, int n)
  51. {
  52. if( n <= 0 )
  53. {
  54. return null;
  55. }
  56. Node root = new Node(values[0]);
  57. for( int i =1 ; i < n ; i ++ )
  58. {
  59. insert(values[i],root);
  60. }
  61. return root;
  62. }
  63. public static void main(String[] args) {
  64. Main man = new Main();
  65. }
  66.  
  67. public static int bstDistance(int[] values, int n, int node1, int node2) {
  68.  
  69. Node node = populate(values,n);
  70. return findDistance(node,node1,node2);
  71. }
  72. public static int findDistance(Node root, int n1, int n2) {
  73. int x = Pathlength(root, n1) - 1;
  74. int y = Pathlength(root, n2) - 1;
  75. if( x < 0 || y < 0 )
  76. {
  77. return -1;
  78. }
  79. int lcaData = findLCA(root, n1, n2).value;
  80. int lcaDistance = Pathlength(root, lcaData) - 1;
  81. return (x + y) - 2 * lcaDistance;
  82. }
  83.  
  84. public static int Pathlength(Node root, int n1) {
  85. if (root != null) {
  86. int x = 0;
  87. if ((root.value == n1) || (x = Pathlength(root.left, n1)) > 0
  88. || (x = Pathlength(root.right, n1)) > 0) {
  89. // System.out.println(root.value);
  90. return x + 1;
  91. }
  92. return -1;
  93. }
  94. return -1;
  95. }
  96.  
  97. public static Node findLCA(Node root, int n1, int n2) {
  98. if (root != null) {
  99. if (root.value == n1 || root.value == n2) {
  100. return root;
  101. }
  102. Node left = findLCA(root.left, n1, n2);
  103. Node right = findLCA(root.right, n1, n2);
  104.  
  105. if (left != null && right != null) {
  106. return root;
  107. }
  108. if (left != null) {
  109. return left;
  110. }
  111. if (right != null) {
  112. return right;
  113. }
  114. }
  115. return null;
  116. }
  117.  
  118.  
  119. private void print(Node tree)
  120. {
  121. System.out.print(tree);
  122. System.out.print("left" + tree.left + "");
  123. System.out.print("right" + tree.right + "");
  124. if( tree.left != null )
  125. {
  126. print(tree.left);
  127. }
  128.  
  129. if( tree.right != null )
  130. {
  131. print(tree.right);
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement