我试图破译这些代码贴在几年前在这里:How to implement a binary search tree in Python?将自变量传递到新函数时发生了什么?
,我感到困惑的部分是本节:
class Node:
def __init__(self, val):
self.l_child = None
self.r_child = None
self.data = val
def binary_insert(root, node):
if root is None:
root = node
else:
if root.data > node.data:
if root.l_child is None:
root.l_child = node
else:
binary_insert(root.l_child, node)
else:
if root.r_child is None:
root.r_child = node
else:
binary_insert(root.r_child, node)
类和功能,然后通过这样叫:
r = Node(3)
binary_insert(r, Node(7))
binary_insert(r, Node(1))
binary_insert(r, Node(5))
我的问题是:传递到binary_insert函数时发生了什么self.data? node.data和root.data来自哪里?
'self'是'Node'类的实例.'root'和'node'都是'Node'类的实例。按照...... * self = root的方式思考它。 self.data * – Bahrom