Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. package skipList;
  2.  
  3. /**
  4. *
  5. * @author Thiago Goncalves Monteiro Viturino - 21011127
  6. *
  7. */
  8.  
  9. public class SkipListImpl implements SkipList {
  10.  
  11. protected SkipNode root;
  12. protected int level;
  13. protected int maxLevel;
  14. protected double probability = 0.5;
  15. private static SkipNode NIL;
  16. private final int PROXIMO = 0;
  17.  
  18. public SkipListImpl(int maxLevel, boolean useMaxLevelAsInitialLevel) {
  19. if(useMaxLevelAsInitialLevel){
  20. this.level = maxLevel;
  21. }else{
  22. this.level = 1;
  23. }
  24. this.maxLevel = maxLevel;
  25. root = new SkipNode(Integer.MIN_VALUE, maxLevel, new Integer(Integer.MIN_VALUE));
  26. NIL = new SkipNode(Integer.MAX_VALUE, 1, new Integer(Integer.MAX_VALUE));
  27. connectRootToNil();
  28. }
  29.  
  30. /**
  31. * Faz a ligacao inicial entre os apontadores forward do ROOT e o no NIL
  32. * Caso teseja-se usando o level do ROOT igual ao maxLevel esse metodo deve
  33. * conectar todos os forward. Senao o ROOT eh inicializaco com
  34. * level=1 e o metodo deve conectar apenas o forward[0].
  35. */
  36. private void connectRootToNil(){
  37. if (this.level == this.maxLevel){
  38. for (int index = 0; index < this.maxLevel; index++){
  39. this.root.forward[index] = NIL;
  40. }
  41. }
  42. else{
  43. this.root.forward[PROXIMO] = NIL;
  44. }
  45. }
  46.  
  47. /**
  48. * Metodo que gera uma altura aleatoria para ser atribuida a um novo no no metodo
  49. * insert(int,Object)
  50. */
  51. private int randomLevel(){
  52. int randomLevel = 1;
  53. double random = Math.random();
  54. while(Math.random() <= probability && randomLevel < maxLevel){
  55. randomLevel = randomLevel + 1;
  56. }
  57. return randomLevel;
  58. }
  59.  
  60. @Override
  61. public void insert(int key, Object newValue) {
  62. SkipNode[] update = new SkipNode[this.maxLevel];
  63. SkipNode noAtual = this.root;
  64. for (int index = (this.level - 1); index >= 0; index--){
  65. while (noAtual.forward[index].key < key){
  66. noAtual = noAtual.forward[index];
  67. }
  68. update[index] = noAtual;
  69. }
  70. noAtual = noAtual.forward[PROXIMO];
  71. if (noAtual.key == key){
  72. noAtual.satteliteData = newValue;
  73. }
  74. else{
  75. int valor = randomLevel();
  76. if (valor > this.level && valor < this.maxLevel){
  77. for (int index = valor; index >= this.level; index--){
  78. update[index] = this.root;
  79. }
  80. this.level = valor;
  81. }
  82. SkipNode novoNo = new SkipNode(key, valor, newValue);
  83. for (int index = 0; index < valor; index++){
  84. novoNo.forward[index] = update[index].forward[index];
  85. update[index].forward[index] = novoNo;
  86. }
  87. }
  88. }
  89.  
  90. @Override
  91. public void insert(int key, Object newValue, int height) {
  92. SkipNode[] update = new SkipNode[this.maxLevel];
  93. SkipNode noAtual = this.root;
  94. for (int index = (this.level - 1); index >= 0; index--){
  95. while (noAtual.forward[index].key < key){
  96. noAtual = noAtual.forward[index];
  97. }
  98. update[index] = noAtual;
  99. }
  100. noAtual = noAtual.forward[PROXIMO];
  101. if (noAtual.key == key){
  102. noAtual.satteliteData = newValue;
  103. }
  104. else{
  105. if (height > this.level && height < this.maxLevel){
  106. for (int index = height; index >= this.level; index--){
  107. update[index] = this.root;
  108. }
  109. this.level = height;
  110. }
  111. SkipNode novoNo = new SkipNode(key, height, newValue);
  112. for (int index = 0; index < height; index++){
  113. novoNo.forward[index] = update[index].forward[index];
  114. update[index].forward[index] = novoNo;
  115. }
  116. }
  117. }
  118.  
  119. @Override
  120. public void remove(int key) throws RuntimeException{
  121. SkipNode[] update = new SkipNode[this.maxLevel];
  122. SkipNode noAtual = this.root;
  123. for (int index = level - 1; index >= 0; index--){
  124. while (noAtual.forward[index].key < key){
  125. noAtual = noAtual.forward[index];
  126. }
  127. update[index] = noAtual;
  128. }
  129. noAtual = noAtual.forward[PROXIMO];
  130. if (noAtual.key == key){
  131. for (int index = 0; index < this.level; index++){
  132. if (update[index].forward[index] != noAtual){
  133. break;
  134. }
  135. update[index].forward[index] = noAtual.forward[index];
  136.  
  137. }
  138. while (this.level > 1 && this.root.forward[this.level - 1] == NIL){
  139. this.level--;
  140. }
  141. }else{
  142. throw new RuntimeException("No inexistente");
  143. }
  144. }
  145.  
  146.  
  147. @Override
  148. public int getHeight() {
  149. return this.level;
  150. }
  151.  
  152. @Override
  153. public SkipNode search(int key) throws RuntimeException{
  154. SkipNode procurado = this.root;
  155. for (int index = level - 1; index >= 0; index--){
  156. while (procurado.forward[index].key < key){
  157. procurado = procurado.forward[index];
  158. }
  159. }
  160. procurado = procurado.forward[PROXIMO];
  161. if (procurado.key == key){
  162. return procurado;
  163. }
  164. throw new RuntimeException("No inexistente");
  165. }
  166.  
  167. @Override
  168. public String printSkipList() {
  169. return localPrintSkipList(this.root).substring(0, localPrintSkipList(this.root).length() - 1);
  170. }
  171.  
  172. private String localPrintSkipList(SkipNode no){
  173. String resp = "";
  174. if (no != null && no != NIL){
  175. resp += "<" + localKey(no) + "," + no.height + ">" + "(";
  176. for (int index = 0; index < no.forward.length; index++){
  177. if (no.forward[index] == null){
  178. if (resp.lastIndexOf(",") == resp.length() - 1){
  179. resp = resp.substring(0, resp.length() - 1);
  180. }
  181. resp += ") ";
  182. break;
  183. }
  184. else{
  185. if (index == 0 && no.forward[index].key == NIL.key && no != this.root){ //Se o no.forward[0] é NIL e ele nao é ROOT, todos seus apontadores estao para NIL. Logo teremos NIL aparecendo height vezes entre os parenteses.
  186. for (int index2 = 0; index2 < no.forward.length; index2++){
  187. if (index2 == no.forward.length - 1){
  188. resp += localKey(NIL) + ") ";
  189. break;
  190. }
  191. else{
  192. resp += localKey(NIL) + ",";
  193. }
  194. }
  195. break;
  196. }
  197. else if (index == no.forward.length - 1){
  198. resp += localKey(no.forward[index]) + ") ";
  199. }
  200. else{
  201. resp += localKey(no.forward[index]) + ",";
  202. }
  203. }
  204. }
  205. resp += localPrintSkipList(no.forward[PROXIMO]);
  206. }
  207. return resp;
  208. }
  209.  
  210. private String localKey(SkipNode no){
  211. if (no.key == this.root.key){
  212. return "ROOT";
  213. }
  214. else if (no.key == NIL.key){
  215. return "NIL";
  216. }
  217. return "" + no.key;
  218. }
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement