Tuesday, August 18, 2009

Given a binary search tree and a "target" value, search the tree to see if it contains the target.


static int lookup(struct node* node, int target) { // 1. Base case == empty tree // in that case, the target is not found so return false if (node == NULL) { return(false); } else { // 2. see if found here if (target == node->data) return(true); else { // 3. otherwise recur down the correct subtree if (target <>data) return(lookup(node->left, target)); else return(lookup(node->right, target)); } }}

No comments: