University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.Date;
  5. import java.io.File;
  6. class Main {
  7. public static void main(String[] args) throws Exception {
  8. BinTree<String> tree = new BinTree<>();
  9. // Insert dictionary into tree
  10. Scanner dictFile = new Scanner(new File("dictionary.txt"));
  11. while (dictFile.hasNextLine())
  12. tree.insert(dictFile.nextLine());
  13. // Remove and add busybody
  14. tree.remove("busybody");
  15. tree.insert("busybody");
  16. // User interface
  17. Scanner in = new Scanner(System.in);
  18. System.out.print("> ");
  19. while (in.hasNextLine()) {
  20. String str = in.nextLine().toLowerCase();
  21. // Quit on q
  22. if (str.equals("q")) {
  23. TreeStats.printStats(tree);
  24. return;
  25. }
  26. // Word exists
  27. if (tree.search(str) != null) {
  28. System.out.println("Word found.");
  29. // Word doesn't exist
  30. } else {
  31. long start = System.nanoTime();
  32. List<BinTree<String>.Node> similar = tree.findSimilar(str);
  33. // No matches found
  34. if (similar.size() == 0) {
  35. System.out.println("Neither the word nor similar words was found.");
  36. // Matches found, list them
  37. } else {
  38. System.out.println("Word not found. Similar words:");
  39. for (BinTree<String>.Node n: similar) {
  40. System.out.println("\t"+n.elem);
  41. }
  42. }
  43. long end = System.nanoTime();
  44. System.out.println("");
  45. System.out.println(similar.size()+" lookups gave a positive answer.");
  46. System.out.printf("Time spent looking for similar words: %.2f ms\n",
  47. ((end - start) / 1000000f));
  48. }
  49. // Pretty prompt
  50. System.out.print("> ");
  51. }
  52. }
  53. }