def find(node, data):
''' (BTNode, object) -> BTNode
Return the BTNode containing data, or else None.
>>> b = BTNode(5, BTNode(4))
>>> find(b, 7) is None
True
>>> find(b, 4)
BTNode(4, None, None)
'''
if not node or node.data == data:
return node
else:
return (find(node.left, data) if (data < node.data)
else find(node.right, data))
As code shows above, BST will search the specific value. If the value less than node, then it cuts right tree and only search left tree. Reversely, it establishes as well.
In addition, there are three approach of BST: preorder, postorder and inorder. These three orders has been tested on term test 2.

No comments:
Post a Comment