The ———— of a daily province a simple jump table (skip list) Java implementation __java

Source: Internet
Author: User
The first time you know that the data structure of the jump table is probably a year ago (this sentence may be despised by countless compatriots), but they do not know how to achieve. The most impressive thing at the time was this article jump table (skip list)-Implementation (Java), because the skip list in this article is the most easy to understand the implementation, I was also through this article on the jump table has a further understanding, so, really want to thank the owner of this article here. A year later, I found that my awareness of the jump is blurred, so the first thought is this article. Read this again today and implement the deletion method that is not given in this article. And the generics are added, but generics are only generic to value, and the key still takes the string type in the original. So it's still simpler and more restrictive. The reason for posting is to improve my understanding of skip list and as a memo. The original text of the link as previously mentioned, the original author actually I do not know who, just want to say thank you silently. Of course, packages the author thinks I have any offense or tort, I will immediately delete the post. on the definition and introduction of the Jump table, the reader can refer to the original. Here directly to the original code, here the original code and the original text is the only difference is that, I have given the original text did not give the deletion method (the original text is actually referring to an English article, the English article gives the deletion method pseudocode, I until later found that, but my own implementation and pseudo code in the same way, The code is slightly superfluous, and here is the deletion method I implemented myself. May be very bad, so I implore you to criticize ... 1 Encapsulation Class Skiplistentry.java for each element (key value pair) in the Hop table
public class Skiplistentry<v> {public String key;

