Advertisement
FNSY

Untitled

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