简体   繁体   English

树中序遍历 Python

[英]Tree Inorder Traversal Python

I'm trying to add elements to a binary tree and print them in in-order.我正在尝试将元素添加到二叉树并按顺序打印它们。

I'm getting an error while adding an element: AttributeError: 'NoneType' object has no attribute 'left'添加元素时出现错误: AttributeError: 'NoneType' object has no attribute 'left'

Please let me know where I have to make a change Below is the code请让我知道我必须在哪里进行更改以下是代码

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

    def insert(self, data):
        if self.data:
            if data < self.data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.insert(data)
            elif data > self.data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.insert(data)

        else:
            self.data = data

    def InorderTraversal(self):
        if self.data is None:
            return
        self.left.InorderTraversal()
        print(self.data)
        self.right.InorderTraversal()



if __name__ == "__main__":
    root = Node(1)
    root.insert(2)
    root.insert(3)
    root.insert(4)
    root.InorderTraversal()

I am Implementing Trees First time Doesn't Have any idea我第一次实现树没有任何想法

You should check if a node has left & right children before recursing on them:您应该在递归之前检查一个节点是否有左右子节点:

class Node:

   ...
   def InorderTraversal(self):
        if self.left:  # if left child exists, then recurse on it
            self.left.InorderTraversal()
        print(self.data)
        if self.right:  # if right child exists, then recurse on it
            self.right.InorderTraversal()

Result:结果:

1
2
3
4

You need the Node to be a separate class which will be used by the Tree class.您需要该节点是一个单独的 class,它将被树 class 使用。
The insert and inorder_traversals are operations you do on a tree and not on a node. insert 和 inorder_traversals 是您在树上而不是在节点上执行的操作。 This is kinda the first thing you should do.这是你应该做的第一件事。

There are already pretty good resources you can look at since I might not be able to explain in that kind of detail: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/您已经可以查看相当不错的资源,因为我可能无法详细解释: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/

Also, you need to read up a little on how recursion and the classes work in python as I see some mistakes that can be fixed.此外,您需要阅读一些关于递归和类如何在 python 中工作的内容,因为我看到了一些可以修复的错误。
You're recursively printing the inorder traversal but you're doing the checks on self.data and running on self.left and self.right.您正在递归地打印中序遍历,但您正在检查 self.data 并在 self.left 和 self.right 上运行。 self is pointing to the same data and you need to pass the node you wanna process next in the recursive function. self 指向相同的数据,您需要在递归 function 中传递您接下来要处理的节点。
Also, you need to check the None condition on the Node and not on the data.此外,您需要检查节点上的无条件而不是数据。 Basically the node doesn't exist and that means you've to return from there基本上该节点不存在,这意味着您必须从那里返回

The inorder code overall idea is correct but you're working with wrong variables and basically you'll end up with incorrect output (or infinite loop)中序代码的总体思路是正确的,但是您使用的变量错误,基本上您最终会得到不正确的 output (或无限循环)



My suggestion is to read up some more on tree on geeksforgeeks to begin with!我的建议是先在 geeksforgeeks 上阅读更多关于树的内容!

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

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