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
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 root and checks the root data which is not null and it executes the second if statement and check for the condition and check the root data wether it is equal to 45 or not. condition is false.
Then executes the else if condition to which returns true and set the current to its 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 false and executes the else statement which is true .
then set the current to right child of it. And begin the third iteration and checks the conditions of the first If which is false and checks the second if which is true and set return the 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.