Advertisement
forgelineage

Untitled

Oct 29th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. import java.nio.file.InvalidPathException;
  2. import java.util.*;
  3.  
  4. import cs_1c.Traverser;
  5.  
  6. public class FHlazySearchTree<E extends Comparable< ? super E > >
  7. implements Cloneable
  8. {
  9. protected int mSize;
  10. protected int mSizeHard;
  11. protected FHlazySTNode<E> mRoot;
  12.  
  13. public FHlazySearchTree() { clear(); }
  14. public boolean empty() { return (mSize == 0); }
  15. public int size() { return mSize; }
  16. public int sizeHard() {return mSizeHard;}
  17. public void clear() { mSize = 0; mRoot = null; mSizeHard = 0;}
  18. public int showHeight() { return findHeight(mRoot, -1); }
  19. public void collectGarbage() { collectGarbage(mRoot); }
  20.  
  21. public E findMin()
  22. {
  23. if (mRoot == null)
  24. throw new NoSuchElementException();
  25. return findMin(mRoot).data;
  26. }
  27.  
  28. public E findMax()
  29. {
  30. if (mRoot == null)
  31. throw new NoSuchElementException();
  32. return findMax(mRoot).data;
  33. }
  34.  
  35. public E find( E x )
  36. {
  37. FHlazySTNode<E> resultNode;
  38. resultNode = find(mRoot, x);
  39. if (resultNode == null)
  40. throw new NoSuchElementException();
  41. return resultNode.data;
  42. }
  43.  
  44. public boolean contains(E x) { return find(mRoot, x) != null; }
  45.  
  46. public boolean insert( E x )
  47. {
  48. int oldSize = mSize;
  49. mRoot = insert(mRoot, x);
  50. return (mSize != oldSize);
  51. }
  52.  
  53. public boolean remove( E x )
  54. {
  55. int oldSize = mSize;
  56. mRoot = remove(mRoot, x);
  57. return (mSize != oldSize);
  58. }
  59.  
  60. public < F extends Traverser<? super E > >
  61. void traverse(F func)
  62. {
  63. traverse(func, mRoot);
  64. }
  65.  
  66. public Object clone() throws CloneNotSupportedException //Updated for Deleted?
  67. {
  68. FHlazySearchTree<E> newObject = (FHlazySearchTree<E>)super.clone();
  69. newObject.clear(); // can't point to other's data
  70.  
  71. newObject.mRoot = cloneSubtree(mRoot);
  72. newObject.mSize = mSize;
  73. newObject.mSizeHard = mSizeHard;
  74.  
  75. return newObject;
  76. }
  77.  
  78. // private helper methods ----------------------------------------
  79. protected FHlazySTNode<E> findMin( FHlazySTNode<E> root ) //Updated for deleted
  80. {
  81. if (root == null)
  82. return null;
  83.  
  84. //Checks left nodes first
  85. FHlazySTNode<E> temp = findMin(root.lftChild);
  86.  
  87. if (temp != null)
  88. return temp;
  89.  
  90. if (!root.deleted)
  91. return root;
  92.  
  93. //If left nodes are deleted, will check right nodes.
  94. return findMin(root.rtChild);
  95. }
  96.  
  97. protected FHlazySTNode<E> findMax( FHlazySTNode<E> root ) //Updated for deleted
  98. {
  99. if (root == null)
  100. return null;
  101.  
  102. //Checks Right nodes first
  103. FHlazySTNode<E> temp = findMax(root.rtChild);
  104.  
  105. if (temp != null)
  106. return temp;
  107.  
  108. if (!root.deleted)
  109. return root;
  110.  
  111. //If Right nodes are deleted, will check left nodes.
  112. return findMax(root.lftChild);
  113. }
  114.  
  115. protected FHlazySTNode<E> insert( FHlazySTNode<E> root, E x ) //updated for deleted
  116. {
  117. int compareResult; // avoid multiple calls to compareTo()
  118.  
  119. if (root == null)
  120. {
  121. mSizeHard++;
  122. mSize++;
  123. return new FHlazySTNode<E>(x, null, null, false);
  124. }
  125.  
  126. compareResult = x.compareTo(root.data);
  127. if ( compareResult < 0 )
  128. root.lftChild = insert(root.lftChild, x);
  129. else if ( compareResult > 0 )
  130. root.rtChild = insert(root.rtChild, x);
  131.  
  132. if (root.deleted == true)
  133. {
  134. root.deleted = false;
  135. mSize++;
  136. }
  137. return root;
  138. }
  139.  
  140.  
  141. protected FHlazySTNode<E> remove( FHlazySTNode<E> root, E x ) //Updated for deleted
  142. {
  143. int compareResult; // avoid multiple calls to compareTo()
  144.  
  145. if (root == null)
  146. return null;
  147.  
  148. compareResult = x.compareTo(root.data);
  149. if ( compareResult < 0 )
  150. root.lftChild = remove(root.lftChild, x);
  151. else if ( compareResult > 0 )
  152. root.rtChild = remove(root.rtChild, x);
  153.  
  154. // found the node
  155. if (root.deleted == false && compareResult == 0)
  156. {
  157. root.deleted = true;
  158. mSize--;
  159. }
  160.  
  161. return root;
  162. }
  163.  
  164. protected void collectGarbage(FHlazySTNode<E> root)
  165. {
  166. if (root == null)
  167. return;
  168.  
  169. collectGarbage(root.lftChild);
  170. collectGarbage(root.rtChild);
  171.  
  172. if (root.deleted == true)
  173. root = removeHard(root, root.data);
  174. }
  175.  
  176. private FHlazySTNode<E> removeHard( FHlazySTNode<E> root, E x)
  177. {
  178. int compareResult; // avoid multiple calls to compareTo()
  179.  
  180. if (root == null)
  181. return null;
  182.  
  183. FHlazySTNode<E> temp = root;
  184.  
  185. compareResult = x.compareTo(root.data);
  186. if ( compareResult < 0 )
  187. root.lftChild = removeHard(root.lftChild, x);
  188. else if ( compareResult > 0 )
  189. root.rtChild = removeHard(root.rtChild, x);
  190.  
  191. else if (root.lftChild != null && root.rtChild != null)
  192. {
  193. System.out.println("Root Prior: " + root.data);
  194. root.data = findMin(root.rtChild).data;
  195. root.deleted = findMin(root.rtChild).deleted;
  196. root.rtChild = removeHard(root.rtChild, root.data);
  197. }
  198. else
  199. {
  200. System.out.println("Root Prior: " + root.data);
  201. root =
  202. (root.lftChild != null)? root.lftChild : root.rtChild;
  203. mSizeHard--;
  204. }
  205.  
  206.  
  207. try {
  208. System.out.println("Root After: " + root.data);
  209. } catch (NullPointerException e)
  210. {
  211. System.out.println("Root After: " + root);
  212. }
  213.  
  214. return root;
  215. }
  216.  
  217. protected <F extends Traverser<? super E>> //No Update Needed
  218. void traverse(F func, FHlazySTNode<E> treeNode)
  219. {
  220. if (treeNode == null)
  221. return;
  222.  
  223. traverse(func, treeNode.lftChild);
  224. func.visit(treeNode.data);
  225. traverse(func, treeNode.rtChild);
  226. }
  227.  
  228. protected FHlazySTNode<E> find( FHlazySTNode<E> root, E x ) //Updated for deleted
  229. {
  230. int compareResult; // avoid multiple calls to compareTo()
  231.  
  232. if (root == null)
  233. return null;
  234.  
  235. compareResult = x.compareTo(root.data);
  236. if (compareResult < 0)
  237. return find(root.lftChild, x);
  238. if (compareResult > 0)
  239. return find(root.rtChild, x);
  240.  
  241. if (root.deleted == true)
  242. return null; // soft deleted
  243.  
  244. return root; // found
  245. }
  246.  
  247. protected FHlazySTNode<E> cloneSubtree(FHlazySTNode<E> root) //Updated for deleted?
  248. {
  249. FHlazySTNode<E> newNode;
  250. if (root == null)
  251. return null;
  252.  
  253. // does not set myRoot which must be done by caller
  254. newNode = new FHlazySTNode<E>
  255. (
  256. root.data,
  257. cloneSubtree(root.lftChild),
  258. cloneSubtree(root.rtChild),
  259. root.deleted
  260. );
  261. return newNode;
  262. }
  263.  
  264. protected int findHeight( FHlazySTNode<E> treeNode, int height )
  265. {
  266. int leftHeight, rightHeight;
  267. if (treeNode == null)
  268. return height;
  269. height++;
  270. leftHeight = findHeight(treeNode.lftChild, height);
  271. rightHeight = findHeight(treeNode.rtChild, height);
  272. return (leftHeight > rightHeight)? leftHeight : rightHeight;
  273. }
  274. }
  275.  
  276.  
  277. class FHlazySTNode<E extends Comparable< ? super E > >
  278. {
  279. // use public access so the tree or other classes can access members
  280. public FHlazySTNode<E> lftChild, rtChild;
  281. public E data;
  282. public FHlazySTNode<E> myRoot; // needed to test for certain error
  283. public boolean deleted;
  284.  
  285. public FHlazySTNode( E d, FHlazySTNode<E> lft, FHlazySTNode<E> rt, boolean del )
  286. {
  287. lftChild = lft;
  288. rtChild = rt;
  289. data = d;
  290. deleted = del;
  291. }
  292.  
  293. public FHlazySTNode()
  294. {
  295. this(null, null, null, false);
  296. }
  297.  
  298. // function stubs -- for use only with AVL Trees when we extend
  299. public int getHeight() { return 0; }
  300. boolean setHeight(int height) { return true; }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement