# EasyAlgo#2_BinarySearchTree-finding a node.

# lets create a function to search a node…

This function takes in a parameter which we intended to search.

by creating an endless loop we can do the finding process until we find the correct node

*while(true){*

*// body*

*}*

we set ** current **to the root at the beginning so it starts matching the entered parameter by checking it from the root by itself.

if we found the data item which we input. it returns the ** current** node which is already pointed to.

Let’s call this function as findNode(35); which is the worst case.

at the first iteration ** current** is in the

**and checks the**

*root***which is**

*root data***and it executes the second if statement and check for the condition and check the root data wether it is equal to**

*not null***or not. condition is**

*45***.**

*false*Then executes the ** else if **condition to which returns

**and set the**

*true***to its**

*current*

*left*

*child*then the second iteration occurs and check the first two ** if** conditions which are

**.**

*false*and jumps to else ** if** which is also

**and executes the**

*false***statement which is**

*else***.**

*true*then set the ** current** to

**of it. And begin the third iteration and checks the conditions of the first**

*right child***which is false and checks the second**

*If***which is**

*if***and set return the**

*true***.**

*current node*you might be wondering what is the job of the first if condition which is not useful — — — — — — — — — → so far its not useful but let’s consider our passed value to the findNode is 37 which is not in the tree.

in the above last step it cant find the data so it checks the if conditions and set the current to a null node by assuming it is in the right side of the node which holds the value 35 and call while to iterate again. there only the first if condition plays its role and return null since the 37 is not in the binary tree.