在python中将二叉树的叶子传递给函数

2024-04-19 12:44:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建一个类节点

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

和一个函数

def runTree(curNode):
    if (curNode is not None):
        if(curNode.isQuestion == True):
            print("\n\t")
            print(curNode.data)
            answer = input("\nEnter your input(1-yes, 0-no):\n")
            if(answer == '1'):
                runTree(curNode.right)
            elif(answer == '0'):
                runTree(curNode.left)

        else:
                print("\n\t")
                print(curNode.data)

当我用“runTree”调用函数时(curNode.左)'或'runTree'(curNode.右)'它不访问我的根对象引用的叶,而是创建一个新对象。如何传递左叶或右叶并在函数中访问该对象?你知道吗


Tags: 对象函数answerselfrightnoneinputdata
2条回答

对不起,我误解了盖洛尼达

整个代码是:

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

root = Node("has wings?", True)

def runTree(curNode):
    if (curNode is not None):
        print(root.right)
        print(curNode)
        if(curNode.isQuestion == True):
            print("\n\t")
            print(curNode.data)
            answer = input("\nEnter your input(1-yes, 0-no):\n")
            if(answer == '1'):
                runTree(curNode.right)
            elif(answer == '0'):
                runTree(curNode.left)

        else:
                print("\n\t")
                print(curNode.data)

    else:
        print("\n###########################\nERROR: node isn't a question and not has a label yet.\n###########################\n")
        answer = input("Want to put a question(1) or a label(0)?\n")
        if(answer == '1'):
            question = input("write your question\n")
            curNode = Node(question, True)
            print(curNode)
            print(root)
        elif(answer == '0'):
            label = input("write your label\n")
            curNode = Node(label, False)
            print(curNode)
            print(root)
        print("\n\nrunning the decisionTree again\n###########################\n")
        runTree(root)

runTree(root)

好的,这里修改了我的答案。你知道吗

请修改你的问题,以便将来的读者理解我的回答。你知道吗

现在我要回答你对自己问题的回答,这可能会让别人感到困惑。你知道吗

主要的一点是,参数传递并不像您期望的那样工作。你知道吗

Python有点特别,有一些关于这方面的好文章。只是不记得链接。你知道吗

下面的代码在调用runTree()之前创建一个空节点。 然后runTree()可以更改对象并添加问题或答案。你知道吗

# create a sentinel object to mark if Node is created without data
EMPTY = object()

class Node(object):
    def __init__(self, data=EMPTY, isQuestion=False, left=None, right=None):
        self.left = left
        self.right = right
        self.data = data
        self.isQuestion = isQuestion

    def __repr__(self):
        return ( "Node(id=%s, data=%s, isq=%s, left=%s, right=%s)"
            % (id(self),
               self.data if self.data is not EMPTY else "empty",
               self.isQuestion,
               id(self.left) if self.left else None,
               id(self.right) if self.right else None))

root = Node("has wings?", True)

def runTree(curNode):
    print(curNode)
    # Node is never None, check only whether data has been filled in
    if (curNode.data is not EMPTY):
        if(curNode.isQuestion == True):
            print("\n\t")
            print(curNode.data)
            answer = input("\nEnter your input(1-yes, 0-no):\n")
            if(answer == '1'):
                if curNode.right is None:  # create child node if not existing
                    curNode.right = Node()
                runTree(curNode.right)
            elif(answer == '0'):
                if curNode.left is None: # create child node if not existing
                    curNode.left = Node()
                runTree(curNode.left)

        else:
                print("\n\t")
                print(curNode.data)

    else:
        print("\n###########################\nERROR: node isn't a question and not has a label yet.\n###########################\n")
        answer = input("Want to put a question(1) or a label(0)?\n")
        if(answer == '1'):
            question = input("write your question\n")
            # you could of course use method Node.change_node() instead if you want
            curNode.data = question
            curNode.isQuestion = True
            print(curNode)
            print(root)
        elif(answer == '0'):
            label = input("write your label\n")
            # you could of course use method Node.change_node() instead if you want
            curNode.data = label
            curNode.isQuestion = False
            print(curNode)
        print("\n\nrunning the decisionTree again\n###########################\n")
        print(root)

        # This creates a recursion step for every time you enter a new question
        # if you enter hundreds / thousands of questions you would encounter a python
        # error for exceeding the recursion depth.
        # But let's address this in another question or at elast with another answer
        runTree(root)

runTree(root)

相关问题 更多 >