[英]I am trying to implement a Skiplist using Python. Can you help me? Very simple
This is just a very simple code and my reference is from here: http://www.mathcs.emory.edu/~cheung/Courses/323/Syllabus/Map/skip-list-impl.html#why-q 这只是一个非常简单的代码,我的参考来自这里: http : //www.mathcs.emory.edu/~cheung/Courses/323/Syllabus/Map/skip-list-impl.html#why-q
I think the insert function is okay but when I try to use the get()
function, it doesn't return anything, instead it loops endlessly inside the searchEntry()
part. 我认为insert函数还可以,但是当我尝试使用get()
函数时,它不返回任何内容,而是在searchEntry()
部分内无限循环。 I don't know what's wrong. 我不知道怎么了 In the insert()
function, the searchEntry()
operates well. 在insert()
函数中, searchEntry()
运行良好。 It returns the reference to the floorEntry(k)
entry containing a key that is smaller than the key that needs to be inserted in the skiplist. 它返回对floorEntry(k)
条目的引用,该条目包含一个键,该键小于需要在floorEntry(k)
中插入的键。 Please help me figure out the source of the error in the searchEntry()
function. 请帮助我找出searchEntry()
函数中错误的来源。 I'm sorry I'm not really good at this. 对不起,我不是很擅长此事。 Thank you! 谢谢!
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
The issue isn't in the code for searchEntry
, which appears to have the correct logic. 问题不存在于searchEntry
的代码中,后者似乎具有正确的逻辑。 The problem is that the list structure is getting messed up. 问题在于列表结构变得混乱。 I believe the issue is with the code you have for adding a new level to the list in insert
. 我相信问题出在insert
代码中向列表添加新级别的代码。 Specifically this bit: 具体来说:
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
The thing that stands out to me about this code is that you're not creating any new nodes, just modifying existing ones, which is what is breaking your list structure. 关于此代码,对我而言突出的是,您不会创建任何新节点,而只是修改现有节点,这破坏了列表结构。 You need to create new head
and tail
nodes, I think, and link them into the top of the strucutre. 我认为您需要创建新的head
和tail
节点,并将它们链接到结构顶部。 ( minus
and plus
are not part of the algorithm as described at your link, so I'm not sure what you're doing with them.) Probably you want something like this: ( 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.