AVLTree.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Made up out of thin air, because all the resources for this are crap.
  3. */
  4. public class AVLTree<T> extends LinkedBinarySearchTree<T> {
  5. /**
  6. * Constructors
  7. */
  8. public AVLTree () {
  9. super ();
  10. }
  11. public AVLTree (T key, T element) {
  12. super (key, element);
  13. }
  14. /**
  15. * insert
  16. * Calls balance()
  17. */
  18. public void insert (T key, T element) {
  19. Node<T> temp = new Node<T> (key, element);
  20. this.addElement (temp);
  21. /*
  22. * Add balance factors
  23. */
  24. Node<T> current = temp;
  25. while (current != root) {
  26. if (current.isRightChild()) {
  27. if ((current.getParent().getHeight() == 0)) {
  28. current.getParent().setRightHeight(1);
  29. } else {// This is an internal node
  30. current.getParent().setRightHeight(current.getHeight() + 1);
  31. }
  32. } else if (current.isLeftChild()) {
  33. if ((current.getParent().getHeight() == 0)) {
  34. current.getParent().setLeftHeight(1);
  35. } else {// This is an internal node
  36. current.getParent().setLeftHeight(current.getHeight() + 1);
  37. }
  38. }
  39. current = current.getParent();
  40. }
  41. this.balance(temp);
  42. }
  43. /**
  44. * remove
  45. * Calls balance()
  46. */
  47. public void remove (Node<T> element) {
  48. Node<T> current = element.getParent();
  49. if (element.isLeaf()) {
  50. if (element == root)
  51. this.removeElement (element.getKey());
  52. else {
  53. if (element.isLeftChild()) {
  54. this.removeElement (element.getKey());
  55. current.setLeftHeight(0);
  56. } else {
  57. this.removeElement (element.getKey());
  58. current.setRightHeight(0);
  59. }
  60. }
  61. } else {
  62. current = element;
  63. Node<T> parent = element.getParent();
  64. /*
  65. * Get the correct replacement
  66. */
  67. if (current.hasRight()) {
  68. current = current.getRight();
  69. if (current.hasLeft()) {
  70. while (current.hasLeft())
  71. current = current.getLeft();
  72. if (current.hasRight())
  73. current.getParent().setLeftHeight(current.getRight().getHeight() + 1);
  74. else
  75. current.getParent().setLeftHeight(0);
  76. current.getParent().setLeft(current.getRight());
  77. }
  78. } else {
  79. current = current.getRight();
  80. if (current.hasRight()) {
  81. while (current.hasRight())
  82. current = current.getRight();
  83. if (current.hasLeft())
  84. current.getParent().setRightHeight(current.getLeft().getHeight() + 1);
  85. else
  86. current.getParent().setRightHeight(0);
  87. current.getParent().setRight(current.getLeft());
  88. }
  89. }
  90. /*
  91. * Set the replacement's numbers and relationships
  92. */
  93. if (element.getRight() != current)
  94. current.setRight(element.getRight());
  95. if (element.getLeft() != current)
  96. current.setLeft(element.getLeft());
  97. if (current.getRight() != null) {
  98. current.getRight().setParent(current);
  99. current.setRightHeight(current.getRight().getHeight() + 1);
  100. } else
  101. current.setRightHeight(0);
  102. if (current.getLeft() != null) {
  103. current.getLeft().setParent(current);
  104. current.setLeftHeight(current.getLeft().getHeight() + 1);
  105. } else
  106. current.setLeftHeight(0);
  107. if (element.isRightChild())
  108. parent.setRight(current);
  109. else if (element.isLeftChild())
  110. parent.setLeft(current);
  111. current.setParent(parent);
  112. /*
  113. * Delete the element
  114. */
  115. this.removeElement (element.getKey());
  116. }
  117. if (!isEmpty())
  118. this.balance(current);
  119. }
  120. /**
  121. * balance
  122. * Calls rotateRight() and rotateLeft()
  123. */
  124. private void balance (Node<T> current) {
  125. do {
  126. if (Math.abs(current.getBalance()) > 1) {
  127. if (current.getBalance() > 1) {
  128. // Right subtree unbalanced
  129. if (current.getRight().getBalance() == 1) {
  130. // Problem is in the right child's right subtree
  131. this.rotateLeft(current);
  132. } else {
  133. // Problem is in the right child's left subtree
  134. this.rotateRight(current.getRight());
  135. this.rotateLeft(current);
  136. }
  137. } else {
  138. // Left subtree unbalanced
  139. if (current.getLeft().getBalance() == 1) {
  140. // Problem is in the left child's right subtree
  141. this.rotateLeft(current.getLeft());
  142. this.rotateRight(current);
  143. } else {
  144. // Problem is in the left child's left subtree
  145. this.rotateRight(current);
  146. }
  147. }
  148. }
  149. current = current.getParent();
  150. } while (current != null);
  151. }
  152. /**
  153. * rotateRight
  154. */
  155. private void rotateRight(Node<T> temp) {
  156. Node<T> current = temp.getLeft();
  157. Node<T> parent = temp;
  158. /*
  159. * Problem is in the left child's left subtree
  160. */
  161. // Step 1: Change current's parent
  162. if (parent.getParent() == null) {
  163. current.setParent(null);
  164. current.unsetIsRight();
  165. current.unsetIsLeft();
  166. root = current;
  167. } else {
  168. if (parent.isRightChild())
  169. parent.getParent().setRight(current);
  170. else if (parent.isLeftChild())
  171. parent.getParent().setLeft(current);
  172. }
  173. // Step 2: Change parent's left
  174. parent.setLeft(current.getRight());
  175. if (current.getRight() != null)
  176. parent.getLeft().setParent(parent);
  177. // Step 3: Change current's right
  178. current.setRight(parent);
  179. parent.setParent(current);
  180. /*
  181. * Change balance factors
  182. */
  183. if (parent.getLeft() != null)
  184. parent.setLeftHeight(parent.getLeft().getHeight() + 1);
  185. else
  186. parent.setLeftHeight(0);
  187. current.setRightHeight(parent.getHeight() + 1);
  188. if (current.getParent() != null) {
  189. if (current.isRightChild())
  190. current.getParent().setRightHeight(current.getHeight() + 1);
  191. else if (current.isLeftChild())
  192. current.getParent().setLeftHeight(current.getHeight() + 1);
  193. }
  194. }
  195. /**
  196. * rotateLeft
  197. */
  198. private void rotateLeft(Node<T> temp) {
  199. Node<T> current = temp.getRight();
  200. Node<T> parent = temp;
  201. /*
  202. * Problem is in right child's right subtree
  203. */
  204. // Step 1: Change current's parent
  205. if (parent.getParent() == null) {
  206. current.setParent(null);
  207. current.unsetIsRight();
  208. current.unsetIsLeft();
  209. root = current;
  210. } else {
  211. if (parent.isRightChild())
  212. parent.getParent().setRight(current);
  213. else if (parent.isLeftChild())
  214. parent.getParent().setLeft(current);
  215. current.setParent(parent.getParent());
  216. }
  217. // Step 2: Change parent's right
  218. parent.setRight(current.getLeft());
  219. if (current.getLeft() != null)
  220. parent.getRight().setParent(parent);
  221. // Step 3: Change current's left
  222. current.setLeft(parent);
  223. parent.setParent(current);
  224. /*
  225. * Change balance factors
  226. */
  227. if (parent.getRight() != null)
  228. parent.setRightHeight(parent.getRight().getHeight() + 1);
  229. else
  230. parent.setRightHeight(0);
  231. current.setLeftHeight(parent.getHeight() + 1);
  232. if (current.getParent() != null) {
  233. if (current.isRightChild())
  234. current.getParent().setRightHeight(current.getHeight() + 1);
  235. else if (current.isLeftChild())
  236. current.getParent().setLeftHeight(current.getHeight() + 1);
  237. }
  238. }
  239. }