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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. class BinTree<T extends Comparable> {
  4. public Node first = new Node();
  5. public void insert(T elem) {
  6. first.insert(elem);
  7. }
  8. public void remove(T elem) {
  9. first = remove(elem, first);
  10. }
  11. public Node remove(T elem, Node n) {
  12. if (n == null) return null;
  13. if (elem.compareTo(n.elem) < 0) {
  14. n.left = remove(elem, n.left);
  15. if (n.left != null) n.left.parent = n;
  16. } else if (elem.compareTo(n.elem) < 0) {
  17. n.right = remove(elem, n.right);
  18. if (n.right != null) n.right.parent = n;
  19. } else {
  20. if (n.left == null) {
  21. Node p = n.parent;
  22. n = n.right;
  23. if (n != null) n.parent = p;
  24. } else if (n.right == null) {
  25. Node p = n.parent;
  26. n = n.left;
  27. if (n != null) n.parent = p;
  28. } else {
  29. n.elem = n.right.findSmallest();
  30. n.right = remove(n.elem, n.right);
  31. n.right.parent = n;
  32. }
  33. }
  34. return n;
  35. }
  36. public Node search(T elem) {
  37. return first.search(elem);
  38. }
  39. public T findSmallest() {
  40. return first.findSmallest();
  41. }
  42. public T findBiggest() {
  43. return first.findBiggest();
  44. }
  45. public List<Node> findSimilar(T str) throws Exception {
  46. if (!(str instanceof String))
  47. throw new Exception("findSimilar only works on string trees");
  48. List<Node> similar = new ArrayList<>();
  49. for (String s: SpellUtils.similarWords((String)str)) {
  50. Node n = this.search((T)s);
  51. if (n != null)
  52. similar.add(n);
  53. }
  54. // Deduplicate
  55. List<Node> exists = new ArrayList<>();
  56. List<Node> res = new ArrayList<>();
  57. for (Node n: similar) {
  58. if (!exists.contains(n)) {
  59. exists.add(n);
  60. res.add(n);
  61. }
  62. }
  63. return res;
  64. }
  65. class Node {
  66. public Node left = null;
  67. public Node right = null;
  68. public Node parent;
  69. public T elem;
  70. public Node(Node parent) {
  71. this.parent = parent;
  72. }
  73. public Node() {
  74. this.parent = null;
  75. }
  76. public void insert(T elem) {
  77. if (this.elem == null) {
  78. this.elem = elem;
  79. } else if (this.elem.compareTo(elem) > 0) {
  80. if (this.left == null) {
  81. this.left = new Node(this);
  82. }
  83. this.left.insert(elem);
  84. } else {
  85. if (this.right == null) {
  86. this.right = new Node(this);
  87. }
  88. this.right.insert(elem);
  89. }
  90. }
  91. public T findSmallest() {
  92. if (this.left != null)
  93. return this.left.findSmallest();
  94. else
  95. return this.elem;
  96. }
  97. public T findBiggest() {
  98. if (this.right != null)
  99. return this.right.findBiggest();
  100. else
  101. return this.elem;
  102. }
  103. public Node search(T elem) {
  104. int cmp = this.elem.compareTo(elem);
  105. if (cmp == 0)
  106. return this;
  107. else if (cmp > 0 && this.left != null)
  108. return this.left.search(elem);
  109. else if (this.right != null)
  110. return this.right.search(elem);
  111. else
  112. return null;
  113. }
  114. public int depth() {
  115. int d = 0;
  116. Node n = this.parent;
  117. while (n != null) {
  118. d += 1;
  119. n = n.parent;
  120. }
  121. return d;
  122. }
  123. }
  124. }