# Data Structures - Check if an Element is Present in a Binary Search Tree

Tell us what’s happening:
Describe your issue in detail here.

Please somebody help me here, I am facing problem with return and recurrsive function using together for a long time now; How effectively and clean I can use with my following program and my programming journey.
here in this example code, even if I found the element and return true, the previous recursive function will undermine the value and return different value.

``````var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
}
function BinarySearchTree() {
this.root = null;
// Only change code below this line
let node = new Node(ele);
if(!this.root){
this.root = node;
}
else {
this.insert(this.root,node)
}

};
this.insert = function(root,newNode){
if(root==newNode){
return null;
}
else if(newNode.value < root.value){
if(!root.left) {
root.left = newNode;
} else {
this.insert(root.left,newNode);
}
}
else if(newNode.value > root.value){
if(!root.right) {
root.right = newNode;
} else {
this.insert(root.right,newNode);
}
}

}

this.isPresent = function(ele) {
if(!this.root){
return null;
}
else if(ele == this.root.value) {
return true;
}
else if(ele<this.root.value) {
return this.find(this.root.left,ele);
}

}

this.find = function(root,e) {
if(!root) {
return null;
}

if (e == root.left.value) {
return true;
}
else if(e < root.left.value){
this.find(root.left, e);
}
}
// Only change code above this line
}

let bst = new BinarySearchTree();
console.log(bst.root);
console.log(bst.isPresent(3))
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36`

Challenge: Data Structures - Check if an Element is Present in a Binary Search Tree

Could you explain what exactly is the issue? What is the expected behavior, what happens instead?

I have created a binary search tree with this element 20, 10 , 5 and 3. While I run the function isPresent(3), it find the element 3 and return true but this return value is overridden by return value of 5 because I am using recursive function, hence the output is ‘undefined’ instead of ‘true’.

I have problem with implementation of recursive function and return value.

`undefined` can mean that recursive function at some point is not returning anything explicitly. Result of the intermediate recursive calls needs to be returned, otherwise results of deeper recursive call will be lost.

Hi, can you please explain with this small code example. I am getting nowhere with your previous reply.

``````function find(arr,item){
if(arr.length-1 < 0) {
return false;
}
else if( item == arr[0]) {
return true;
}
else {
find(arr.slice(1),item)
}
}

let array = [5,2,1,4,3];
console.log(find(array,1));
``````

What function returns when recursive call is needed to look for item?

Same = “undefined”

I wrote the small find function to replicate my recursive and return problem.

Sorry, I meant to ask which part of code is returning the result of recursive calls.

no problem,

there is two return statement, one is if the array element become empty which means the item is not found, hence return “false”. Another return statement is when the item is found. Here is the code below:

``````if(arr.length-1 < 0) {
}
else if( item == arr[0]) {
return true; //return when element is found
}
``````

That’s the part returning from recursive call, what about returning the result of recursive call from the call one level higher?

Consider what happens for the example from your code:

``````let array = [5,2,1,4,3];
find([5,2,1,4,3], 1)
// Item is not at the index `0`, array is not empty,
// recursive call follows
find([2,1,4,3], 1)
// Item is not at the index `0`, array is not empty,
// recursive call follows
find([1,4,3], 1)
// Item is at the index `0`, `true` is returned
find([1,4,3], 1) -> true;

// Going back through recursive calls

// result of recursive call is not returned, function doesn't
// return anything explicitly, implicitly `undefined` is returned
find([2,1,4,3], 1) -> undefined

// result of recursive call is not returned, function doesn't
// return anything explicitly, implicitly `undefined` is returned
find([5,2,1,4,3], 1) -> undefined
``````

Thanks sanity, I got your point now. I am not able to figure it out how to explicitly return it and also how the below function able to return the value from implicit only.

``````function isEven(number) {
if (number % 2 === 0) {
return true;
} else {
return false;
}
};

console.log(isEven(2));
``````

I don’t see any difference between find and isEven function except one is recursive and one is normal function. And I am not able to see how a normal function explicitly returning a correct value.

Looking again at the `find` function

``````function find(arr,item){
if(arr.length-1 < 0) {
return false;
}
else if( item == arr[0]) {
return true;
}
else {
find(arr.slice(1),item)
}
}
``````

You can think about it as three cases:

• empty array - return `false`
• item isn’t - return `true`
• recursive call - result of the call should be returned

Two first cases are basically the same for `isEven`, but the third one is what makes `find` recursive and makes the difference.
However how result of function call can be used is basically the same. Imagine, that `isEven` is supposed to be used in another function, ie.:

``````function evenBetween(start, stop) {
for (let i = start; i <= stop; i++) {
isEven(i)
}
}
``````

Right now the `isEven(i)` is called, but result of it isn’t used for anything. If we’d want to print even numbers it could be something like:

``````function evenBetween(start, stop) {
for (let i = start; i <= stop; i++) {
if (isEven(i)) {
console.log(i);
}
}
}
``````

Here the result of `isEven(i)` is used. `find` doesn’t need to use the result in some additional conditional, but the idea is similar.

sanity thanks alot for taking your valuable time and explaining me alot about explicit and implicit return with recursive function.
I have kind of understand the work around but not confident about it and its internal working yet. Maybe it will take some more reading to observe fully.

Here I solve the problem with returning the recursive function which you are trying to guide me from the beginning.

`````` return find(arr.slice(1),item);
``````

thanks alot again for all your words…

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.