代码
class TreeNode(object):
def __int__(self, elem = None, _lichild = None, _rchild = None):
self.elem = elem
self.lchild = _lichild
self.rchild = _rchild
class BTree(object):
def __int__(self, _root = None):
self.root = _root
self.NodeQueue = []
def add(self, elem):
NodeQueue = []
if self.root is None:
self.root = TreeNode(elem)
else:
NodeQueue.append(self.root)
while NodeQueue:
temp = NodeQueue.pop(0)
if temp.lchild == None:
temp.lchild = TreeNode(elem)
return
elif temp.rchild == None:
NodeQueue.append(temp.lchild)
temp.rchild = TreeNode(elem)
return
else:
NodeQueue.append(temp.lchild)
NodeQueue.append(temp.rchild)
return
if __name__ == "__main__":
tree = BTree()
tree.add(0)
运行结果及报错内容
Traceback (most recent call last):
File "xxx\BinaryTree\main.py", line 75, in
tree.add(0)
File "xxx\BinaryTree\main.py", line 19, in add
if self.root is None:
AttributeError: 'BTree' object has no attribute 'root'
求问应该如何解决