简体   繁体   English

我正在尝试使用Python实现Skiplist。 你能帮助我吗? 很简单

[英]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. 我认为您需要创建新的headtail节点,并将它们链接到结构顶部。 ( 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.

相关问题 你能帮我解决这个问题吗? 我正在尝试使用 python 创建一个简单的计算器 - can you help me with this? im trying to crete a simple calculator using python 我有一个CSV文件,我正在尝试使用python提取数据。 没有得到正确的结果。 有人可以帮忙吗? 附带的代码和样本数据 - I have a CSV file and I am trying to extract data using python. Not getting correct results. Can someone help? Code and sample data attached 我正在尝试在 python 中使用 sklearn 的mutual_info_classif。 它给我一个错误? - i am trying to use sklearn's mutual_info_classif in python. It is throwing me a error? 我做了一个非常简单的 Pygame 零游戏,但是我的 Actor 图像和 screen.clear() 不起作用。 你能帮我解决这个问题吗? - I made a very simple Pygame Zero game, but my Actor image and the screen.clear() is not working. Can you help me fix this? 我正在用 python 做一个语音助手,遇到了一个错误,你能帮我解决这个问题吗? - I am doing a voice Assistant with python and got an error can you help me with this? 我正在尝试用 python 制作一个测验应用程序,并想单独显示问题和选项有人可以帮助我吗? - I am trying to make a quiz app in python and want to display the question and options separately can someone help me? 我的 python 代码出现错误,你能帮我修复它吗? - i am getting an error in my python code can you help me fix it? Python,我正在尝试制作排行榜,但它不会保存在文本文件中,有人可以帮助我吗? - Python, I am trying to make a leader board but it wont save in the text file can someone help me? 我正在尝试通过调用命令提示符在 qt 创建者中运行 python 脚本。 有人可以帮我吗? - I am trying to run a python script in qt creator by calling command prompt. Can anybody help me with this? 我正在尝试使用 Python 从 Excel 检索内容。 我收到投资未定义错误 - I am trying to retrieve the content from an Excel using Python. I am getting Investment undefined error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM