0

这只是一个非常简单的代码,我的参考来自这里: http: //www.mathcs.emory.edu/~cheung/Courses/323/Syllabus/Map/skip-list-impl.html#why-q

我认为插入函数没问题,但是当我尝试使用该get()函数时,它不会返回任何内容,而是在searchEntry()零件内部无限循环。我不知道怎么了。在insert()功能上,searchEntry()运行良好。它返回对floorEntry(k)包含小于需要插入到跳过列表中的键的键的条目的引用。请帮我找出searchEntry()函数中错误的根源。对不起,我真的不擅长这个。谢谢!

from QuadLinkedList import QLLNode
import random
class Skippy:


    def __init__(self):

        self._p1 = QLLNode("MINUS_INF")
        self._p2 = QLLNode("PLUS_INF")

        self._head = self._p1
        self._tail = self._p2

        self._p1.setNext(self._p2)
        self._p2.setPrev(self._p1)

        self._height = 0
        self._n = 0

    def insert(self, key, value):

        p = self.searchEntry(key)
        print "p = " + str(p.getKey())
        q = QLLNode(key, value)
        q.setPrev(p)
        q.setNext(p.getNext())
        p.getNext().setPrev(q)
        p.setNext(q)

        i = 0

        while random.randint(0,1) != 0:

            if i >= self._height:
                self._height += 1

                newHead = QLLNode("MINUS_INF")
                newTail = QLLNode("PLUS_INF")

                newHead.setNext(newTail)
                newHead.setDown(self._head)

                newTail.setPrev(newHead)
                newTail.setDown(self._tail)

                self._head.setUp(newHead)
                self._tail.setUp(newTail)

                self._head = newHead
                self._tail = newTail

            while p.getUp() == None:
                p = p.getPrev()

            p = p.getUp()

            e = QLLNode(key,None)

            e.setPrev(p)
            e.setNext(p.getNext())
            e.setDown(q)

            p.getNext().setPrev(e)
            p.setNext(e)
            q.setUp(e)

            q = e

            i += 1

        self._n += 1
        return None

    def get(self, key):
        p = self.searchEntry(key)

        if key == p.getKey():
            return p.getElement()
        else:
            return "There's None!"

    def searchEntry(self, key):
        p = self._head

        while True:
            while p.getNext().getKey() != "PLUS_INF" and p.getNext().getKey() <= key:
                p = p.getNext()
            if p.getDown() != None:
                p = p.getDown()
            else:
                break
        return p
4

1 回答 1

1

问题不在 的代码中searchEntry,它似乎具有正确的逻辑。问题是列表结构变得混乱。我认为问题出在您用于在insert. 特别是这一点:

       if i >= self._height:    #make an empty level

            self._height += 1

            self._minus.setNext(self._plus)
            self._minus.setDown(self._head)

            self._plus.setPrev(self._minus)
            self._plus.setDown(self._tail)

            self._head.setUp(self._minus)
            self._tail.setUp(self._plus)

            self._head = self._minus
            self._tail = self._plus

这段代码让我印象深刻的是,您没有创建任何新节点,只是修改现有节点,这就是破坏列表结构的原因。我认为你需要创建新的headtail节点,并将它们链接到结构的顶部。(minus并且plus不是链接中描述的算法的一部分,所以我不确定你在用它们做什么。)可能你想要这样的东西:

       if i >= self._height:    #make an empty level

            self._height += 1

            newHead = QLLNode("MINUS_INF")
            newTail = QLLNode("PLUS_INF")

            newHead.setNext(newTail)
            newHead.setDown(self._head)

            newTail.setPrev(newHead)
            newTail.setDown(self._tail)

            self._head.setUp(newHead)
            self._head = newHead

            self._tail.setUp(newTail)
            self._tail = newTail
于 2014-07-14T18:34:36.543 回答