def find_value ( _list, goal) :
first = 0
end = len ( _list) - 1
while first <= end:
mid = ( first + end) // 2
if _list[ mid] > goal:
end = mid - 1
elif _list[ mid] < goal:
first = mid + 1
else :
return mid
_list = [ 1 , 2 , 4 , 11 , 14 , 23 , 45 , 66 , 88 , 96 , 112 , 133 ]
goal = 2
s = "i AM a STUDENT"
c = s. split( " " )
print ( c)
c. reverse( )
print ( " " . join( c) )
class Tree :
def __init__ ( self, value= None , left= None , right= None ) :
self. left = left
self. right = right
self. value = value
def forward ( root) :
if root == None :
return
print ( root. value)
forward( root. left)
forward( root. right)
def middle ( root) :
if root == None :
return
middle( root. left)
print ( root. value)
middle( root. right)
def backward ( root) :
if root == None :
return
backward( root. left)
backward( root. right)
print ( root. value)
if __name__ == "__main__" :
root = Tree( 'D' , Tree( 'B' , Tree( "A" ) , Tree( "C" ) ) , Tree( "E" , right= Tree( "G" , Tree( "F" ) ) ) )
print ( "forward" )
forward( root)
print ( "\n" )
print ( "middle" )
middle( root)
print ( "\n" )
print ( "backward" )
backward( root)
Python实现二叉树的遍历