Java Program to find the smallest element in a tree
The variable min is used to store the data of the root, which is initially defined. The smallest node in the left subtree is then located by moving through the subtree. It is compared to a min, and a minimum of two is stored in the variable min. Next, we search the current subtree for the smallest node, compare it to min, and move on. Variable Min will have the smallest node at the end of the traversal.
Algorithm
- Define Data; left and right is the node class's three characteristics. Left indicates the node's left child, and right here represents the node's right child.
- Data is passed to the node's data attribute at creation, and left, and right are both set to null.
- Define a different class with a root attribute.
- Root initializes to null and represents the tree's root node.
- The function smallestElement() will determine the binary tree's smallest node:
- If the root of the given tree is null, then it is assumed to be empty.
- Create a variable called min that will hold the data from temp if the tree is not empty.
- Recursively executing smallestElement() will reveal the smallest node in the left subtree. Put that number in leftMin. The minimum of two is stored in a min after comparing the min value with leftMin.
- Recursively invoking smallestElement() will reveal the smallest node in the right subtree. Put that number into rightMin. The maximum of two should be stored in a min after comparing the value of min with rightMin.
- The smallest binary tree node will be stored in min at the conclusion.
SmallestNodeInTree.java
// Main class declaration for finding the smallest number in the tree
public class SmallestNodeInTree {
// creating the Node for the tree using static keyword
// By Using static keyword, we can access the method without creating the object
public static class Node {
// storing the data
int data;
/* Left Node creation to store left node of the tree */
Node left;
/* Right Node creation to store the right node of the tree */
Node right;
/* Node parameterized constructor creation */
public Node(int data){
/* Set the left and right children to null and assign data to the new node */
/* this keyword is used to represent the present class */
this.data = data;
this.left = null;
this.right = null;
}
}
/* Node root represents the root of the binary tree */
public Node r;
public Node SmallestNode(){
r = null;
return r;
}
/* smallestElement() will identify the binary tree's smallest node. */
public int smallestElement(Node cur){
/* Verify if the tree is empty. */
if(r == null) {
System.out.println(" The Tree is empty" );
return 0 ;
}
else {
// creating two integer variables
int lMin, rMin;
// storing the node data into min variable
int min = cur.data;
// this if block is used to find the smallest Element in the left sub tree of the given node //
if(cur.left != null){
lMin = smallestElement(cur.left);
// Store the value of leftMin into min if min is greater than leftMin value.
min = Math.min(min, lMin);
}
// this if block is used to find the smallest Element in the left sub tree of the given node
if(cur.right != null){
rMin = smallestElement(cur.right);
// If min value is greater than rightMin value, then store the value of rightMin into min
min = Math. min(min, rMin);
}
return min;
}
}
public static void main (String[] args) {
SmallestNodeInTree t = new SmallestNodeInTree();
// Adding nodes to the binary tree by calling the parameterized constructor
// adding a root node
t.r = new Node(4);
// adding a left node to the tree
t.r.left = new Node(2);
// adding a right node to the tree
t.r.right = new Node(3);
// adding a left node to the left sub tree
t.r.left.left = new Node(9);
// adding a right node to the right sub tree
t.r.right.right = new Node(5);
// adding a left node to the right sub tree
t.r.right.left = new Node(6);
// Print the smallest node in the given binary tree
System.out.println("The Smallest element in the binary tree: " + t.smallestElement(t.r));
}
}
Output
The smallest element in the binary tree: 2