Guest User

Untitled

a guest
Dec 11th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. public class SkipList {
  2.  
  3. public static final double PROB = 0.5;
  4. public static final int MAX_LEVEL = 5;
  5. public static final int MIN_KEY = 0; //only keys between 0-1000 are valid for prac
  6. public static final int MAX_KEY = 1000;
  7.  
  8. //private int listLevel; //leaving out for now
  9. private SkipNode headerNode, nilNode;
  10.  
  11. public SkipList() {
  12. //current list level
  13. //listLevel = 0;
  14.  
  15. //create header/tail nodes
  16. headerNode = new SkipNode(MAX_LEVEL, MIN_KEY-1, 0);
  17. nilNode = new SkipNode(MAX_LEVEL, MAX_KEY+1, 0);
  18.  
  19. //connect header to tail
  20. for (int i=0; i<MAX_LEVEL; i++) {
  21. headerNode.forward[i] = nilNode;
  22. }
  23. }
  24.  
  25. public SkipNode search(int searchKey) {
  26. SkipNode currentNode = headerNode;
  27. int currentLevel = currentNode.getLevel();
  28.  
  29. if(searchKey > MAX_KEY) {
  30. System.out.println("Key cannot be greater than "+MAX_KEY);
  31. return null;
  32. }
  33.  
  34. while(currentLevel != 0) { //while current != bottom of stack
  35. currentLevel--; //current = current.down
  36. while(searchKey >= currentNode.forward[currentLevel].getKey()) { //while(key >= current.right.key)
  37. currentNode = currentNode.forward[currentLevel]; //current = current.right
  38. System.out.println("checking key "+currentNode.getKey());
  39. }
  40. }
  41. //return (currentNode.key == searchKey); //returns True if the key is found
  42. if(currentNode.getKey() == searchKey)
  43. return currentNode;
  44. else
  45. return null;
  46. }
  47.  
  48. public void delete(int searchKey) {
  49. if(searchKey > MAX_KEY)
  50. System.out.println("Key cannot be greater than "+MAX_KEY);
  51. else {
  52. SkipNode currentNode = headerNode;
  53. int currentLevel = currentNode.getLevel();
  54. SkipNode record[] = new SkipNode[MAX_LEVEL];
  55. //record[currentLevel] = currentNode;
  56.  
  57. while(currentLevel != 0) { //searches for node
  58. currentLevel--;
  59. //record[currentLevel] = currentNode;
  60. while(searchKey >= currentNode.forward[currentLevel].getKey()) {
  61. currentNode = currentNode.forward[currentLevel]; //change node
  62. //record[currentLevel] = currentNode; //update record
  63. //System.out.println("checking key "+currentNode.getKey());
  64. }
  65. record[currentLevel] = currentNode;
  66. }
  67. if(currentNode.getKey() == searchKey) { //updates pointers
  68. for(int i=0; i<record.length; i++) {
  69. System.out.println(record[i].toString());
  70. }
  71. for(int i=0; i<MAX_LEVEL; i++) {
  72. //System.out.println("for i= "+i); //test
  73. //System.out.println(currentNode.toString());
  74. //System.out.println("Comparing "+record[i].forward[i].getKey()+" to "+currentNode.getKey());
  75. if(record[i].forward[i].getKey() == currentNode.getKey()) { //doesnt pass !!!! record[i].forward[i] == currentNode
  76. //System.out.println("record["+i+"].forward["+i+"] == currentNode"); //test
  77. //System.out.println();
  78. record[i].forward[i] = currentNode.forward[i];
  79. }
  80. }
  81. currentNode = null; //removes node after severing links
  82. } else {
  83. System.out.println("Key does not exist.");
  84. }
  85. }
  86. }
  87.  
  88. public void insert(int maxLevel, int newKey, int newValue) {
  89. if(newKey > MAX_KEY)
  90. System.out.println("Key cannot be greater than "+MAX_KEY);
  91. else {
  92. SkipNode currentNode = headerNode;
  93. int currentLevel = currentNode.getLevel();
  94. SkipNode record[] = new SkipNode[MAX_LEVEL];
  95. //record[currentLevel] = currentNode;
  96.  
  97. while(currentLevel != 0) { //searches for node
  98. currentLevel--;
  99. record[currentLevel] = currentNode;
  100. while(newKey >= currentNode.forward[currentLevel].getKey()) {
  101. currentNode = currentNode.forward[currentLevel]; //change node
  102. record[currentLevel] = currentNode; //update record
  103. }
  104. }
  105. if(currentNode.getKey() == newKey) {
  106. System.out.println("Key already exists, updating value.");
  107. currentNode.setValue(newValue); //if node with same key exists, update value
  108. } else {
  109. SkipNode newNode = new SkipNode(maxLevel, newKey, newValue); //creates new node, links bottom level
  110. newNode.forward[0] = currentNode.forward[0];
  111. currentNode.forward[0] = newNode;
  112.  
  113. int levelCount = 1;
  114. while(Math.random() <= PROB && levelCount < MAX_LEVEL) { //randomly decides level and updates pointers
  115. newNode.forward[levelCount] = record[levelCount].forward[levelCount]; //updates forward
  116. record[levelCount].forward[levelCount] = newNode; //updates back
  117. levelCount++;
  118. }
  119. }
  120. }
  121. }
  122.  
  123. public void printList() {
  124. SkipNode currentNode = this.headerNode;
  125. while(currentNode.getKey() != MAX_KEY+1) {
  126. System.out.println(currentNode.toString()); //prints header and all inserted nodes
  127. currentNode = currentNode.forward[0];
  128. }
  129. System.out.println(currentNode.toString()); //prints tail/nil node
  130. }
  131.  
  132. ////////////////////
  133.  
  134. public SkipNode getHeaderNode() {
  135. return headerNode;
  136. }
  137.  
  138. public SkipNode getNilNode() {
  139. return nilNode;
  140. }
  141. }
Add Comment
Please, Sign In to add comment