Advertisement
Guest User

Untitled

a guest
May 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Arrays;
  5. import java.util.Random;
  6. import java.util.Comparator;
  7. import java.util.Collections;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.Stream;
  10.  
  11. public class Search {
  12. public static void main(String[] args) {
  13. // Read in file contents (title+description)
  14. List<String> desc;
  15. try {
  16. desc = Util.readFile(args[0]);
  17. } catch (IOException e) {
  18. System.err.println("Couldn't read file! " + e.getMessage());
  19. return;
  20. }
  21.  
  22. // Creates a new search tree with a comparator compairing Films
  23. AVLTree<Film> tree = new AVLTree<Film>(new FilmComperator());
  24.  
  25. // Insert the films into the tree and count how many were successfully inserted
  26. // (i.e. weed out duplicate keys)
  27. int size = 0;
  28. for (String d : desc) {
  29. String title = d.split("\t")[0];
  30. String description = d.split("\t")[1];
  31. Film f = new Film(title, description);
  32. boolean inserted = tree.insert(f);
  33. if (inserted)
  34. size++;
  35. }
  36. // Compare logarithm and depth of search tree
  37. System.out.println("log2(" + size + ") = " + Math.log(size) / Math.log(2));
  38. System.out.println("depth = " + tree.depth());
  39.  
  40. // Read in file contents (titles to search)
  41. List<String> needles;
  42. try {
  43. needles = Util.readFile(args[1]);
  44. } catch (IOException e) {
  45. System.err.println("Couldn't read file! " + e.getMessage());
  46. return;
  47. }
  48.  
  49. // Search the given titles in the tree
  50. List<String> output = new ArrayList<String>();
  51. for (String needle : needles) {
  52. Film f = tree.search(new Film(needle, ""));
  53. String out = f == null ? " --- NOT FOUND ---" : f.toString();
  54. output.add(out);
  55. }
  56.  
  57. // Write output file
  58. try {
  59. Util.writeFile(args[2], output.stream().collect(Collectors.joining("\n")));
  60. } catch (IOException e) {
  61. System.err.println("Couldn't write file! " + e.getMessage());
  62. return;
  63. }
  64. }
  65.  
  66. private static class Util {
  67. @SuppressWarnings("unchecked")
  68. static List<String> readFile(String file) throws IOException {
  69. BufferedReader reader = new BufferedReader(new FileReader(file));
  70. List<?> contents = reader.lines().collect(Collectors.toList());
  71. reader.close();
  72. return (List<String>) contents;
  73. }
  74.  
  75. static void writeFile(String file, String contents) throws IOException {
  76. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  77. writer.write(contents);
  78. writer.close();
  79. }
  80. }
  81. }
  82.  
  83. class FilmComperator implements Comparator<Film> {
  84. @Override
  85. public int compare(Film s1, Film s2) {
  86. return s1.getTitle().compareTo(s2.getTitle());
  87. }
  88. }
  89.  
  90. class Film {
  91. private String description;
  92. private String title;
  93.  
  94. public Film(String title, String desc) {
  95. this.title = title;
  96. this.description = desc;
  97. }
  98.  
  99. public String getTitle() {
  100. return this.title;
  101. }
  102.  
  103. public String getDescription() {
  104. return this.description;
  105. }
  106.  
  107. @Override
  108. public String toString() {
  109. return this.title + "\t" + this.description;
  110. }
  111. }
  112.  
  113. // A search tree
  114. class AVLTree<T> {
  115. private Comparator<T> comparator;
  116. public Node<T> root; // Is null if the tree is empty
  117.  
  118. public AVLTree(Comparator<T> comp) {
  119. this.comparator = comp;
  120. this.root = null;
  121. }
  122.  
  123. // Returns true if the value was successfully inserted
  124. public boolean insert(T data) {
  125. if (this.root == null) {
  126. this.root = new Node<T>(data, null, this.comparator, this);
  127. return true;
  128. }
  129. return this.root.insert(data);
  130. }
  131.  
  132. // Returns the value belonging to the given key, or null if not found
  133. public T search(T data) {
  134. if (this.root == null) {
  135. return null;
  136. }
  137. return this.root.search(data);
  138. }
  139.  
  140. // Computes the depth of the search tree
  141. public int depth() {
  142. if (this.root == null) {
  143. return -1;
  144. }
  145. return this.root.depth();
  146. }
  147. }
  148.  
  149. // A node of the search tree
  150. class Node<T> {
  151. private Comparator<T> comp;
  152. private Node<T> parent; // Can be null
  153. private Node<T> left; // Can be null
  154. private Node<T> right; // Can be null
  155. private AVLTree<T> tree;
  156. private T data;
  157.  
  158. public Node(T data, Node<T> parent, Comparator<T> comparator, AVLTree<T> tree) {
  159. this.comp = comparator;
  160. this.parent = parent;
  161. this.left = null;
  162. this.right = null;
  163. this.data = data;
  164. this.tree = tree;
  165. }
  166.  
  167. // Returns true if the value was successfully inserted
  168. public boolean insert(T data) {
  169. int c = this.comp.compare(data, this.data);
  170. if (c < 0) {
  171. if (this.left == null) {
  172. this.left = new Node<T>(data, this, this.comp, tree);
  173. return true;
  174. } else {
  175. boolean tmp = this.left.insert(data);
  176. this.correctBalance();
  177. return tmp;
  178. }
  179. } else if (c > 0) {
  180. if (this.right == null) {
  181. this.right = new Node<T>(data, this, this.comp, tree);
  182. return true;
  183. } else {
  184. boolean tmp = this.right.insert(data);
  185. this.correctBalance();
  186. return tmp;
  187. }
  188. }
  189. return false;
  190. }
  191.  
  192. // Returns the value belonging to the given key, or null if not found
  193. public T search(T data) {
  194. int c = this.comp.compare(data, this.data);
  195. if (c < 0) {
  196. if (this.left == null) {
  197. return null;
  198. }
  199. return this.left.search(data);
  200. }
  201. if (c > 0) {
  202. if (this.right == null) {
  203. return null;
  204. }
  205. return this.right.search(data);
  206. }
  207. return this.data;
  208. }
  209.  
  210. public int depth() {
  211. boolean l0 = this.left == null;
  212. boolean r0 = this.right == null;
  213. if (r0 && l0) {
  214. return 0;
  215. }
  216. if (r0) {
  217. return 1 + this.left.depth();
  218. }
  219. if (l0) {
  220. return 1 + this.right.depth();
  221. }
  222. int d_left = this.left.depth();
  223. int d_right = this.right.depth();
  224. return 1 + Math.max(d_left, d_right);
  225. }
  226.  
  227. private void correctBalance() {
  228. if (this.getBalance() > 1) {
  229. leftRotation();
  230. } else if (this.getBalance() < -1) {
  231. rightRotation();
  232. }
  233. }
  234.  
  235. private int getBalance() {
  236. if (this.left != null && this.right != null) {
  237. return this.right.depth() - this.left.depth();
  238. } else if (this.left != null && this.right == null) {
  239. return 0 - this.left.depth();
  240. } else if (this.left == null && this.right != null) {
  241. return this.right.depth();
  242. } else {
  243. return 0;
  244. }
  245. }
  246.  
  247. private void rightRotation() {
  248. boolean isLeft = true;
  249. Node<T> pp = null;
  250. if(this.parent != null) {
  251. isLeft = this.parent.left == this;
  252. pp = this.parent;
  253. }
  254. else {
  255. tree.root = this.left;
  256. }
  257.  
  258. Node<T> t2 = this.left.right;
  259. Node<T> r = this.left;
  260.  
  261. r.right = null;
  262. this.left = t2;
  263. r.parent = pp;
  264. if(t2 != null) t2.parent = this;
  265. r.right = this;
  266. this.parent = r;
  267. if(pp != null) {
  268. if(isLeft) pp.left = r;
  269. else pp.right = r;
  270. }
  271. }
  272.  
  273. private void leftRotation() {
  274. boolean isLeft = true;
  275. Node<T> pp =null;
  276. if (this.parent != null) {
  277. isLeft = this.parent.left == this;
  278. pp = this.parent;
  279. } else {
  280. tree.root = this.right;
  281. }
  282. Node<T> t2 = this.right.left;
  283. Node<T> r = this.right;
  284.  
  285. r.left = null;
  286. this.right = t2;
  287. r.parent = pp;
  288. if (t2 != null) t2.parent = this;
  289. r.left = this;
  290. this.parent = r;
  291. if (pp != null) {
  292. if (isLeft)
  293. pp.left = r;
  294. else
  295. pp.right = r;
  296. }
  297. }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement