Java program to search a node in a Binary Tree17 Mar 2025 | 3 min read Trees are the non-linear data structure that stores data hierarchically. The tree is a collection of elements called nodes. Nodes are connected through edges and contain data. The first node of the tree is called Root. Each node may or may not have children node. The node which doesn't have any child node is called leaf. The binary tree is another kind of tree data structure in which each node can have at most two children. That is, each node in the binary tree will have data, left child and right child. ![]() Above diagram represents a binary tree in which 1 represent the root node of the tree. Node 2 has 4 as its left child and Node 3 has 5 as its left child and 6 as its right child. Nodes 4, 5 and 6 are leaf nodes as they don?t have any child node. Explanation:In this program, we will search a particular value in the binary tree. If it is present, print the message "Element is present in the binary tree" else print the message "Element is not present in the binary tree". In a nutshell, we will first compare data of root with data of node to be searched. If the match is found, set the flag to true. Else, search the node in left subtree and then in the right subtree. Algorithm
a. searchNode() will search for a particular node in the binary tree:
Program:Output: Element is present in the binary tree. Next TopicJava Programs |
In this program, our task is to count the total number of vowels and consonants present in the given string. As we know that, the characters a, e, i, o, u are known as vowels in the English alphabet. Any character other than that is known...
3 min read
In this program, we need to find out the total number of binary search trees can be constructed with n values. Below diagram shows a possible binary search tree with the key value as 3. So, we can construct a total of five binary search...
3 min read
Java Convert int to double We can convert int to double in java using assignment operator. There is nothing to do extra because lower type can be converted to higher type implicitly. It is also known as implicit type casting or type promotion. Java int to double Example Let's...
1 min read
. In this program, we need to find the smallest and the largest word present in the string: Hardships often prepare ordinary people for an extraordinary destiny Consider above example in which 'an' is the smallest word and 'extraordinary' is the largest word. One of the approach to...
4 min read
In this program, our task is to remove all the white-spaces from the string. For this purpose, we need to traverse the string and check if any character of the string is matched with a white-space character or not. If so, use any built-in method...
2 min read
In this program, we will create a singly linked list and add a new node at the middle of the list. To accomplish this task, we will calculate the size of the list and divide it by 2 to get the mid-point of the list...
7 min read
It is very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to...
2 min read
? In Java programming, we often required to generate random numbers while we develop applications. Many applications have the feature to generate numbers randomly, such as to verify the user many applications use the OTP. The best example of random numbers is dice. Because when we throw...
7 min read
Program to find the frequency of each element in the array In this program, we have an array of elements to count the occurrence of its each element. One of the approaches to resolve this problem is to maintain one array to store the counts of...
3 min read
The singly linked list is a linear data structure in which each element of the list contains a pointer which points to the element in the list. Each element in the singly linked list is called a node. Each node has two components: data...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India