简体   繁体   English

在Python中打印二叉树插入的每个阶段的最佳方法

[英]Best way to print every stage of Binary Tree insertion in Python

I trying to print each stage of a Binary Search Tree after every step of Insertion. 我尝试在插入的每个步骤之后打印二进制搜索树的每个阶段。 However the output generated by my code seems to miss out the initial root data insertion step and also repeats certain stages. 但是,由我的代码生成的输出似乎错过了初始根数据插入步骤,并且还会重复某些阶段。 I need a clean and easy way to do this. 我需要一种干净简便的方法来执行此操作。 Is there a way to do it using __dict__.values() . 有没有办法使用__dict__.values()做到这__dict__.values()

My Code: 我的代码:

class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

    def addNode(self, data):
        if data < self.data:
            if self.left is None:
                self.left = Node(data)
            else:
                self.left.addNode(data)  # recursively calling addNode method
        else:
            if self.right is None:
                self.right = Node(data)
            else:
                self.right.addNode(data)

        if not self.left is None and not self.right is None:
            print self.left.data, self.data, self.right.data
        elif self.left is None and not self.right is None:
            print None, self.data, self.right.data
        elif not self.left is None and self.right is None:
            print self.left.data, self.data, None

if __name__ == '__main__':
    n = Node(5)
    #print n.__dict__.values()
    n.addNode(3)
    n.addNode(2)
    n.addNode(8)
    n.addNode(4)

Output Generated: 生成的输出:

3 5 None
2 3 None
3 5 None
3 5 8
2 3 4
3 5 8

Expected Output: 预期产量:

None 5 None
3 5 None
2 3 None
3 5 8
2 3 4

Because of the way you designed this, the printing step is happening on each recursive step, from the inside to the outside. 由于您采用了这种设计方式,因此从内部到外部的每个递归步骤都将执行打印步骤。 So, for that to make more sense, lets walk through this. 因此,为了使之更有意义,让我们逐步完成。 You n.addNode(3) which gives us n.addNode(3)给我们

3 5 None

No surprises. 没什么好奇怪的 But when you say you expect: 但是当您说您期望:

2 3 None

but get both, 但要两者兼得

2 3 None
3 5 None

its because to do n.addNode(2) is to examine the first level ( 3 5 None ) then to look at the lower level (the node with data attribute = 3 ). 这是因为要做n.addNode(2)是检查第一个级别( 3 5 None ),然后查看下一个级别( data属性为3的节点)。 So, printing "from the inside out", we get 2 3 None (new subtree) then the first level again, 3 5 None . 因此,“从内到外”打印,我们得到2 3 None (新的子树),然后得到第一层, 3 5 None Its for the same reason that when you expect, 出于同样的原因,当您期望时,

2 3 4

you instead get, 你反而得到

2 3 4
3 5 8

The fact that the initial tree (where you expect None 5 None ) is not printing is a separate issue. 初始树(您期望None 5 None )不在打印的事实是一个单独的问题。 It's just because you are initializing the data attribute and don't ever get to the printing part because you don't call addNone . 这仅仅是因为您正在初始化data属性,而从未去打印部分,因为您没有调用addNone

A possible solution (that still maintains your structure) would be to only print when you add the node to the tree structure and print the initial subtree manually. 一种可能的解决方案(仍保持您的结构)是仅在将节点添加到树结构并手动打印初始子树时打印。 Maybe write something like this: 也许这样写:

class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

    def addNode(self, data):
        if data < self.data:
            if self.left is None:
                self.left = Node(data)
                self.printSubtree()
            else:
                self.left.addNode(data)  # recursively calling addNode method
        else:
            if self.right is None:
                self.right = Node(data)
                self.printSubtree()
            else:
                self.right.addNode(data)

    def printSubtree(self):
        if not self.left is None and not self.right is None:
            print self.left.data, self.data, self.right.data
        elif self.left is None and not self.right is None:
            print None, self.data, self.right.data
        elif not self.left is None and self.right is None:
            print self.left.data, self.data, None
        else:
            print None, self.data, None

if __name__ == '__main__':
    n = Node(5)
    n.printSubtree()
    n.addNode(3)
    n.addNode(2)
    n.addNode(8)
    n.addNode(4)

1- you printed after adding the nodes, so you can't find an element with None,Number,None as you expect.(so try to move this part before adding the node). 1-在添加节点后打印,因此您找不到所期望的None,Number,None的元素。(因此请在添加节点之前尝试移动此部分)。 2- when printing you only considered three possibilities but there is another one, when the left and the right nodes are both None, try adding this. 2-当打印时,您仅考虑了三种可能性,而另一种可能性是,当左右节点均为无时,请尝试添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM