简体   繁体   English

具有键和值的Python二进制搜索树

[英]Python Binary Search Tree with key and value

I need to implement a Binary Search Tree class as homework but I struggle making the insert function. 我需要将Binary Search Tree类作为家庭作业来实现,但是我在制作insert函数时遇到了困难。 I have looked through Google a lot to find some solutions or possibilities on how to do it but none of them has used a key and value (mostly just value) or if they used a key aswell, they had tons of seperate functions which I am not allowed to do I think. 我已经通过Google进行了很多研究,找到了一些解决方案或解决方案,但其中没有一个使用键和值(主要是值),或者如果他们也使用键,它们都有大量的独立功能,我不允许这样做。

So the pre-built is simply that: 因此,预先构建的只是:

class Node:
    def __init__(self, key, value):
        self.key = key
        self.value = value
        self.left = self.right = None

class BinarySearchTree:
    def __init__(self):
        self.root = None
        self.size = 0

    def __len__(self):
        return self.size

    def insert(self, key, value):
        pass

    def remove(self, key):
        pass

    def find(self, key):
        pass

Now the thing is, if I want to check for example whether the value is smaller or bigger than a current Node to put it either right or left, I get Errors such as "root is not defined" or "root.right" has no such attribute etc... And I guess that makes sense because self.root is declared as None. 现在的问题是,如果我想检查例如该值是小于还是大于当前Node的值以将其放在右边还是左边,我会得到诸如“未定义root”或“ root.right”之类的错误信息这样的属性等...我想这很有意义,因为self.root被声明为None。

But how do I actually fix it now to make the insert function work? 但是,现在如何实际修复它以使插入功能正常工作?

I am a little confused by this task as it uses key + value, so I need to insert the value bound to the specific key and in case the key already existed, overwrite its value. 我对此任务有点困惑,因为它使用键+值,因此我需要插入绑定到特定键的值,并在键已经存在的情况下覆盖其值。

You didn't specify, but I'm guessing the point of the keys is to determine if a particular key is already in the tree, and if so, replace the related node's value in O(1) runtime complexity. 您没有指定,但是我猜这些键的要点是确定树中是否已经有特定的键,如果是,则以O(1)运行时复杂度替换相关节点的值。

So when you're inserting a node, you will first check the dictionary for the key (you will initialize an empty dictionary yourself in __init__ ). 因此,当您插入节点时,首先要检查字典中的密钥(您将自己在__init__初始化一个空字典)。 If it already is there, then you simply just need to replace the value of the node for that particular key. 如果已经存在,则只需要替换该特定键的节点值即可。 Otherwise, you add the new node the same way that you would in any BST, and also remember to update your dictionary to map the key to it's node. 否则,您将以与任何BST中相同的方式添加新节点,并且还记得更新字典以将键映射到其节点。

its 5 in the morning so this might be all wrong, but here goes: 它是早上5点,所以这可能全错了,但是这里有:
the key is what we are sorting by, the values aren't interesting 关键是我们排序的依据,值并不有趣
your insert function should probably look something like this: 您的插入函数应该看起来像这样:

def insert(self, key, value):
        if self.root = None:
            self.root = Node(key,value)
            return
        #regular binary tree traversal (comparing the key) to find where to insert, lets assume we need to insert on the left
        parent.left = Node(key,value)

can you figure it out from here or would you like more direction 你能从这里弄清楚还是想要更多方向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM