[英]Recursive tree insertion only creates root node
My function is given a vector.我的 function 被赋予了一个向量。 It gets the maximum value from it and creates a root node for it.
它从中获取最大值并为其创建一个根节点。 It then divides the vector in two at the index of this highest value (excluding that value itself).
然后它在这个最高值的索引处将向量一分为二(不包括该值本身)。 The left side of the vector should then be used recursively to set the root's left child.
然后应该递归地使用向量的左侧来设置根的左孩子。 The same should happen at the right.
同样的情况也应该发生在右边。
But my code is only filling in the root node.但我的代码只是填写根节点。 Why?
为什么?
import numpy as np
class Node:
def __init__(self, information):
self.information = information
self.left = None
self.right = None
class Max_Tree:
def __init__(self):
self.root = None
def insert(self, array):
if len(array) == 0:
return 0
max_value_index = np.argmax(array)
self.root = Node(array[max_value_index])
array_left = array[0: max_value_index]
array_right = array[max_value_index + 1:]
self._insert(array_left, self.root.left)
self._insert(array_right, self.root.right)
def _insert(self, array, node):
if len(array) == 0:
return 0
max_value_index = np.argmax(array)
node = Node(array[max_value_index])
array_left = array[0: max_value_index]
array_right = array[max_value_index + 1:]
self._insert(array_left, node.left)
self._insert(array_right, node.right)
Example run:示例运行:
array = np.array([2, 42, 17, 13, 21, 50, 32, 9, 14])
max_tree = Max_Tree()
max_tree.insert(array)
print(max_tree.root.information) # OK
print(max_tree.root.left) # None ???? Why ???
The problem is that when you call self._insert
, the second argument is always None
.问题是当你调用
self._insert
时,第二个参数总是None
。 In Python arguments are passed by value, so in this case there is just the value None
... there is no way the function is going to update the variable of the caller: it has its own parameter variable, and only that variable will get the reference to the new Node that it creates.在 Python arguments 中是按值传递的,所以在这种情况下只有值
None
... function 将更新它的变量,并且只有调用者的变量将具有它自己的参数:对它创建的新节点的引用。 But that reference is lost when the function returns.但是当 function 返回时,该参考将丢失。 So all that work is done for nothing.
因此,所有这些工作都是徒劳的。
Instead, design your self._insert
function so that it will not take that second argument, but will return the Node
it creates for the given array.相反,请设计您的
self._insert
function 以便它不会采用第二个参数,而是返回它为给定数组创建的Node
。 Then it is the caller's responsibility to assign that returned Node
instance to whatever property ( left
, right
, root
, ...).然后调用者负责将返回的
Node
实例分配给任何属性( left
, right
, root
,...)。
Here is how self._insert
should be adapted:下面是
self._insert
应该如何适应:
def _insert(self, array):
if len(array) == 0:
return # Return None!
max_value_index = np.argmax(array)
node = Node(array[max_value_index])
array_left = array[0: max_value_index]
array_right = array[max_value_index + 1:]
node.left = self._insert(array_left) # Assign the returned Node instance (or None)
node.right = self._insert(array_right) # Same priniciple
return node # Return the new Node with all its descendants
Once you have this, the main self.insert
function does not have to repeat any of this code.一旦你有了这个,主
self.insert
function 就不必重复任何这段代码了。 It can just delegate the call to self._insert
and assign the result to the root:它可以将调用委托给
self._insert
并将结果分配给根:
def insert(self, array):
self.root = self._insert(array)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.