我正在SkiplistList上实现一个类

时间:2017-10-29 02:41:20

标签: java collections skip-lists

protected Node findPred(int i) {
    // Hint: It's not enough to know u, you also need the value j,
    //       maybe return the pair (u,j)
    Node u = sentinel;
    int r = h;
    int j = -1;   // index of the current node in list 0
    while (r >= 0) {
        while (u.next[r] != null && j + u.length[r] < i) {
            j += u.length[r];
            u = u.next[r];
        }
        r--;
    }
    return u;
}

public T get(int i) {
    // Hint: this is too restrictive any non-negative i is allowed
    if (i < 0 || i > n-1) throw new IndexOutOfBoundsException();
    // Hint: Are you sure findPred(i).next is the node you're looking for?
    return findPred(i).next[0].x;
}

public T set(int i, T x) {
    // Hint: this is too restrictive any non-negative i is allowed
    if (i < 0 || i > n-1) throw new IndexOutOfBoundsException();
    // Hint: Are you sure findPred(i).next is the node you're looking for?
    //       If it's not, you'd better add a new node, maybe get everything
    //       else working and come back to this later.
    Node u = findPred(i).next[0];
    T y = u.x;
    u.x = x;
    return y;
}

虽然有一些提示,但我仍然不明白j值是什么意思,为什么我们在其他方法中需要这个j值?我们可以将这个j值用于什么?这是基于跳过列表。

0 个答案:

没有答案