Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. import javafx.util.Pair;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.Scanner;
  7.  
  8. public class Tree implements Runnable {
  9. private Node root;
  10. private Node max;
  11.  
  12. public static void main(String[] args) {
  13. new Thread(null, new Tree(), "", 64 * 1024 * 1024).start();
  14. }
  15.  
  16. public void run() {
  17. try {
  18. Tree t = new Tree();
  19. t.create("src/data/input.txt");
  20. t.count();
  21. t.print("src/data/output.txt");
  22. } catch(IOException e) {
  23. System.out.println("error found");
  24. }
  25. }
  26.  
  27. private Pair<Node, Node> findNode(long value) {
  28. Node current = root;
  29. Node parent = null;
  30. while (true) {
  31. if (value > current.value && current.right != null) {
  32. parent = current;
  33. current = current.right;
  34. } else if (value < current.value && current.left != null) {
  35. parent = current;
  36. current = current.left;
  37. } else if (value == current.value) {
  38. return new Pair<>(current, parent);
  39. } else {
  40. return null;
  41. }
  42. }
  43. }
  44.  
  45. private Pair<Node, Node> findInsertion(Node current, Node parent) {
  46. while (current.left != null) {
  47. parent = current;
  48. current = current.left;
  49. }
  50.  
  51. return new Pair<>(current, parent);
  52. }
  53.  
  54. public void delete(String path) throws IOException {
  55. Scanner s = new Scanner(new File(path));
  56. delete(s.nextLong());
  57. s.close();
  58. }
  59.  
  60. private void delete(long value) {
  61. Pair<Node, Node> found = findNode(value);
  62. if (found != null) {
  63. Node foundParent = found.getValue();
  64. Node foundCurrent = found.getKey();
  65.  
  66. if (foundCurrent.left != null && foundCurrent.right != null) {
  67. Pair<Node, Node> insertion = findInsertion(foundCurrent.right, foundCurrent);
  68. Node insertionParent = insertion.getValue();
  69. Node insertionCurrent = insertion.getKey();
  70.  
  71. if (insertionParent == foundCurrent) {
  72. if (foundCurrent == root) {
  73. root = insertionCurrent;
  74. if (foundCurrent.left != null) {
  75. root.left = foundCurrent.left;
  76. }
  77. } else {
  78. if (foundParent.left == foundCurrent) {
  79. foundParent.left = insertionCurrent;
  80. } else {
  81. foundParent.right = insertionCurrent;
  82. }
  83. insertionCurrent.left = foundCurrent.left;
  84. }
  85. } else {
  86. if (foundCurrent == root) {
  87. if (insertionCurrent.right != null) {
  88. if (insertionParent.right == insertionCurrent) {
  89. insertionParent.right = insertionCurrent.right;
  90. } else {
  91. insertionParent.left = insertionCurrent.right;
  92. }
  93. } else {
  94. if (insertionParent.right == insertionCurrent) {
  95. insertionParent.right = null;
  96. } else {
  97. insertionParent.left = null;
  98. }
  99. }
  100. root = insertionCurrent;
  101. insertionCurrent.left = foundCurrent.left;
  102. insertionCurrent.right = foundCurrent.right;
  103. } else {
  104. if (insertionCurrent.right != null) {
  105. if (insertionParent.left == insertionCurrent) {
  106. insertionParent.left = insertionCurrent.right;
  107. } else {
  108. insertionParent.right = insertionCurrent.right;
  109. }
  110. } else {
  111. if (insertionParent.left == insertionCurrent) {
  112. insertionParent.left = null;
  113. } else {
  114. insertionParent.right = null;
  115. }
  116. }
  117.  
  118. if (foundParent.right == foundCurrent) {
  119. foundParent.right = insertionCurrent;
  120. } else {
  121. foundParent.left = insertionCurrent;
  122. }
  123.  
  124. insertionCurrent.left = foundCurrent.left;
  125. insertionCurrent.right = foundCurrent.right;
  126. }
  127.  
  128. }
  129. } else if (foundCurrent.left == null && foundCurrent.right == null) {
  130. if (foundCurrent != root) {
  131. if (foundParent.left == foundCurrent) {
  132. foundParent.left = null;
  133. } else {
  134. foundParent.right = null;
  135. }
  136. } else {
  137. root = null;
  138. }
  139. } else {
  140. if (foundCurrent != root) {
  141. if (foundParent.left == foundCurrent) {
  142. if (foundCurrent.left != null) {
  143. foundParent.left = foundCurrent.left;
  144. } else {
  145. foundParent.left = foundCurrent.right;
  146. }
  147. } else {
  148. if (foundCurrent.left != null) {
  149. foundParent.right = foundCurrent.left;
  150. } else {
  151. foundParent.right = foundCurrent.right;
  152. }
  153. }
  154. } else {
  155. if (foundCurrent.left != null) {
  156. root = foundCurrent.left;
  157. } else {
  158. root = foundCurrent.right;
  159. }
  160. }
  161. }
  162. }
  163. }
  164.  
  165. public void create(String path) throws IOException {
  166. File file = new File(path);
  167. Scanner s = new Scanner(file);
  168. Scanner check = new Scanner(file);
  169.  
  170. check.nextLine();
  171. if (check.nextLine().isEmpty()) {
  172. s.nextLine();
  173. s.nextLine();
  174. }
  175. check.close();
  176.  
  177. if (s.hasNext() && root == null) {
  178. root = new Node(s.nextLong());
  179. max = root;
  180. }
  181.  
  182. while (s.hasNext()) {
  183. long v = s.nextLong();
  184. Node insertPlace = findPlace(v);
  185. if (insertPlace != null) {
  186. insertPlace.addChild(v);
  187. }
  188. }
  189.  
  190. s.close();
  191. }
  192.  
  193. public void count() {
  194. count(root);
  195. // delete(max.value);
  196. }
  197.  
  198. private void count(Node current) {
  199. if (current.left != null) {
  200. count(current.left);
  201. }
  202. if (current.right != null) {
  203. count(current.right);
  204. }
  205.  
  206. current.count += 1;
  207. if (current.right != null) {
  208. current.count += current.right.count;
  209. }
  210. if (current.left != null) {
  211. current.count += current.left.count;
  212. }
  213.  
  214. findMax(current);
  215. }
  216.  
  217. private int findDifference(Node current) {
  218. if (current.left != null && current.right != null) {
  219. return Math.abs(current.left.count - current.right.count);
  220. } else if (current.left != null) {
  221. return current.left.count;
  222. } else if (current.right != null) {
  223. return current.right.count;
  224. }
  225. return 0;
  226. }
  227.  
  228. private void findMax(Node current) {
  229. int currentDifference = findDifference(current);
  230. int maxDifference = findDifference(max);
  231.  
  232. if (currentDifference > maxDifference) {
  233. max = current;
  234. } else if (currentDifference == maxDifference && current.value > max.value) {
  235. max = current;
  236. }
  237. }
  238.  
  239. private Node findPlace(long value) {
  240. Node current = root;
  241. while (true) {
  242. if (value > current.value && current.right != null) {
  243. current = current.right;
  244. } else if (value < current.value && current.left != null) {
  245. current = current.left;
  246. } else if (value == current.value) {
  247. return null;
  248. } else {
  249. break;
  250. }
  251. }
  252.  
  253. return current;
  254. }
  255.  
  256. public void print(String path) throws IOException {
  257. FileWriter w = new FileWriter(path);
  258. print(root, w);
  259. w.close();
  260. }
  261.  
  262. private void print(Node current, FileWriter w) throws IOException {
  263. w.write(current.toString());
  264. w.write("\n");
  265.  
  266. if (current.left != null) {
  267. print(current.left, w);
  268. }
  269. if (current.right != null) {
  270. print(current.right, w);
  271. }
  272. }
  273.  
  274. private class Node {
  275. private Node left;
  276. private Node right;
  277. private long value;
  278. private int count;
  279.  
  280. public Node(long value) {
  281. this.value = value;
  282. }
  283.  
  284. public void addChild(long value) {
  285. if (value > this.value) {
  286. right = new Node(value);
  287. } else if (value < this.value) {
  288. left = new Node(value);
  289. }
  290. }
  291.  
  292. @Override
  293. public String toString() {
  294. return Long.toString(value) + ": " + count;
  295. }
  296. }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement