这只是一个非常简单的代码,我的参考来自这里: 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