Home > Back-end >  On JAVA SSet
On JAVA SSet

Time:11-14

Rankedsset contains two methods:
? I get (I) : return ranked the values in the set
? Rank (x) : the return on the set x ranking
Have SlowSkiplistRankedSSet and FastSkiplistRankedSSet in the folder, the function of the two programs are exactly the same, the method get (I) and rank (x) in the worst case for the time complexity of the Ω (n),
Now get in FastSkiplistRankedSSet (I) and rank (x) the time complexity of the method reduced to O (logn), (can't change the time complexity of other methods)

 public class FastSkiplistRankedSSet Implements RankedSSet {
Protected Comparator c;

@ SuppressWarnings (" unchecked ")
Protected the static class Node {
T x;
Node [] next;
Public Node (T ix, int h) {
X=ix.
Next=(Node []) Array. NewInstance (Node class, h + 1);
}
Public int height () {
Return next. Length - 1;
}
}

/* *
* This node Sits on the left side of the skiplist
*/
Protected Node Sentinel.

/* *
* The maximum height of any element
*/
Int h;

/* *
* The number of elements stored in The skiplist
*/
int n;

/* *
* A source of random Numbers
*/
The Random rand.

/* *
* 2 by the add method (x)
*/
Protected Node [] the stack;

@ SuppressWarnings (" unchecked ")
Public FastSkiplistRankedSSet (Comparator C) {
This. C=c;
N=0;
Sentinel=new Node (null, 32);
Stack=(Node []) Array. NewInstance (Node. Class, sentinel. Next. Length);
H=0;
Rand=new Random ();
}

Public FastSkiplistRankedSSet () {
This (new DefaultComparator ());
}

/* *
* Find the node U that precedes the value x in the skiplist.
*
* @ param x - the value to search for
* @ return a node U that maximizes u.x subject to
* the constraint that u.x & lt; X - or sentinel if u.x & gt;=x for
* all node S x
*/
Protected Node FindPredNode (T, x) {
Node U=sentinel;
Int r=h;
While (r & gt;=0) {
While (u.n ext [r].=null & amp; & C.com pare said (u.n ext [r]. X, x) & lt; 0)
U=u.n ext [r].//go right in the list r
R -;//go down into the list r - 1
}
The return of u;
}

Public T find (T, x) {
Node U=findPredNode (x);
Return u.n ext [0]==null? Null: u.n ext [0]. X;
}

Public T findGE (T, x) {
If (x==null) {//return first node
Return sentinel. Next [0]==null? Null: sentinel. Next [0]. X.
}
Return the find (x);
}

Public T findLT (T, x) {
If (x==null) {//return last node
Node U=sentinel;
Int r=h;
While (r & gt;=0) {
While (u.n ext [r].=null)
U=u.n ext [r].
R -;
}
Return u.x;
}
Return findPredNode (x) x;
}

Public T get (int I) {
//This is too missile and making it faster - will take changes to This
//structure
Iterator It=this. The iterator ();
For (int j=0; J & lt; i; J++) {
It. The next ();
}
Return it. The next ();
}

Public int rank (T, x) {
//This is too missile and making it faster - will take changes to This
//structure
Iterator It=this. The iterator ();
Int r=0;
While (it. HasNext () & amp; & C.com pare said (it. Next (), x) & lt; 0 {
R++;
}
return r;
}

Public Boolean remove x (T) {
Boolean removed=false;
Node U=sentinel;
Int r=h;
Int comp=0;
While (r & gt;=0) {
While (u.n ext [r].=null
& & (comp=c.com pare said (u.n ext [r]. X, x)) & lt; 0 {
U=u.n ext [r].
}
If (u.n ext [r].=null & amp; & Comp==0) {
Removed=true;
U.n ext [r]=u.n ext [r]. Next [r].
If (u==sentinel & amp; & U.n ext [r]==null)
H -;//height has gone down
}
R -;
}
If (removed) n -;
Return removed;
}


/* *
* Simulate repeatedly tossing a coin until it comes up tails.
* Note that this code will never generate a height greater than 32
* @ return the number of coin tosses - 1
*/
Protected int pickHeight () {
Int z=rand. NextInt ();
Int k=0;
Int m=1;
While ((z & amp; M)!=0) {
k++;
M & lt; <=1;
}
The return of k;
}

Public void the clear () {
N=0;
H=0;
The Arrays. The fill (sentinel. Next, null);
}

Public int size () {
Return n.
}

Public Comparator Comparator () {
return c;
}

/* *
* Create a new iterator in which the value in the next iteration is u.n ext x
* TODO: Constant time removal requires the use of a skiplist finger (a stack)
* @ param u
* @ return
*/
Protected Iterator The iterator (Node U) {
The class SkiplistIterator implements Iterator {
Node U, prev;
Public SkiplistIterator (Node U) {
This. U=u;
Prev=null;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related