Guest User

Untitled

a guest
Jan 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. package adt.skipList;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class SkipListImpl<V> implements SkipList<V> {
  6.  
  7. protected SkipNode<V> root;
  8. protected int level;
  9. protected int maxLevel;
  10.  
  11. //SET THIS VALUE TO TRUE IF YOU ARE IMPLEMENTING MAX_LEVEL = LEVEL
  12. //SET THIS VALUE TO FALSE IF YOU ARE IMPLEMENTING MAX_LEVEL != LEVEL
  13. protected boolean useMaxLevelAsLevel;
  14. protected double probability = 0.5;
  15. protected SkipNode<V> NIL;
  16.  
  17. public SkipListImpl(int maxLevel) {
  18. if(useMaxLevelAsLevel){
  19. this.level = maxLevel;
  20. }else{
  21. this.level = 1;
  22. }
  23. this.maxLevel = maxLevel;
  24. root = new SkipNode(Integer.MIN_VALUE, maxLevel, new Integer(Integer.MIN_VALUE));
  25. NIL = new SkipNode(Integer.MAX_VALUE, maxLevel, new Integer(Integer.MAX_VALUE));
  26. connectRootToNil();
  27. }
  28.  
  29. /**
  30. * Faz a ligacao inicial entre os apontadores forward do ROOT e o NIL
  31. * Caso esteja-se usando o level do ROOT igual ao maxLevel esse metodo deve
  32. * conectar todos os forward. Senao o ROOT eh inicializado com
  33. * level=1 e o metodo deve conectar apenas o forward[0].
  34. */
  35. private void connectRootToNil(){
  36. for(int i = 0; i < level; i++){
  37. root.forward[i] = NIL;
  38. }
  39. }
  40.  
  41. /**
  42. * Metodo que gera uma altura aleatoria para ser atribuida a um novo no no metodo
  43. * insert(int,V)
  44. */
  45. private int randomLevel(){
  46. int randomLevel = 1;
  47. double random = Math.random();
  48. while(Math.random() <= probability && randomLevel < maxLevel){
  49. randomLevel = randomLevel + 1;
  50. }
  51. return randomLevel;
  52. }
  53.  
  54. @Override
  55. public void insert(int key, V newValue) {
  56.  
  57. }
  58.  
  59. @Override
  60. public void insert(int key, V newValue, int height) {
  61. // TODO Auto-generated method stub
  62.  
  63. }
  64.  
  65. @Override
  66. public void remove(int key) {
  67. // TODO Auto-generated method stub
  68.  
  69. }
  70.  
  71. @Override
  72. public int height() {
  73. return level;
  74. }
  75.  
  76. @Override
  77. public SkipNode<V> search(int key) {
  78. SkipNode<V> Auxiliar = root;
  79. for(int i = level - 1; i >= 0; i --){
  80. while(Auxiliar.forward[i].key <= key){
  81. Auxiliar = Auxiliar.forward[i];
  82. if(Auxiliar.key == key){
  83. return Auxiliar;
  84. }
  85. }
  86. }
  87. return null;
  88. }
  89.  
  90. @Override
  91. public int size(){
  92. SkipNode<V> Auxiliar = root;
  93. int Tamanho = 0;
  94. while(!(Auxiliar.forward == null)){
  95. Auxiliar = Auxiliar.forward[0];
  96. Tamanho ++;
  97. }
  98. return Tamanho;
  99. }
  100.  
  101. @Override
  102. public SkipNode<V>[] toArray() {
  103. SkipNode<V> Auxiliar = root;
  104. ArrayList<V> arrayList = new ArrayList<V>();
  105. while(Auxiliar.forward[0] != NIL){
  106. arrayList.add(Auxiliar.satteliteData);
  107. Auxiliar = Auxiliar.forward[0];
  108. }
  109. return (SkipNode<V>[]) arrayList.toArray();
  110. }
  111.  
  112. public static void main(String args[]){
  113. SkipListImpl s = new SkipListImpl(5);
  114. }
  115.  
  116. }
Add Comment
Please, Sign In to add comment