Advertisement
forgelineage

Untitled

Nov 4th, 2014
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. import cs_1c.FHs_treeNode;
  2. import cs_1c.FHsearch_tree;
  3.  
  4. public class FHsplayTree<E extends Comparable< ? super E > >
  5. extends FHsearch_tree<E>
  6. {
  7. public boolean insert(E x)
  8. {
  9. int compareResult;
  10. FHs_treeNode tempNode;
  11.  
  12. if (mRoot == null)
  13. {
  14. mSize++;
  15. mRoot = new FHs_treeNode(x, null, null);
  16. return true;
  17. }
  18.  
  19. tempNode = splay(mRoot, x);
  20.  
  21. compareResult = x.compareTo(mRoot.data);
  22.  
  23. if ( compareResult < 0 )
  24. {
  25. FHs_treeNode newNode = new FHs_treeNode(x, tempNode.lftChild, mRoot);
  26. mRoot = newNode;
  27. mSize++;
  28. return true;
  29. }
  30. else if ( compareResult > 0 )
  31. {
  32. FHs_treeNode newNode = new FHs_treeNode(x, mRoot, tempNode.rtChild);
  33. mRoot = newNode;
  34. mSize++;
  35. return true;
  36. }
  37. else
  38. return false; // duplicate
  39. }
  40.  
  41. public boolean remove( E x )
  42. {
  43. FHs_treeNode newRoot;
  44.  
  45. if (mRoot == null)
  46. {
  47. return false;
  48. }
  49.  
  50. newRoot = splay(mRoot, x);
  51.  
  52. if (mRoot != x)
  53. return false;
  54. if (mRoot.lftChild == null)
  55. newRoot = mRoot.rtChild;
  56.  
  57. else
  58. {
  59. newRoot = mRoot.lftChild;
  60. newRoot = splay(newRoot, x);
  61. newRoot.rtChild = mRoot.rtChild;
  62. }
  63.  
  64. mRoot = newRoot;
  65. return true;
  66. }
  67.  
  68. public E showRoot()
  69. {
  70. if (mRoot == null)
  71. return null;
  72. return mRoot.data;
  73. }
  74.  
  75. FHs_treeNode<E> splay( FHs_treeNode<E> root, E x )
  76. {
  77. FHs_treeNode rtTree = null;
  78. FHs_treeNode lftTree = null;
  79. FHs_treeNode rtTreeMin = null;
  80. FHs_treeNode lftTreeMax = null;
  81. int compareResult;
  82.  
  83. while (root != null)
  84. {
  85. compareResult = x.compareTo(root.data);
  86.  
  87. if (compareResult < 0)
  88. {
  89. if (root.lftChild == null)
  90. break;
  91.  
  92. //Zig Zig left
  93. if (x.compareTo(root.lftChild.data) < 0)
  94. {
  95. root = rotateWithLeftChild(root);
  96. if (root.lftChild == null)
  97. break;
  98. }
  99.  
  100. rtTreeMin.lftChild = root;
  101. rtTreeMin = root;
  102. root = root.lftChild;
  103. }
  104.  
  105. else if (compareResult > 0)
  106. {
  107. if (root.rtChild == null)
  108. break;
  109.  
  110. //Zig Zig right
  111. if (x.compareTo(root.rtChild.data) < 0)
  112. {
  113. root = rotateWithRightChild(root);
  114. if (root.lftChild == null)
  115. break;
  116. }
  117.  
  118. lftTreeMax.rtChild = root;
  119. lftTreeMax = root;
  120. root = root.rtChild;
  121. }
  122. else
  123. break;
  124. }
  125.  
  126. if (lftTree != null)
  127. {
  128. lftTreeMax.lftChild = root.lftChild;
  129. root.lftChild = lftTree;
  130. }
  131.  
  132. if (rtTree != null)
  133. {
  134. rtTreeMin.rtChild = root.rtChild;
  135. root.rtChild = rtTree;
  136. }
  137. return root;
  138. }
  139.  
  140. FHs_treeNode<E> rotateWithLeftChild( FHs_treeNode<E> k2 )
  141. {
  142. FHs_treeNode<E> k1 = k2.lftChild;
  143. k2.lftChild = k1.rtChild;
  144. k1.rtChild = k2;
  145. return k1;
  146. }
  147.  
  148. FHs_treeNode<E> rotateWithRightChild( FHs_treeNode<E> k2 )
  149. {
  150. FHs_treeNode<E> k1 = k2.rtChild;
  151. k2.rtChild = k1.lftChild;
  152. k1.lftChild = k2;
  153. return k1;
  154. }
  155.  
  156. protected FHs_treeNode<E> find( FHs_treeNode<E> root, E x )
  157. {
  158. if (mRoot == null)
  159. return null;
  160. FHs_treeNode retRoot = splay (mRoot, x);
  161. if (retRoot.data == x)
  162. return retRoot;
  163. return null;
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement