# Coding Challenge Day-15: Problem 1: Search for element in Binary Search Tree

Hello! So I’m doing a 30 day coding challenge where I solve a few questions every day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 15: Problem 1! Be sure to post your answers, queries etc in the comments!

Problem: Search for element in Binary Search Tree

Sample:

Sample: Search element= 4
Output: Element found

Sample: Search element= 23

Solution:

``````package tree.binarysearchtree;

public class Program001searching_for_element {

static class Node {
int data;
Node left, right;

public Node (int d){
data=d;
left= right= null;
}
}

static class BST {
Node root;

public BST(){
root= null;
}

public void add_left(Node parent_node, Node node){
parent_node.left= node;
}

public void add_right (Node parent_node, Node node){
parent_node.right= node;
}

public Node search (Node node, int search){
if (node==null || node.data==search){
return node;
}

if( search<node.data){
return search (node.left, search);
}

return search(node.right, search);
}
}

public static void main (String [] args){
BST tree= new BST();

tree.root= new Node(8);

Node node6= new Node(6);

Node node10= new Node(10);

Node node4= new Node(4);

Node node7= new Node(7);

Node node9= new Node(9);

Node node11= new Node(11);

Node node3= new Node(3);

Node node5= new Node(5);

Node found= tree.search(tree.root, 10);

if(found!=null){
System.out.println("Element found");
}

else{