0

I tried my best implementing a Skip List in C++ in which I had a particular idea that I wanted to try out. Instead having every element in the base level be a column vector, I only only have a singly-based list in the base level, and I store simple integers as to what level they are in.

For Example, instead of getting heads 5 times and copying an object of Cat 5 times, I simply did

while(coinFilp() == heads) {
   node.level++
}

and then I used node.level as a height indicator.

The following is how I implemented the Skip List:

Iterator<T> after(int level, Iterator<T> it)
    {

        Node<T> *cNode = it.currentNode;
        Node<T> *nextNode = cNode->next;

        while (nextNode->level < level && nextNode != ll.getTrailer())
        {
            nextNode = nextNode->next;
        }
        return Iterator<T>(nextNode);
    }

    Iterator<T> skipSearch(T v)
    {
        Iterator<T> n = Iterator<T>(s);
        int currentLevel = s->level;

        while (currentLevel > -1)
        {

            n = Iterator<T>(s);

            // make sure one is not null
            while (after(currentLevel, n).currentNode != ll.getTrailer() && after(currentLevel, n).getValue() < v)
            {
                n = after(currentLevel, n);
            }
            currentLevel--;
        }
        return n;
    }

    Iterator<T> skipInsert(T v)
    {
        Iterator<T> it = skipSearch(v);
        Iterator<T> newElement = insertAfterSkip(v, it);
        Node<T> *n = newElement.currentNode;

        while (n->level >= currentLevel)
        {
            incrementLevel();
        }
        return newElement;
    }

    void incrementLevel()
    {
        s->level++;
        ll.getTrailer()->level++;
        currentLevel++;
    }

private:
    Iterator<T> insertAfterSkip(T val, Iterator<T> p)
    {

        Iterator<T> it = after(0, p);
        Node<T> *no = ll.insert(val, *(it.currentNode));
        n++;
        return Iterator(*no);
    }

These are just a few methods in my Skip List implementation. I mean I feel like instead of copying an object a certain number of times, we can just give it a random number and that would be its level. Firstly, is this implementation of a Skip List correct? And is assigning a number instead of copying objects a better way to approach it?

Thanks

2
  • 1
    If I understand correctly, you would want to create one node for representing a whole stack up to the highest level for that particular data value. But this takes away the possibility to link this value horizontally (previous/next) at each level. I might be missing something here, but if you just increment the level of a node without creating a new one, you take it away from its previous level, hurting the horizontally linked list you must have at that level. Commented Jun 2 at 9:24
  • 1
    I'm not clear on "copying an object." In every skiplist implementation I've seen, the "objects" you're copying are just references: 4-byte or 8-byte pointers. Sure, that's more expensive than just incrementing a counter, but nothing like copying a large object. And, as @trincot pointed out, there's some question of whether your modification works the way you think it does. Commented Jun 3 at 15:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.