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

passing a leaf of binary tree to a function in python

我创建了一个class节点

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.left)' 或 'runTree(curNode.right)' 调用我的函数时,它不会访问我的根对象引用的叶,而是创建一个新对象。如何传递我的左叶或右叶并在函数中访问该对象?

对不起我对gelonida的误解

整个代码就是:

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)

OK这里修改解决方案我替换了我的旧答案。

请调整您的问题,以便未来的读者理解我的回答。

目前我回答你对自己问题的回答,这可能会让其他人感到困惑。

要点是,参数传递并不完全像您预期的那样工作。

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)