views:

68

answers:

2

how to traverse a binary decision tree using python language. given a tree,i want know how can we travesre from root to required leaf the feature of the required leaf are given in an dictionary form assume and have to traverse from root to leaf answering the questions at each node with the details given in feature list.. the decision tree node has format ((question)(left tree)(right tree)) while traversing it should answer question at each node and an choose left or right and traverse till leaf?

+1  A: 
TheMachineCharmer
A: 

@TheMachineCharmer is right: recursivity is the keyword here!

I'd add to the nice function given by @TheMachineCharmer a little return (the trivial case, where answer is neither left nor right)

def walk(node):
    answer = ask(node.question)
    if answer == left:
        walk(node.left_tree)
    else:
        walk(node.right_tree)
    return answer

This way if the node contains THE real answer it will return it.

sandra