Advertisement
Guest User

Untitled

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