首页 > 解决方案 > 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()

标签: pythondebuggingbinary-treebreadth-first-search

解决方案


只需更改脚本的最后一部分,insert_leftinsert_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()

推荐阅读