    public V value; public int pos; Mainly in order to print the list with public skiplistentry<v> up, down, left, right; Up or down four pointers public static String Neginf = new String ("-oo"); Negative Infinity public static String Posinf = new String ("+oo");
        Positive Infinity public skiplistentry (String K, v V) {key = k;
        Value = V;
    up = down = left = right = NULL;
    Public v. GetValue () {return value;
    Public String Getkey () {return key;
        Public V SetValue (v Val) {v oldValue = value;
        Value = val;
    return oldValue;
        @SuppressWarnings ("Unchecked") public boolean equals (Object o) {skiplistentry<v> entry;
        try {entry = (skiplistentry<v>) o;//Detect type} catch (ClassCastException ex)
        {return false; Return (Entry.getkey () = = key) &AMP;&Amp
    (Entry.getvalue (). Equals (value));
    Public String toString () {return "(" + Key + "," + Value + "); }
}
2 Skip list specific implementation (including add, delete, change, check)
Import Java.util.Random; /** * A simple implementation of the jump table. Key can only be a string type, value may be any object type * @param <V> * @author xxx February 14, 2017 pm 9:42:06 * @version v1.0/public class SKIPL

    ist<v> {public skiplistentry<v> head;//top of the first element public skiplistentry<v> tail;//The last element of the top level public int size; The number of elements in the jump table is public int height; The height of the jump table is public Random flag; 
    Throwing Coins/** * default constructor * @author xxx February 14, 2017 pm 9:32:22 * @since v1.0 * * Public skiplist ()
        {head = new skiplistentry<v> (skiplistentry.neginf, NULL);

        tail = new Skiplistentry<v> (skiplistentry.posinf, NULL);
        Head.right = tail;

        Tail.left = head;
        size = 0;
        Height = 0;
    Flag = new Random ();  /** * Returns the number of elements * @return * @author xxx February 14, 2017 pm 9:35:22 * @since v1.0/public int
    Size () {return size; /** * Determines whether the number of elements in the hop table is zero * @return * @author xxx February 14, 2017 afternoon 9:35:52 * @since v1.0 * * Public boolean IsEmpty () {return (size = = 0); /** * Starts with the first element at the top, also the head element, until the No. 0 level is found, the key in front of the position to be inserted is @param k * @return * @author XXX 201 7 February 14 pm 9:42:12 * @since v1.0 * * Private skiplistentry<v> Findentry (String k) {Skiplis

        Tentry<v> p = head; while (true) {/* * always look to the right, example: k=34. --->--->---> 40 ^ |  P will stop at 30/while (P.right.key!= skiplistentry.posinf && p.right.key.compareto (k) <=
            0) {p = p.right;
            //If there is another layer, go to the next level to find if (P.down!= null) {p = P.down; else {break;//Stop looking at the bottom level} return p;
       P.key <= k}/** returns the value of the key binding/public V get (String k) { Skiplistentry<v> p = findentry (k);
        if (K.equals (P.getkey ())) {return p.value;
        else {return null; }/** * Inserts a key-value pair into the Hop table, and if the key already exists, overwrites the corresponding value and returns the old value * @param k * @param v * @return * @author XX X February 14, 2017 pm 9:48:54 * @since v1.0 */public V put (String K, v V) {System.out.println ("-----plug
        The Jump table before ["+ K +"] is:-----");

        Printhorizontal ();

        Skiplistentry<v> p, q;

        p = findentry (k);
            if (K.equals (P.getkey ())) {V old = P.value;
            P.value = v;
        return old;
        } q = new skiplistentry<v> (k, V);
        Q.left = p;
        Q.right = P.right;
        P.right.left = q;

        P.right = q; int currentlevel = 0; 
        The current layer CurrentLevel = 0//The random value is less than 0.5, then the inserted key value pairs the corresponding key need to establish an association on the previous level, and it is possible to increase the height of the hop table while (flag.nextdouble () < 0.5) {//If the height is exceeded, you shouldTo rebuild a top-level if (currentlevel >= height) {skiplistentry<v> P1, p2;
                Height = height + 1;
                P1 = new Skiplistentry<v> (skiplistentry.neginf, NULL);

                P2 = new Skiplistentry<v> (skiplistentry.posinf, NULL);
                P1.right = p2;

                P1.down = head;
                P2.left = p1;

                P2.down = tail;
                Head.up = p1;

                Tail.up = p2;
                head = p1;
            tail = p2;
            while (p.up = = null) {p = p.left;

            } p = p.up;

            Skiplistentry<v> e;
             * Note that in this implementation, only the No. 0 level of the linked list holds keys corresponding to the value of the 1 ~ height layer of the Skiplistentry object * Only hold a reference to the key, the value is empty, in order to save space.
            * * e = new skiplistentry<v> (k, null);
            E.left = p;
            E.right = P.right;
            E.down = q; P.right. left = e;
            P.right = e;

            Q.up = e; Q = e; Q for the next layer of iterative CurrentLevel = CurrentLevel + 1;

        Current layer +1}//Insert a key value pair after total plus 1 size = size + 1;
        SYSTEM.OUT.PRINTLN ("-----Insert [" + K + "] after the jump table is:-----");
        Printhorizontal ();
    return null;
     /** * @param key * @return * @author xxx February 14, 2017 pm 10:08:17 * @since v1.0

        */public void Remove (String key) {skiplistentry<v> p = findentry (key);
        if (!p.getkey (). Equals (key)) {return;
        ///delete elements and then reconnect, and make the deleted objects free and easy to recycle p.left.right = p.right;
        P.right.left = P.left;
        P.right = null;
        P.left = null;
            From the bottom up, make all the keys equal to the key of the Skiplistentry object in the two-directional reference place null while (P.up!= null) {p = p.up;
            P.left.right = P.right;
            P.right.left = P.left;
            P.right = null;
        P.left = null; }

        From top to bottom, make null while (P.down!= null) {skiplistentry<v> T with references in two directions up and down in all keys equal to key Skiplistentry objects
            EMP = P.down;
            P.down = null;
            Temp.up = null;
        p = temp;
         * * * Delete elements, if the top of the linked list only head and tail two elements, then delete the top level.
         * Delete the top layer after the latest top-level if still only head and tail two elements, then also be deleted, and so on.
            */while (Head.right.key = = Tail.key && Height > 0) {skiplistentry<v> P1, p2;
            P1 = Head.down;

            P2 = Tail.down;
            Head.right = null;

            Head.down = null;
            Tail.left = null;

            Tail.down = null;
            P1.up = null;
            P2.up = null;
            head = p1;
            tail = p2;
        Height = height-1;

        ///successfully removed an element, size minus 1 = size-1;
        SYSTEM.OUT.PRINTLN ("-----Delete [" + key + "] after the jump table is:-----");

    Printhorizontal (); /** * Print out the diagram structure of the Hop table (horizontal direction) * @author XXX February 14, 2017 10:35: * @since v1.0/public void Printhorizontal () {String s = "";
        int i;

        Skiplistentry<v> p;

        p = head;
        while (P.down!= null) {p = P.down;
        } i = 0;
            while (P!= null) {p.pos = i++;
        p = p.right;
        } p = head;
            while (P!= null) {s = Getonerow (p);
            System.out.println (s);
        p = p.down;
        } private String Getonerow (Skiplistentry<v> p) {string s;

        int A, b, I;

        A = 0;
        s = "" + P.key;

        p = p.right;

            while (P!= null) {skiplistentry<v> q;
            Q = p;
            while (Q.down!= null) q = Q.down;

            b = Q.pos;

            s = s + "<-";

            for (i = a + 1; i < b; i++) s = s + "--------"; s = s + ">" + p.keY

            A = b;
        p = p.right;
    return s; /** * Print out the diagram structure of the Hop table (vertical direction) * @author XXX February 14, 2017 pm 10:35:36 * @since v1.0 * * public void PRI
        Ntvertical () {String s = "";
        Skiplistentry<v> p;
        p = head;

        while (P.down!= null) p = P.down;
            while (P!= null) {s = Getonecolumn (p);

            System.out.println (s);
        p = p.right;
        } private String Getonecolumn (Skiplistentry<v> p) {string s = "";
            while (P!= null) {s = s + "" + P.key;
        p = p.up;
    return (s);
 }

}
3 Test
public class Test {public static void main (string[] args) {skiplist<string> s = new skiplist<
        String> ();
        S.put ("ABC", "");
        S.put ("DEF", "");
        S.put ("KLM", "");
        S.put ("HIJ", "");
        S.put ("Ghj", "");

        S.put ("AAA", "");
        S.remove ("ABC");
        S.remove ("DEF");
        S.remove ("KLM");
        S.remove ("HIJ");
        S.remove ("Ghj");


        S.remove ("AAA");
        S.put ("ABC", "");
        S.put ("DEF", "");
        S.put ("KLM", "");
        S.put ("HIJ", "");
        S.put ("Ghj", "");

    S.put ("AAA", ""); (Note: The results are different each time because of the characteristics of the Hop table)-----The Jump table before inserting [ABC] is:------oo <-> +oo-----The jump table after inserting [ABC] is:------oo < -> ABC <-> +oo-oo <-> ABC <-> +oo-----The jump table before inserting [DEF] is:------oo <-> ABC <-> +oo-oo < -> ABC <-> +oo-----The jump table after inserting [DEF] is:------oo <---------> def <-> +oo-oo <-> ABC <-> def <-> +oo-oo <-> ABC <-> DEF <-> +oo-----The jump table before inserting [KLM] is:------oo <---------> DEF <-> +oo-oo <-> ABC &LT;-&G T def <-> +oo-oo <-> ABC <-> DEF <-> +oo-----The jump table after inserting [KLM] is:------oo <---------> DEF <-& Gt KLM <-> +oo-oo <-> ABC <-> DEF <-> KLM <-> +oo-oo <-> ABC <-> DEF <-> KL M <-> +oo-----The jump table before inserting [HIJ] is:------oo <---------> DEF <-> KLM <-> +oo-oo <-> ABC <-&gt ; DEF <-> KLM <-> +oo-oo <-> ABC <-> DEF <-> KLM <-> +oo-----The Jump table after insert [HIJ] is:------oo <---------> DEF <---------> KLM <-> +oo-oo <-> ABC <-> DEF <---------> KLM <-> +oo-oo <-> ABC <-> DEF <-> HIJ <-> KLM <-> +oo-----The jump table before inserting [GHJ] is:------oo <---------& Gt DEF <---------> KLM <-> +oo-oo <-> ABC <-> DEF <---------> KLM <-> +oo-oo <-> ABC &LT;-&GT DEF <-> HIJ <-> KLM <-> +oo-----The jump table after inserting [GHJ] is:------oo <-----------------> Ghj <----------- ------> +oo-oo <-----------------> Ghj <-----------------> +oo-oo <-----------------> GHJ <--- --------------> +oo-oo <-----------------> Ghj <-----------------> +oo-oo <-----------------> Ghj <-----------------> +oo-oo <-----------------> Ghj <-----------------> +oo-oo <---------> DEF <-> Ghj <---------> KLM <-> +oo-oo <-> ABC <-> DEF <-> ghj <---------> KLM <-> +oo-oo <-> ABC <-> DEF <-> ghj <-> HIJ <-> KLM <-> +oo-----The jump table before inserting [AAA] is: ------oo <-----------------> ghj <-----------------> +oo-oo <-----------------> Ghj <----------- ------> +oo-oo <-----------------> Ghj <-----------------> +oo-oo <-----------------> GHJ <--- --------------> +oo-oo <-----------------> Ghj <-----------------> +oo-oo <-----------------> Ghj <-----------------> +oo-oo & lt;---------> DEF <-> ghj <---------> KLM <-> +oo-oo <-> ABC <-> DEF <-> ghj <- --------> KLM <-> +oo-oo <-> ABC <-> DEF <-> ghj <-> HIJ <-> KLM <-> +oo--- --The Jump table after inserting [AAA] is:------oo <-------------------------> ghj <-----------------> +oo-oo <-------------- -----------> Ghj <-----------------> +oo-oo <-------------------------> Ghj <-----------------> +oo-oo <-------------------------> Ghj <-----------------> +oo-oo <-------------------------> Ghj & lt;-----------------> +oo-oo <-> AAA <-----------------> Ghj <-----------------> +oo-oo <-> AAA <---------> DEF <-> ghj <---------> KLM <-> +oo-oo <-> AAA <-> ABC <-> DEF <-> Ghj <---------> KLM;-> +oo-oo <-> AAA <-> ABC <-> DEF <-> ghj <-> HIJ <-> KLM <-> +oo-----Delete [A BC] After the jump table is:------oo <-----------------> ghj <-----------------> +oo-oo <-----------------> Ghj <- ----------------> +oo-oo <-----------------> Ghj <-----------------> +oo-oo <-----------------> Ghj <-----------------> +oo-oo <-----------------> Ghj <-----------------> +oo-oo <-> AAA <- --------> Ghj <-----------------> +oo-oo <-> AAA <-> DEF <-> ghj <---------> KLM &LT;-&G T +oo-oo <-> AAA <-> DEF <-> ghj <---------> KLM <-> +oo-oo <-> AAA <-> DEF < -> ghj <-> HIJ <-> KLM <-> +oo-----Delete [DEF] After the jump table is:------oo <---------> Ghj <--------------- --> +oo-oo <---------> Ghj <-----------------> +oo-oo <---------> Ghj <-----------------> +o O-oo <---------> Ghj <-----------------> +oo-oo <---------> Ghj <-----------------> +oo-oo <-> AAA <-> ghj <- ----------------> +oo-oo <-> aaa <-> Ghj <---------> KLM <-> +oo-oo <-> AAA <-> G HJ <---------> KLM <-> +oo-oo <-> AAA <-> ghj <-> HIJ <-> KLM <-> +oo-----Delete [K LM] After the jump table is:------oo <---------> ghj <---------> +oo-oo <---------> Ghj <---------> +oo-oo <-- -------> Ghj <---------> +oo-oo <---------> Ghj <---------> +oo-oo <---------> GHJ <------ ---> +oo-oo <-> AAA <-> ghj <---------> +oo-oo <-> AAA <-> ghj <---------> +oo-o o <-> AAA <-> ghj <---------> +oo-oo <-> AAA <-> ghj <-> HIJ <-> +oo-----Delete [HI J] After the jump table is:------oo <---------> ghj <-> +oo-oo <---------> ghj <-> +oo-oo <---------> Ghj & Lt;-> +oo-oo <---------> Ghj <-> +oo-oo <---------> ghj <-> +oo-oo <-> AAA <-> ghj <-> +oo-oo ;-> AAA <-> ghj <-> +oo-oo <-> aaa <-> ghj <-> +oo-oo <-> aaa <-> ghj <- > +oo-----Delete [Ghj] After the jump table is:------oo <-> aaa <-> +oo-oo <-> aaa <-> +oo-oo <-> AAA <-& Gt 
+oo-oo <-> AAA <-> +oo-----Delete [AAA] After the jump table is:------oo <-> +oo-----Insert [ABC] before the jump table is:------oo <-> +oo The Jump table after-----Insert [ABC] is:------oo <-> ABC <-> +oo-----The jump table before inserting [DEF] is:------oo <-> ABC <-> +oo-----  The jump table after inserting [DEF] is:------oo <---------> def <-> +oo-oo <---------> def <-> +oo-oo <---------> DEF <-> +oo-oo <---------> DEF <-> +oo-oo <-> ABC <-> def <-> +oo-----Insert Jump before [KLM] Table:------oo <---------> def <-> +oo-oo <---------> def <-> +oo-oo <---------> def &LT;-&G T +oo-oo <---------> DEF <-> +oo-oo <-> ABC <-> DEF <-> +oo-----The jump table after [KLM] is:------oo <---------> D EF <---------> +oo-oo <---------> DEF <---------> +oo-oo <---------> DEF <---------> +oo- Oo <---------> DEF <---------> +oo-oo <-> ABC <-> DEF <-> KLM <-> +oo-----Insert before [HIJ] Jump tables are:------oo <---------> DEF <---------> +oo-oo <---------> DEF <---------> +oo-oo <-------  --> DEF <---------> +oo-oo <---------> DEF <---------> +oo-oo <-> ABC <-> DEF <-> KLM <-> +oo-----The jump table after inserting [HIJ] is:------oo <---------> DEF <-----------------> +oo-oo <--------- ; DEF <-----------------> +oo-oo <---------> DEF <-----------------> +oo-oo <---------> DEF <- > HIJ <---------> +oo-oo <-> ABC <-> DEF <-> HIJ <-> KLM <-> +oo-----Insert Jump before [GHJ] Table:------oo <---------> DEF <-----------------> +oo-oo <---------> DEF <-----------------> +oo-oo <---------> DE F <-----------------> +oo-oo <---------> DEF <-> HIJ <---------> +oo-oo <-> ABC <-> DEF <-> HIJ <-> KLM <-> +oo-----The jump table after inserting [GHJ] is:------oo <---------> DEF <------------------- ------> +oo-oo <---------> DEF <-------------------------> +oo-oo <---------> DEF <----------- --------------> +oo-oo <---------> DEF <---------> HIJ <---------> +oo-oo <-> ABC <-> D EF <-> ghj <-> HIJ <-> KLM <-> +oo-----The jump table before inserting [AAA] is:------oo <---------> DEF <------- ------------------> +oo-oo <---------> DEF <-------------------------> +oo-oo <---------> DEF <-------------------------> +oo-oo <---------> DEF <---------> HIJ <---------> +oo-oo <- > ABC <-> DEF <-> Ghj <-> HIJ <-> KLM <-> +oo-----The jump table after inserting [AAA] is:------oo <-----------------> DEF <--------------- ----------> +oo-oo <-----------------> DEF <-------------------------> +oo-oo <----------------- > DEF <-------------------------> +oo-oo <-----------------> DEF <---------> HIJ <---------
 > +oo-oo <-> AAA <-> ABC <-> DEF <-> ghj <-> HIJ <-> KLM <-> +oo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.