[英]Difficulty with storing key-value pairs in Binary Search Tree in Python
I need to store user objects with each key in my BST.我需要在我的 BST 中使用每个键存储用户对象。 class
BSTNode
represents the nodes of our tree. class
BSTNode
代表我们树的节点。
Here is the code of my BST node class:这是我的 BST 节点 class 的代码:
class BSTNode():
def __init__(self, key, value=None):
self.key = key
self.value = value
self.left = None
self.right = None
self.parent = None
When I try inputting usernames as keys and user objects as values当我尝试输入用户名作为键和用户对象作为值时
tree = BSTNode(john.username, john)
The following error gets thrown:抛出以下错误:
NameError Traceback (most recent call last)
Input In [62], in <cell line: 2>()
1 #Level 0
----> 2 tree = BSTNode(john.username, john)
NameError: name 'john' is not defined
What am I doing wrong?我究竟做错了什么?
Is john object already created, seems python is not able to identify john.username
. john object 是否已经创建,似乎 python 无法识别
john.username
。 Also better to store the key as the object itself rather than john.username
as it will resolve to the username value itself.最好将密钥存储为 object 本身而不是
john.username
,因为它将解析为用户名值本身。
It says john is not created.它说约翰没有被创造。 it means error is at last line at which you are making instance of
BSTNode
and passing value of john.username
and john is not defined previously.这意味着错误在您创建 BSTNode 实例的最后一行,并且之前
john.username
BSTNode
john 的传递值。 so please defined the variable john
first.所以请先定义变量
john
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.