w_Claire_w 2022-11-29 19:02 采纳率: 100%
浏览 25
已结题

AttributeError: '……' object has no attribute '……'

代码
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'

求问应该如何解决

  • 写回答

2条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-11-30 10:02
    关注

    初始化函数少个 i
    是 init,你写成了 int

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月8日
  • 已采纳回答 11月30日
  • 创建了问题 11月29日

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件