python - python中的实例属性调试,如果条件不按预期工作,实例的这个属性是空的?
问题描述
我有一个与类实例值问题有关的问题。下面的代码由一个二叉树类和一些搜索方法(深度优先搜索)组成,它们是预排序算法和中序算法。
代码 100% 工作问题是当我调试 root.in_order() 函数时打印出 b、a、c 但预期的返回值为 d、b、a、e、c、f。
class Node:
def __init__(self, value):
self.value = value
self.left_hand = None
self.right_hand = None
def insert_left(self,value):
if self.left_hand == None:
self.left_hand = Node(value)
else:
new_node = Node(value)
new_node.left_hand = self.left_hand
self.left_hand = new_node
def insert_right(self,value):
if self.right_hand == None:
self.right_hand = Node(value)
else:
new_node = Node(value)
will_be_loaded_right_child = self.right_hand
self.right_hand = new_node
new_node.right_hand = will_be_loaded_right_child
def pre_order(self):
print(self.value.value)
print(repr(self.value))
if self.left_hand:
self.left_hand.pre_order()
if self.right_hand:
self.right_hand.pre_order()
def in_order(self):
if self.left_hand:
self.left_hand.in_order()
print(self.value)
if self.right_hand:
self.right_hand.in_order()
def __str__(self):
return "{}".format(self.value)
def printAtt(self , arr = []):
if arr.count>1:
try:
arr = [self.value, self.left_hand.value, self.right_hand.value]
print(arr)
except AttributeError:
print("""There is nothing to show of the root |-> {} <-|
Node |-> {} <-| has not right or left handside value,
please continue to add or it
could be the last child:D""".format(self.value.value, self.value.value))
root = Node("a")
node_b = Node("b")
node_c = Node("c")
node_d = Node("d")
node_e = Node("e")
node_f = Node("f")
root.insert_left(node_b)
root.insert_right(node_c)
node_b.insert_right(node_d)
node_c.insert_left(node_e)
node_c.insert_right(node_f)
root.in_order()
解决方案
只需更改脚本的最后一部分,
insert_left
并insert_right
期望值。
更新版本如下:
class Node:
def __init__(self, value):
self.value = value
self.left_hand = None
self.right_hand = None
def insert_left(self,value):
if self.left_hand == None:
self.left_hand = Node(value)
else:
new_node = Node(value)
new_node.left_hand = self.left_hand
self.left_hand = new_node
def insert_right(self,value):
if self.right_hand == None:
self.right_hand = Node(value)
else:
new_node = Node(value)
will_be_loaded_right_child = self.right_hand
self.right_hand = new_node
new_node.right_hand = will_be_loaded_right_child
def pre_order(self):
print(self.value)
if self.left_hand:
self.left_hand.pre_order()
if self.right_hand:
self.right_hand.pre_order()
def in_order(self):
if self.left_hand:
self.left_hand.in_order()
print(self.value)
if self.right_hand:
self.right_hand.in_order()
def post_order(self):
if self.left_hand:
self.left_hand.post_order()
if self.right_hand:
self.right_hand.post_order()
print(self.value)
root = Node("a")
root.insert_left("b")
root.insert_right("c")
node_b = root.left_hand
node_c = root.right_hand
node_b.insert_right("d")
node_c.insert_left("e")
node_c.insert_right("f")
root.in_order()
print("*"*30)
root.pre_order()
print("*"*30)
root.post_order()
推荐阅读
- javascript - 将HTML转换为图像时如何显示图像
- python - 如何获得变压器模型的输入形状
- java - Android Firebase-没有将所有键作为列表,我该怎么办?
- c++ - C++大于检查返回错误结果
- c - 宏标识符未声明,即使已声明
- javascript - 当我在 chrome 的控制台中运行 js 代码时,我得到不同的打印
- c# - 异步/等待跳过任务中的其余代码
- python - 我收到导入错误。如何解决这个问题?
- git - 带有 Docker 文件的 Openshift git 源
- r - 如何绘制带有不同级别标签的plot.dendrogram(如plot.hclust)?