Find the node with minimum value in a Binary Search Tree
Last Updated :
28 Feb, 2024
Write a function to find the node with minimum value in a Binary Search Tree.
Example:Â
Input:Â
Â
first example BST
Output: 8
Input:Â
Â
second example BST
Output: 10Â
Brute Force Approach: To solve the problem follow the below idea:
The in-order traversal of a binary search tree always returns the value of nodes in sorted order. So the 1st value in the sorted vector will be the minimum value  which is the answer.
Below is the implementation of the above approach:
C++14
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode( int data)
{
struct node* node
= ( struct node*) malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
struct node* insert( struct node* node, int data)
{
if (node == NULL)
return (newNode(data));
else {
if (data <= node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
return node;
}
}
void inorder( struct node* node, vector< int >& sortedInorder)
{
if (node == NULL)
return ;
inorder(node->left, sortedInorder);
sortedInorder.push_back(node->data);
inorder(node->right, sortedInorder);
}
int main()
{
struct node* root = NULL;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 4);
insert(root, 5);
vector< int > sortedInorder;
inorder(
root,
sortedInorder);
printf ( "\n Minimum value in BST is %d" ,
sortedInorder[0]);
getchar ();
return 0;
}
|
Java
import java.util.*;
class Node {
int data;
Node left, right;
public Node( int data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
}
class Main {
public static Node insert(Node node, int data)
{
if (node == null ) {
return new Node(data);
}
else {
if (data <= node.data) {
node.left = insert(node.left, data);
}
else {
node.right = insert(node.right, data);
}
return node;
}
}
public static void inorder(Node node,
List<Integer> sortedInorder)
{
if (node == null ) {
return ;
}
inorder(node.left, sortedInorder);
sortedInorder.add(node.data);
inorder(node.right, sortedInorder);
}
public static void main(String[] args)
{
Node root = null ;
root = insert(root, 4 );
insert(root, 2 );
insert(root, 1 );
insert(root, 3 );
insert(root, 6 );
insert(root, 4 );
insert(root, 5 );
List<Integer> sortedInorder
= new ArrayList<Integer>();
inorder(root, sortedInorder);
System.out.printf( "\n Minimum value in BST is %d" ,
sortedInorder.get( 0 ));
}
}
|
Python3
from typing import List
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def insert(node: Node, data: int ) - > Node:
if not node:
return Node(data)
if data < = node.data:
node.left = insert(node.left, data)
else :
node.right = insert(node.right, data)
return node
def inorder(node: Node, sorted_inorder: List [ int ]) - > None :
if not node:
return
inorder(node.left, sorted_inorder)
sorted_inorder.append(node.data)
inorder(node.right, sorted_inorder)
if __name__ = = '__main__' :
root = None
root = insert(root, 4 )
insert(root, 2 )
insert(root, 1 )
insert(root, 3 )
insert(root, 6 )
insert(root, 4 )
insert(root, 5 )
sorted_inorder = []
inorder(root, sorted_inorder)
print (f "Minimum value in BST is {sorted_inorder[0]}" )
|
C#
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
}
class MainClass {
public static Node insert(Node node, int data)
{
if (node == null ) {
return new Node(data);
}
else {
if (data <= node.data) {
node.left = insert(node.left, data);
}
else {
node.right = insert(node.right, data);
}
return node;
}
}
public static void inorder(Node node,
List< int > sortedInorder)
{
if (node == null ) {
return ;
}
inorder(node.left, sortedInorder);
sortedInorder.Add(node.data);
inorder(node.right, sortedInorder);
}
public static void Main( string [] args)
{
Node root = null ;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 4);
insert(root, 5);
List< int > sortedInorder = new List< int >();
inorder(root, sortedInorder);
Console.WriteLine( "\n Minimum value in BST is {0}" ,
sortedInorder[0]);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
function newNode(data) {
return new Node(data);
}
function insert(node, data) {
if (node == null ) return newNode(data);
else {
if (data <= node.data) node.left = insert(node.left, data);
else node.right = insert(node.right, data);
return node;
}
}
function inorder(node, sortedInorder) {
if (node == null ) return ;
inorder(node.left, sortedInorder);
sortedInorder.push(node.data);
inorder(node.right, sortedInorder);
}
let root = null ;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 4);
insert(root, 5);
let sortedInorder = [];
inorder(root, sortedInorder);
console.log( "Minimum value in BST is " + sortedInorder[0]);
|
Output
Minimum value in BST is 1
Time Complexity: O(n) where n is the number of nodes.
Auxiliary Space: O(n) (for recursive stack space + vector used additionally)
Efficient Approach: To solve the problem follow the below idea:
This is quite simple. Just traverse the node from root to left recursively until left is NULL. The node whose left is NULL is the node with minimum value
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode( int data)
{
struct node* node
= ( struct node*) malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
struct node* insert( struct node* node, int data)
{
if (node == NULL)
return (newNode(data));
else {
if (data <= node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
return node;
}
}
int minValue( struct node* node)
{
struct node* current = node;
while (current->left != NULL) {
current = current->left;
}
return (current->data);
}
int main()
{
struct node* root = NULL;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);
cout << "\n Minimum value in BST is " << minValue(root);
getchar ();
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode( int data)
{
struct node* node
= ( struct node*) malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
struct node* insert( struct node* node, int data)
{
if (node == NULL)
return (newNode(data));
else {
if (data <= node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
return node;
}
}
int minValue( struct node* node)
{
struct node* current = node;
while (current->left != NULL) {
current = current->left;
}
return (current->data);
}
int main()
{
struct node* root = NULL;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);
printf ( "\n Minimum value in BST is %d" , minValue(root));
getchar ();
return 0;
}
|
Java
class Node {
int data;
Node left, right;
Node( int d)
{
data = d;
left = right = null ;
}
}
class BinaryTree {
static Node head;
Node insert(Node node, int data)
{
if (node == null ) {
return ( new Node(data));
}
else {
if (data <= node.data) {
node.left = insert(node.left, data);
}
else {
node.right = insert(node.right, data);
}
return node;
}
}
int minvalue(Node node)
{
Node current = node;
while (current.left != null ) {
current = current.left;
}
return (current.data);
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
Node root = null ;
root = tree.insert(root, 4 );
tree.insert(root, 2 );
tree.insert(root, 1 );
tree.insert(root, 3 );
tree.insert(root, 6 );
tree.insert(root, 5 );
System.out.println( "Minimum value of BST is "
+ tree.minvalue(root));
}
}
|
Python3
class Node:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def insert(node, data):
if node is None :
return (Node(data))
else :
if data < = node.data:
node.left = insert(node.left, data)
else :
node.right = insert(node.right, data)
return node
def minValue(node):
current = node
while (current.left is not None ):
current = current.left
return current.data
if __name__ = = '__main__' :
root = None
root = insert(root, 4 )
insert(root, 2 )
insert(root, 1 )
insert(root, 3 )
insert(root, 6 )
insert(root, 5 )
print ( "\nMinimum value in BST is %d" % (minValue(root)))
|
C#
using System;
public class Node {
public int data;
public Node left, right;
public Node( int d)
{
data = d;
left = right = null ;
}
}
public class BinaryTree {
public static Node head;
public virtual Node insert(Node node, int data)
{
if (node == null ) {
return ( new Node(data));
}
else {
if (data <= node.data) {
node.left = insert(node.left, data);
}
else {
node.right = insert(node.right, data);
}
return node;
}
}
public virtual int minvalue(Node node)
{
Node current = node;
while (current.left != null ) {
current = current.left;
}
return (current.data);
}
public static void Main( string [] args)
{
BinaryTree tree = new BinaryTree();
Node root = null ;
root = tree.insert(root, 4);
tree.insert(root, 2);
tree.insert(root, 1);
tree.insert(root, 3);
tree.insert(root, 6);
tree.insert(root, 5);
Console.WriteLine( "Minimum value of BST is "
+ tree.minvalue(root));
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
let head;
function insert(node, data) {
if (node == null ) {
return ( new Node(data));
} else {
if (data <= node.data) {
node.left = insert(node.left, data);
} else {
node.right = insert(node.right, data);
}
return node;
}
}
function minvalue(node) {
if (node === null ) return null ;
let current = node;
while (current.left != null ) {
current = current.left;
}
return (current.data);
}
let root = null ;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);
document.write( "Minimum value in BST is " + minvalue(root));
</script>
|
PHP
<?php
class node
{
private $node , $left , $right ;
function __construct( $node )
{
$this ->node = $node ;
$left = $right = NULL;
}
function set_left( $left )
{
$this ->left = $left ;
}
function set_right( $right )
{
$this ->right = $right ;
}
function get_left()
{
return $this ->left;
}
function get_right()
{
return $this ->right;
}
function get_node()
{
return $this ->node;
}
}
function get_minimum_value( $node )
{
while ( $node ->get_left() != NULL)
{
$node = $node ->get_left();
}
return $node ->get_node();
}
$node = new node(4);
$lnode = new node(2);
$lnode ->set_left( new node(1));
$lnode ->set_right( new node(3));
$rnode = new node(6);
$rnode ->set_left( new node(5));
$node ->set_left( $lnode );
$node ->set_right( $rnode );
$minimum_value = get_minimum_value( $node );
echo 'Minimum value of BST is ' .
$minimum_value ;
?>
|
Output
Minimum value in BST is 1
Time Complexity: O(h) where h is the height of Binary Search Tree
Auxiliary Space: O(1)
Please Login to comment...