Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类中的Python二叉树属性问题_Python_List_Tree_Binary - Fatal编程技术网

类中的Python二叉树属性问题

类中的Python二叉树属性问题,python,list,tree,binary,Python,List,Tree,Binary,我收到错误消息: 回溯(最近一次呼叫最后一次): 文件“…”,第36行,在 root.data.append(“L”) AttributeError:“节点”对象没有属性“数据” 但我的类中显然有一个数据属性 class Node: def _init_(self): self.left = None self.right = None self.data = list() def addPerson(root, person):

我收到错误消息: 回溯(最近一次呼叫最后一次): 文件“…”,第36行,在 root.data.append(“L”) AttributeError:“节点”对象没有属性“数据”

但我的类中显然有一个数据属性

class Node:
    def _init_(self):
        self.left = None
        self.right = None
        self.data = list()

def addPerson(root, person):
    if person < root.data[0]:
        if root.left == None:
            root.left = Node()
            root.left.data.append(person)
        else:
            addPerson(root.left,person)
    else:
        if person > root.data[0]:
            if root.right == None:
                root.right = Node()
                root.right.data.append(person)
            else:
                addPerson(root.right,person)
        else:
            root.data.append(person)

def printPerson(root):
    if root == None:
        return
    print(root.data)
    printPerson(root.left)
    printPerson(root.right)

root = Node()
root.data.append("L")

for i in range(0,6):
    addPerson(root, input("Add Person: "))

print("Left side of room A-K: ")
printPerson(root.left)
print("Right side of room L-Z: ")
printPerson(root.right)
类节点:
定义初始(自我):
self.left=无
self.right=无
self.data=list()
def addPerson(root,person):
如果人员root.data[0]:
如果root.right==无:
root.right=节点()
root.right.data.append(person)
其他:
addPerson(root.right,person)
其他:
root.data.append(个人)
def打印人员(root):
如果根=无:
返回
打印(root.data)
printPerson(root.left)
printPerson(root.right)
根=节点()
root.data.append(“L”)
对于范围(0,6)内的i:
addPerson(根,输入(“添加人员:”)
打印(“房间A-K的左侧:”)
printPerson(root.left)
打印(“房间L-Z的右侧:”)
printPerson(root.right)

初始化方法名称不正确,应该是
\uuuu init\uuu
。这些神奇的方法通常被称为“dunders”,因为它们以双下划线开始和结束。将
节点
类定义更改为:

类节点:
定义初始化(自):
self.left=无
self.right=无
self.data=list()

哇!真不敢相信我没听懂……python新手。C++和C是我的正常语言,但我在学校里使用Python课程。Thanks@ZacharyDaniels,这发生在我们最好的人身上。我使用一种字体,在相邻的下划线之间呈现一个空白像素,以避免将一个下划线误认为两个下划线。