Cofusing about lookup node with binary tree
<p>I build a binary tree with python code, now I could print it in order with <code>testTree.printInorder(testTree.root)</code>. I have tried to lookup some node ,and the function <code>findNode</code> doesn't work anymore . <code>print testTree.findNode(testTree.root,20)</code> whatever I put in just return None.</p>

<pre><code>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 &lt; 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 &lt; node.data and node.left != None:
                self.findNode(node.left,value)
            elif value &gt; 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)
</code></pre>

<p>Any function without an explicit return will return None. </p>

<p>You have not returned the recursive calls within <code>findNode</code>. So, here.  </p>

<pre><code>if value == node.data:
    return node.data
elif value &lt; node.data and node.left != None:
    return self.findNode(node.left,value)
elif value &gt; node.data and node.right != None:
    return self.findNode(node.right,value)
</code></pre>

<hr>

<p>Now, I can't help but thinking this is a bit noisy. You'll always start adding from the root, yes?</p>

<pre><code>testTree.addNode(testTree.root, 200)
</code></pre>

<p>You could rather do this </p>

<pre><code>testTree.addNode(200)
</code></pre>

<p>And to do that, you basically implement your methods on the <code>TreeNode</code> class instead. So, for the <code>addNode</code>. </p>

<p>You could also "return up" from the recursion, rather than "pass down" the nodes as parameters. </p>

<pre><code>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 &lt; 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
</code></pre>

<p>Then, in the <code>Tree</code> class, just delegate the <code>addNode</code> responsibility to the root </p>

<pre><code>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)
</code></pre>

python binary-search-tree