andrealorenzon / AP_exam

Binary Tree implementation
1 stars 2 forks source link

find function #15

Open andrealorenzon opened 5 years ago

andrealorenzon commented 5 years ago

Accepts a key as input

Returns a shared_ptr to the searched key Node if the key is present in the tree, Returns nullpointer otherwise

andrealorenzon commented 5 years ago
Node search(Node root, T key) 
{ 
    // Base Cases: root is null or key is present at root 
    if (root == NULL || root->key == key) 
       return root; 

    // Key is greater than root's key 
    if (root->key < key) 
       return search(root->right, key); 

    // Key is smaller than root's key 
    return search(root->left, key); 
}