Advertisement
forgelineage

Untitled

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