关于二叉树的查找节点的融合

时间:2016-10-08 04:48:53

标签: python binary-search-tree

我用python代码构建了一个二叉树,现在我可以用testTree.printInorder(testTree.root)按顺序打印它。我试图查找某个节点,函数findNode不再起作用了。 print testTree.findNode(testTree.root,20)无论我输入什么,只需返回无。

class TreeNode:
    def __init__(self, value):
        self.left = None;
        self.right = None;
        self.data = value;


class Tree:
    def __init__(self):
        self.root = None
    def addNode(self,node,value):
        if node == None:
            self.root = TreeNode(value)
        else:
            if value < node.data:
                if node.left == None:
                    node.left = TreeNode(value)
                else:
                    self.addNode(node.left,value)
            else:
                if node.right == None:
                    node.right = TreeNode(value)
                else:
                    self.addNode(node.right,value)

    def printInorder(self,node):
        if node != None:
            self.printInorder(node.left)
            print node.data
            self.printInorder(node.right)

    def findNode(self,node,value):
        if self.root != None:
            if value == node.data:
                return node.data
            elif value < node.data and node.left != None:
                self.findNode(node.left,value)
            elif value > node.data and node.right != None:
                self.findNode(node.right,value)
        else:
            return None
testTree = Tree()
testTree.addNode(testTree.root, 200)
testTree.addNode(testTree.root, 300)
testTree.addNode(testTree.root, 100)
testTree.addNode(testTree.root, 30)
testTree.addNode(testTree.root, 20)
#testTree.printInorder(testTree.root)
print testTree.findNode(testTree.root,20)

2 个答案:

答案 0 :(得分:2)

当您递归到LSApplicationQueriesSchemes中的孩子时,您需要返回结果,否则该函数将隐式返回findNode

None

答案 1 :(得分:2)

任何没有显式返回的函数都将返回None。

您尚未在findNode内返回递归调用。所以在这里。

if value == node.data:
    return node.data
elif value < node.data and node.left != None:
    return self.findNode(node.left,value)
elif value > node.data and node.right != None:
    return self.findNode(node.right,value)

现在,我不禁想到这有点吵。你总是从根开始添加,是吗?

testTree.addNode(testTree.root, 200)

你宁愿这样做

testTree.addNode(200)

要做到这一点,你基本上是在TreeNode类上实现你的方法。所以,对于addNode

您也可以从递归中“返回”,而不是将节点“传递”为参数。

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

    def addNode(self,value):
        if self.data == None:  # Ideally, should never end-up here
            self.data = value
        else:
            if value < self.data:
                if self.left == None:
                    self.left = TreeNode(value)
                else:
                    self.left = self.left.addNode(value)
            else:
                if self.right == None:
                    self.right = TreeNode(value)
                else:
                    self.right = self.right.addNode(value)

        return self # Return back up the recursion

然后,在Tree课程中,只需将addNode职责委托给根

class Tree:
    def __init__(self):
        self.root = None

    def addNode(self,value):
        if self.root == None:
            self.root = TreeNode(value)
        else:
            self.root = self.root.addNode(value)