首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Skiplist -尝试实现get()和set()方法

Skiplist -尝试实现get()和set()方法
EN

Stack Overflow用户
提问于 2020-03-14 01:33:30
回答 2查看 394关注 0票数 2

所以我正在尝试实现一个FastDefaultList类,它基本上是一个跳表,它表示一个索引为0,1,2,3,…的无限列表、∞.开始时,此列表中的每个值都被指定为默认值null。否则,这个类的行为就像一个列表;它有add(i,x)、remove(i)、set(i,x)和get(i),它们的行为就像列表中的相同方法。这些操作中的每一个都应该在O(log )时间内运行。我的许多skiplist代码来自于:

https://opendatastructures.org/odsjava/4_2_SkiplistSSet_Efficient_.html

我想我的大部分代码都能正常工作,但我正在尝试修复get()和set()方法。每当我试图编译这个程序时,我都会得到错误:

错误:找不到适合add(int,FastDefaultList.Node,int) add(i,node,0)的方法;

我也不知道为什么。任何帮助都将不胜感激。

代码语言:javascript
复制
package comp;
import java.lang.reflect.Array;
import java.lang.IllegalStateException;
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Random;


public class FastDefaultList<T> extends AbstractList<T> {
    class Node {
        T x;
        Node[] next;
        int[] length;
        @SuppressWarnings("unchecked")
        public Node(T ix, int h) {
            x = ix;
            next = (Node[])Array.newInstance(Node.class, h+1);
            length = new int[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;   // Hint: You don't need this anymore!

    /**
     * A source of random numbers
     */
    Random rand;


    public FastDefaultList() {
        n = 0;
        sentinel = new Node(null, 32);
        h = 0;
        rand = new Random(0);
    }


    /**
     * Represents a node/index Pair
     */
    protected class Pair {
        Node u;
        int j;

        Pair(Node u, int j) {
            this.u = u;
            this.j = j;
        }
    }

    protected Pair findPred(int i) {
        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 new Pair(u, j);
    }

    public T get(int i) {

        if (i < 0 || i > size()-1) throw new IndexOutOfBoundsException();
        Pair n = findPred(i);
        if(n.u.next[0] != null && n.j + n.u.length[0] == i){ return n.u.next[0].x; }
        return null;        
    }

    public T set(int i, T x) {

        if (i < 0 || i > size()-1) throw new IndexOutOfBoundsException();
        Pair n = findPred(i);
        if (n.u.next[0] != null && n.j + n.u.length[0] == i) {
            Node u = n.u.next[0];
            T y = u.x;
            u.x = x;
            return y;
        }
        else{
            Node node = new Node(x, pickHeight());
            if(node.height() > h){ h = node.height(); }
            add(i, node, 0);
        }
        return null;
    }

    /**
     * Insert a new node into the skiplist
     * @return the node u that precedes v in the skiplist
     */
    protected Node add(int i, Node w) {
        Node u = sentinel;
        int k = w.height();
        int r = h;
        int j = -1; // index of u
        while (r >= 0) {
            while (u.next[r] != null && j+u.length[r] < i) {
                j += u.length[r];
                u = u.next[r];
            }
            u.length[r]++; // accounts for new node in list 0
            if (r <= k) {
                w.next[r] = u.next[r];
                u.next[r] = w;
                w.length[r] = u.length[r] - (i - j);
                u.length[r] = i - j;
            }
            r--;
        }
        n++;
        return u;
    }

    /**
     * Simulate repeatedly tossing a coin until it comes up tails.
     * Note, 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 & m) != 0) {
            k++;
            m <<= 1;
        }
        return k;
    }

    public void add(int i, T x) {
        if (i < 0 || i > size()-1) throw new IndexOutOfBoundsException();
        Node w = new Node(x, pickHeight());
        if (w.height() > h)
            h = w.height();
        add(i, w);
    }

    public T remove(int i) {
        if (i < 0 || i > size()-1) throw new IndexOutOfBoundsException();
        T x = null;
        Node u = sentinel;
        int r = h;
        int j = -1; // index of node u
        while (r >= 0) {
            while (u.next[r] != null && j+u.length[r] < i) {
                j += u.length[r];
                u = u.next[r];
            }
            u.length[r]--;  // for the node we are removing
            if (j + u.length[r] + 1 == i && u.next[r] != null) {
                x = u.next[r].x;
                u.length[r] += u.next[r].length[r];
                u.next[r] = u.next[r].next[r];
                if (u == sentinel && u.next[r] == null)
                    h--;
            }
            r--;
        }
        n--;
        return x;
    }


    public int size() {
        return Integer.MAX_VALUE;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
            int i = -1;
            Node u = sentinel;
            while (u.next[0] != null) {
                i += u.length[0];
                u = u.next[0];
                sb.append(" " + i + "=>" + u.x);
            }
            return sb.toString();
    }

    public static void main(String[] args) {

    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-14 01:37:53

您的add函数接受2个参数

代码语言:javascript
复制
protected Node add(int i, Node w)

但你用3个参数来调用它-

这里:

代码语言:javascript
复制
 if(node.height() > h){ h = node.height(); }
        add(i, node, 0);
票数 0
EN

Stack Overflow用户

发布于 2020-03-14 01:46:18

您的代码和AbstractList都没有定义具有签名add(int,Node,int)的方法。

尽管如此,在set()方法中还是有这样一个方法的调用:

代码语言:javascript
复制
public T set(int i, T x) {

    if (i < 0 || i > size()-1) throw new IndexOutOfBoundsException();
    Pair n = findPred(i);
    if (n.u.next[0] != null && n.j + n.u.length[0] == i) {
        Node u = n.u.next[0];
        T y = u.x;
        u.x = x;
        return y;
    }
    else{
        Node node = new Node(x, pickHeight());
        if(node.height() > h){ h = node.height(); }
        /**/add(i, node, 0)/**/;
    }
    return null;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60674940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档