Advertisement
FNSY

Untitled

Mar 23rd, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. package eg.edu.alexu.csd.filestructure.avl;
  2.  
  3. public class AVLTree<T extends Comparable<T>> implements IAVLTree<T> {
  4. private Node<T> root;
  5.  
  6. public AVLTree() {
  7. root = new Node<T>(null, null);
  8. }
  9.  
  10. @Override
  11. public INode<T> getTree() {
  12. return root;
  13. }
  14.  
  15. @Override
  16. public void insert(T key) {
  17. Node<T> cursor = null; // y
  18. Node<T> current = root; // x
  19. Node<T> newNode = new Node<T>(key, null); // z
  20. while (current != null && current.getValue() != null) {
  21. cursor = current;
  22. try {
  23. if (newNode.getValue().compareTo(current.getValue()) < 0) {
  24. current = (Node<T>) current.getLeftChild();
  25. } else if (newNode.getValue().compareTo(current.getValue()) > 0) {
  26. current = (Node<T>) current.getRightChild();
  27. }
  28. } catch (NullPointerException ex) {
  29. break;
  30. }
  31. }
  32.  
  33. newNode.setParent(cursor);
  34. if (cursor == null) {
  35. root = newNode;
  36. } else if (newNode.getValue().compareTo(cursor.getValue()) < 0) {
  37. cursor.setLeftChild(newNode);
  38. newNode.setParent(cursor);
  39. reBalance((Node<T>) cursor.getLeftChild());
  40. } else if (newNode.getValue().compareTo(cursor.getValue()) > 0) {
  41. cursor.setRightChild(newNode);
  42. newNode.setParent(cursor);
  43. reBalance((Node<T>) cursor.getRightChild());
  44. }
  45.  
  46. }
  47.  
  48. @Override
  49. public boolean delete(T key) {
  50. if (!search(key))
  51. return false;
  52.  
  53. Node<T> toBeDeleted = searchReturnsNode(key);
  54. if (!toBeDeleted.hasLeftChild()) {
  55. this.transplant(toBeDeleted, (Node<T>) toBeDeleted.getRightChild());
  56. } else if (!toBeDeleted.hasRightChild()) {
  57. this.transplant(toBeDeleted, (Node<T>) toBeDeleted.getLeftChild());
  58. } else {
  59. Node<T> node = minimum((Node<T>) toBeDeleted.getRightChild());
  60. this.transplant(toBeDeleted, node);
  61. }
  62.  
  63. return true;
  64. }
  65.  
  66. @Override
  67. public boolean search(T key) {
  68. return this.search(key, root);
  69. }
  70.  
  71. public Node<T> getNodes() {
  72. return this.root;
  73. }
  74.  
  75. public void inOrder(Node<T> root) {
  76.  
  77. if (root != null) {
  78. inOrder((Node<T>) root.getLeftChild());
  79. System.out.println(root.getValue());
  80. inOrder((Node<T>) root.getRightChild());
  81. }
  82. }
  83.  
  84. @Override
  85. public int height() {
  86. if (root.getValue() == null)
  87. return -1;
  88. return Math.max(root.getLeftHeight(), root.getLeftHeight());
  89. }
  90.  
  91. private void reBalance(Node<T> t) {
  92. Node<T> parent = null; // up
  93. Node<T> check = t;
  94. Node<T> child = t;
  95. while (check.hasParent()) {
  96. child = check;
  97. check = (Node<T>) check.getParent();
  98. int balanceFactor = check.getBalanceFactor();
  99. if (balanceFactor == 2) {
  100. if (check.getParent() != null)
  101. parent = (Node<T>) check.getParent();
  102. if (child.getBalanceFactor() == 1) {
  103. leftLeft(check, child, parent);
  104. break;
  105. } else if (child.getBalanceFactor() == -1) {
  106. leftRight(check, child, parent);
  107. break;
  108. }
  109. } else if (balanceFactor == -2) {
  110.  
  111. if (check.getParent() != null)
  112. parent = (Node<T>) check.getParent();
  113. if (child.getBalanceFactor() == 1) {
  114. rightLeft(check, child, parent);
  115. break;
  116. } else if (child.getBalanceFactor() == -1) {
  117. rightRight(check, child, parent);
  118. break;
  119.  
  120. }
  121. }
  122. }
  123. }
  124.  
  125. private void rightRight(Node<T> check, Node<T> child, Node<T> parent) {
  126. boolean right = false, noParent = false;
  127. check.setRightChild(null);
  128. child.setParent(null);
  129. if (parent == null)
  130. noParent = true;
  131. else if (parent.getValue().compareTo(check.getValue()) < 0)
  132. right = true;
  133.  
  134. if (!noParent) {
  135. if (right)
  136. parent.setRightChild(child);
  137. else
  138. parent.setLeftChild(child);
  139. child.setParent(parent);
  140. }
  141. if (child.hasLeftChild()) {
  142. ((Node<T>) child.getLeftChild()).setParent(check);
  143. check.setRightChild((Node<T>) child.getLeftChild());
  144. }
  145. child.setLeftChild(check);
  146. check.setParent(child);
  147. if (check == root)
  148. root = child;
  149. }
  150.  
  151. private void rightLeft(Node<T> check, Node<T> child, Node<T> parent) {
  152. Node<T> n = (Node<T>) child.getLeftChild();
  153. boolean right = false, noParent = false;
  154. check.setRightChild(null);
  155. child.setLeftChild(null);
  156. if (parent == null)
  157. noParent = true;
  158. else if (parent.getValue().compareTo(check.getValue()) < 0)
  159. right = true;
  160.  
  161. if (!noParent) {
  162. if (right)
  163. parent.setRightChild(n);
  164. else
  165. parent.setLeftChild(n);
  166. n.setParent(parent);
  167. }
  168.  
  169. if (n.hasRightChild()) {
  170. ((Node<T>) n.getRightChild()).setParent(child);
  171. child.setLeftChild((Node<T>) n.getRightChild());
  172. }
  173. n.setRightChild(child);
  174. child.setParent(n);
  175. if (n.hasLeftChild()) {
  176. ((Node<T>) n.getLeftChild()).setParent(check);
  177. check.setRightChild((Node<T>) n.getLeftChild());
  178. }
  179. n.setLeftChild(check);
  180. check.setParent(n);
  181. if (check == root)
  182. root = n;
  183.  
  184. }
  185.  
  186. private void leftRight(Node<T> check, Node<T> child, Node<T> parent) {
  187. Node<T> n = (Node<T>) child.getRightChild();
  188. boolean right = false, noParent = false;
  189. check.setLeftChild(null);
  190. child.setRightChild(null);
  191. if (parent == null)
  192. noParent = true;
  193. else if (parent.getValue().compareTo(check.getValue()) < 0)
  194. right = true;
  195.  
  196. if (!noParent) {
  197. if (right)
  198. parent.setRightChild(n);
  199. else
  200. parent.setLeftChild(n);
  201. n.setParent(parent);
  202. }
  203.  
  204. if (n.hasRightChild()) {
  205. ((Node<T>) n.getRightChild()).setParent(check);
  206. check.setLeftChild((Node<T>) n.getRightChild());
  207. }
  208. n.setRightChild(check);
  209. check.setParent(n);
  210. if (n.hasLeftChild()) {
  211. ((Node<T>) n.getLeftChild()).setParent(child);
  212. child.setRightChild((Node<T>) n.getLeftChild());
  213. }
  214. n.setLeftChild(child);
  215. child.setParent(n);
  216. if (check == root)
  217. root = n;
  218.  
  219. }
  220.  
  221. private void leftLeft(Node<T> check, Node<T> child, Node<T> parent) {
  222. boolean right = false, noParent = false;
  223. check.setLeftChild(null);
  224. child.setParent(null);
  225. if (parent == null)
  226. noParent = true;
  227. else if (parent.getValue().compareTo(check.getValue()) < 0)
  228. right = true;
  229.  
  230. if (!noParent) {
  231. if (right)
  232. parent.setRightChild(child);
  233. else
  234. parent.setLeftChild(child);
  235. child.setParent(parent);
  236. }
  237. if (child.hasRightChild()) {
  238. ((Node<T>) child.getRightChild()).setParent(check);
  239. check.setLeftChild((Node<T>) child.getRightChild());
  240.  
  241. }
  242. child.setRightChild(check);
  243. check.setParent(child);
  244. if (check == root)
  245. root = child;
  246. }
  247.  
  248. private Node<T> minimum(Node<T> node) {
  249. if (!node.hasLeftChild())
  250. return node;
  251. return minimum((Node<T>) node.getLeftChild());
  252.  
  253. }
  254.  
  255. private void transplant(Node<T> toBeRemoved, Node<T> toBePlanted) {
  256. Node<T> toBeRemovedParent = (Node<T>) toBeRemoved.getParent();
  257. if (!toBeRemoved.hasParent()) {
  258. root = toBePlanted;
  259. } else if (toBeRemoved.equals(toBeRemovedParent.getLeftChild())) {
  260. toBeRemovedParent.setLeftChild(toBePlanted);
  261. } else {
  262. toBeRemovedParent.setRightChild(toBePlanted);
  263. }
  264. toBeRemoved.setParent(null);
  265. if (toBePlanted != null) {
  266. Node<T> toBePlantedParent = (Node<T>) toBePlanted.getParent();
  267.  
  268. if (toBePlantedParent.hasLeftChild() && toBePlantedParent.getLeftChild().equals(toBePlanted))
  269. toBePlantedParent.setLeftChild(null);
  270. else if (toBePlantedParent.hasRightChild())
  271. toBePlantedParent.setRightChild(null);
  272. toBePlanted.setParent(toBeRemovedParent);
  273. if (toBeRemoved.hasLeftChild()) {
  274. Node<T> leftOfToBeRemoved = ((Node<T>) toBeRemoved.getLeftChild());
  275. toBePlanted.setLeftChild(leftOfToBeRemoved);
  276. leftOfToBeRemoved.setParent(toBePlanted);
  277. }
  278. if (toBeRemoved.hasRightChild()) {
  279. Node<T> rightOfToBeRemoved = ((Node<T>) toBeRemoved.getRightChild());
  280. toBePlanted.setRightChild(rightOfToBeRemoved);
  281. rightOfToBeRemoved.setParent(toBePlanted);
  282. }
  283. }
  284.  
  285. }
  286.  
  287. private Node<T> searchReturnsNode(T key, Node<T> node) {
  288. if (key.compareTo(node.getValue()) == 0)
  289. return node;
  290. if (key.compareTo(node.getValue()) < 0)
  291. return searchReturnsNode(key, (Node<T>) node.getLeftChild());
  292. else
  293. return searchReturnsNode(key, (Node<T>) node.getRightChild());
  294.  
  295. }
  296.  
  297. private Node<T> searchReturnsNode(T key) {
  298. return this.searchReturnsNode(key, root);
  299. }
  300.  
  301. private boolean search(T key, Node<T> node) {
  302. if (node != null) {
  303. if (key.compareTo(node.getValue()) == 0)
  304. return true;
  305. if (key.compareTo(node.getValue()) < 0)
  306. return search(key, (Node<T>) node.getLeftChild());
  307. else
  308. return search(key, (Node<T>) node.getRightChild());
  309. }
  310. return false;
  311.  
  312. }
  313.  
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement