Advertisement
Manioc

skipinho

Jul 12th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. package adt.skipList;
  2.  
  3. public class SkipListImpl<T> implements SkipList<T> {
  4.  
  5.     protected SkipListNode<T> root;
  6.     protected SkipListNode<T> NIL;
  7.  
  8.     protected int maxHeight;
  9.  
  10.     protected double PROBABILITY = 0.5;
  11.  
  12.     public SkipListImpl(int maxHeight) {
  13.         this.maxHeight = maxHeight;
  14.         root = new SkipListNode(Integer.MIN_VALUE, maxHeight, null);
  15.         NIL = new SkipListNode(Integer.MAX_VALUE, maxHeight, null);
  16.         connectRootToNil();
  17.     }
  18.  
  19.     /**
  20.      * Faz a ligacao inicial entre os apontadores forward do ROOT e o NIL Caso
  21.      * esteja-se usando o level do ROOT igual ao maxLevel esse metodo deve
  22.      * conectar todos os forward. Senao o ROOT eh inicializado com level=1 e o
  23.      * metodo deve conectar apenas o forward[0].
  24.      */
  25.     private void connectRootToNil() {
  26.         for (int i = 0; i < maxHeight; i++) {
  27.             root.forward[i] = NIL;
  28.         }
  29.     }
  30.  
  31.    
  32.     @Override
  33.     public void insert(int key, T newValue, int height) {
  34.         SkipListNode<T> current = this.root;
  35.        
  36.         SkipListNode<T> update[] = new SkipListNode[this.maxHeight + 1];
  37.        
  38.         for(int h = this.maxHeight-1; h >= 0; h--) {
  39.             while(current.forward[h] != this.NIL && current.getForward(h).getKey() < key ) {
  40.                 current = current.getForward(h);
  41.             }
  42.             update[h] = current;
  43.         }
  44.        
  45.         current = current.getForward(0);
  46.         if(current.getKey() != key) {
  47.             SkipListNode<T> node = new SkipListNode<T>(key, height, newValue);
  48.             for(int i = 0; i < height; i++) {
  49.                 node.forward[i] = update[i].forward[i];
  50.                 update[i].forward[i] = node;
  51.             }
  52.         }else if(current.getKey() == key){
  53.             System.out.println("opa");
  54.             current = new SkipListNode(key, height, newValue);
  55.             for(int i = 0; i < height; i++) {
  56.                 current.forward[i] = update[i].forward[i];
  57.                 update[i].forward[i] = current;
  58.             }
  59.         }
  60.     }
  61.  
  62.     @Override
  63.     public void remove(int key) {
  64.        
  65.     }
  66.  
  67.     @Override
  68.     public int height() {
  69.         int ans = 0;
  70.         SkipListNode<T> current = this.root;
  71.         while(current != NIL) {
  72.             ans = Math.max(ans, current.height());
  73.             current = current.getForward(0);
  74.         }
  75.         return ans;
  76.     }
  77.  
  78.     @Override
  79.     public SkipListNode<T> search(int key) {
  80.         SkipListNode<T> current = this.root;
  81.        
  82.         for(int h = this.maxHeight-1; h >= 0; h--) {
  83.             while(current.forward[h] != this.NIL && current.getForward(h).getKey() < key ) {
  84.                 current = current.getForward(h);
  85.             }
  86.         }
  87.         current = current.getForward(0);
  88.         if(current.getKey() > key || current == NIL) return null;
  89.         return current;
  90.     }
  91.  
  92.     @Override
  93.     public int size() {
  94.         int sz = 0;
  95.         SkipListNode<T> current = this.root;
  96.         while(current != NIL) {
  97.             sz++;
  98.             //System.out.println(current.getValue());
  99.             current = current.getForward(0);
  100.         }
  101.         --sz;
  102.         return sz;
  103.     }
  104.  
  105.     @Override
  106.     public SkipListNode<T>[] toArray() {
  107.         SkipListNode<T> arr[] = new SkipListNode[this.size()+2];
  108.         int i = 0;
  109.         SkipListNode<T> current = this.root;
  110.         while(current != NIL) {
  111.             arr[i++] = current;
  112.             current = current.getForward(0);
  113.         }
  114.         arr[i] = current;
  115.         return arr;
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement