Find in BST
by Isai Damier, Android Engineer @ Google

  #=====================================================================
  # Author: Isai Damier
  # Title: Find
  # Project: geekviewpoint
  # Package: algorithms
  #
  # Time Complexity of Solution:
  #   Best = const; Average = O(log(n)); Worst = O(n).
  #
  #=====================================================================
 
 
class BST( object ):

  def __init__( self ):
      self.root = None


  def getRoot( self ):
    return self.root

  def find( self, el ):
    n = self.root
    while n is not None and el != n.data:
      if el > n.data:
        n = n.right
      else:
        n = n.left

    return n
import unittest
from algorithms.BST import BST
from cStringIO import StringIO
import sys
class Test( unittest.TestCase ):
  #===================================================================
  # Test of find method, of class BST.
  #===================================================================
  def testFind( self ):
    bst = BST()
    el = [7, 9, 2, 4, 6, 7, 10, 1]
    self.assertIsNone( bst.find( el[0] ) )
    for i in el:
      bst.add( i )
    self.assertIsNotNone( bst.find( el[0] ) )