打印一个分析树:AttributeError:'NoneType'对象没有属性'data'

时间:2020-04-20 13:52:45

标签: python python-3.x oop parsing tree

该类应基于后缀字符串生成一个解析树,然后在将打印该解析树的根节点上递归调用打印函数,但我不断收到以下错误: 第15行,在infixPrintNode中 self.right.infixPrintNode() AttributeError:“ NoneType”对象没有属性“ infixPrintNode”

我是python的新手,我相信该错误与实例有关,我真的很困惑,希望您澄清一下

    import operator


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

    def __str__(self):
        return self.data

    def infixPrintNode(self):

        if self:
            if self.right:
                return self.right.infixPrintNode()
            print(self.data)
            if self.left:
                return self.left.infixPrintNode()






class ParseTree:

    def __init__(self, root = None):
        self.root = (root)
    def __str__(self):
         pass # to be implemented

    def fromPostfix(self, expression=""):

        s=[] #maintain parents


        for c in expression.split():
             if c in ["+", "-", "*", "/", "^", "#"]:
                 n=Node(c)

                 n.right = s.pop()
                 n.left=s.pop()
                 s.append(n)

             elif c.isnumeric() or c.isalpha() :
                  n=Node(c)
                  s.append(n)

             else:

                     raise ValueError("Invalid token! ")

        self.root = (s.pop())
        print(self.root.left.data)
        print(self.root.data)
        print(self.root.right.data)
        return self.root #or  Node(s.pop())


    def printinfix(self):


        if self.root:
            self.root.infixPrintNode()


t=ParseTree("x y +")
t.fromPostfix("x y +")
t.printinfix()

1 个答案:

答案 0 :(得分:0)

在这里:

•   Both accounts can’t be in a pending or changing state, and the latest version of the agreements in the Agreements, Tax, and Banking section must be accepted.
•   No version of the app can use an iCloud entitlement.
•   No version of the app can use a Passbook entitlement.
•   The App must have had at least one version that has been released to the App Store.
•   In-app purchase product IDs on the app can’t be the same as product IDs on any apps in the recipient’s account.
•   TestFlight beta testing should be turned off for all beta versions of the app.
•   Sandboxed Mac apps that share the Application Group Container Directory with other Mac apps cannot be transferred.

您仅传递了数据,因此左和右仍然为None。

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

所以您得到了:

 n=Node(c)