Find the node with minimum value in a Binary Search Tree

20 Jun, 2022
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.  
 
For the above tree, we start with 20, then we move left 8, we keep on moving to left until we see NULL. Since left of 4 is NULL, 4 is the node with minimum value.  
C++
C
Java
Python3
C#
PHP
Javascript
//C++ program to find minimum value node in binary search Tree.#include <bits/stdc++.h>using namespace std; /* A binary tree node has data, pointer to left childand a pointer to right child */struct node{    int data;    struct node* left;    struct node* right;}; /* Helper function that allocates a new nodewith the given data and NULL left and rightpointers. */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);} /* Give a binary search tree and a number,inserts a new node with the given number inthe correct place in the tree. Returns the newroot pointer which the caller should then use(the standard trick to avoid using referenceparameters). */struct node* insert(struct node* node, int data){/* 1. If the tree is empty, return a new,    single node */if (node == NULL)    return(newNode(data));else{    /* 2. Otherwise, recur down the tree */    if (data <= node->data)        node->left = insert(node->left, data);    else        node->right = insert(node->right, data);     /* return the (unchanged) node pointer */    return node;}} /* Given a non-empty binary search tree,return the minimum data value found in thattree. Note that the entire tree does not needto be searched. */int minValue(struct node* node){struct node* current = node; /* loop down to find the leftmost leaf */while (current->left != NULL){    current = current->left;}return(current->data);} /* Driver Code*/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;} // This code is contributed by Mukul Singh.
#include <stdio.h>#include<stdlib.h> /* A binary tree node has data, pointer to left child   and a pointer to right child */struct node{    int data;    struct node* left;    struct node* right;}; /* Helper function that allocates a new nodewith the given data and NULL left and rightpointers. */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);} /* Give a binary search tree and a number,inserts a new node with the given number inthe correct place in the tree. Returns the newroot pointer which the caller should then use(the standard trick to avoid using referenceparameters). */struct node* insert(struct node* node, int data){  /* 1. If the tree is empty, return a new,          single node */  if (node == NULL)    return(newNode(data));   else  {    /* 2. Otherwise, recur down the tree */    if (data <= node->data)        node->left  = insert(node->left, data);    else        node->right = insert(node->right, data);       /* return the (unchanged) node pointer */    return node;  }} /* Given a non-empty binary search tree, return the minimum data value found in thattree. Note that the entire tree does not needto be searched. */int minValue(struct node* node) {  struct node* current = node;   /* loop down to find the leftmost leaf */  while (current->left != NULL) {    current = current->left;  }  return(current->data);} /* Driver program to test sameTree function*/   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 program to find minimum value node in Binary Search Tree // A binary tree nodeclass Node {     int data;    Node left, right;     Node(int d) {        data = d;        left = right = null;    }} class BinaryTree {     static Node head;         /* Given a binary search tree and a number,     inserts a new node with the given number in     the correct place in the tree. Returns the new     root pointer which the caller should then use     (the standard trick to avoid using reference     parameters). */    Node insert(Node node, int data) {                 /* 1. If the tree is empty, return a new,             single node */        if (node == null) {            return (new Node(data));        } else {                         /* 2. Otherwise, recur down the tree */            if (data <= node.data) {                node.left = insert(node.left, data);            } else {                node.right = insert(node.right, data);            }             /* return the (unchanged) node pointer */            return node;        }    }     /* Given a non-empty binary search tree,      return the minimum data value found in that     tree. Note that the entire tree does not need     to be searched. */    int minvalue(Node node) {        Node current = node;         /* loop down to find the leftmost leaf */        while (current.left != null) {            current = current.left;        }        return (current.data);    }         // Driver program to test above functions    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));    }} // This code is contributed by Mayank Jaiswal
# Python program to find the node with minimum value in bst # A binary tree nodeclass Node:     # Constructor to create a new node    def __init__(self, key):        self.data = key        self.left = None        self.right = None """ Give a binary search tree and a number,inserts a new node with the given number inthe correct place in the tree. Returns the newroot pointer which the caller should then use(the standard trick to avoid using referenceparameters). """def insert(node, data):     # 1. If the tree is empty, return a new,    # single node    if node is None:        return (Node(data))     else:        # 2. Otherwise, recur down the tree        if data <= node.data:            node.left = insert(node.left, data)        else:            node.right = insert(node.right, data)         # Return the (unchanged) node pointer        return node """ Given a non-empty binary search tree, return the minimum data value found in thattree. Note that the entire tree does not needto be searched. """def minValue(node):    current = node     # loop down to find the leftmost leaf    while(current.left is not None):        current = current.left     return current.data # Driver programroot = Noneroot = 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))) # This code is contributed by Nikhil Kumar Singh(nickzuck_007)
using System; // C# program to find minimum value node in Binary Search Tree // A binary tree nodepublic 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;     /* Given a binary search tree and a number,      inserts a new node with the given number in      the correct place in the tree. Returns the new      root pointer which the caller should then use      (the standard trick to avoid using reference      parameters). */    public virtual Node insert(Node node, int data)    {         /* 1. If the tree is empty, return a new,              single node */        if (node == null)        {            return (new Node(data));        }        else        {             /* 2. Otherwise, recur down the tree */            if (data <= node.data)            {                node.left = insert(node.left, data);            }            else            {                node.right = insert(node.right, data);            }             /* return the (unchanged) node pointer */            return node;        }    }     /* Given a non-empty binary search tree,       return the minimum data value found in that      tree. Note that the entire tree does not need      to be searched. */    public virtual int minvalue(Node node)    {        Node current = node;         /* loop down to find the leftmost leaf */        while (current.left != null)        {            current = current.left;        }        return (current.data);    }     // Driver program to test above functions    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));    }}   // This code is contributed by Shrikant13
<?php// PHP program to find the node with// minimum value in bst // create a binary treeclass node{    private $node, $left, $right;    function __construct($node)    {        $this->node = $node;        $left = $right = NULL;    }     // set the left node in tree    function set_left($left)    {        $this->left = $left;    }     // set the right node in tree    function set_right($right)    {        $this->right = $right;    }     // get left node    function get_left()    {        return $this->left;    }     // get right node    function get_right()    {        return $this->right;    }     // get value of current node    function get_node()    {        return $this->node;    }     } // Find the node with minimum value// in a Binary Search Treefunction get_minimum_value($node){    /*travel till last left node to      get the minimum value*/    while ($node->get_left() != NULL)    {        $node = $node->get_left();    }    return $node->get_node();} // code to creating a tree$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; // This code is contributed// by Deepika Pathak?>
<script>     // JavaScript program to find minimum    // value node in Binary Search Tree         class Node    {        constructor(data) {           this.left = null;           this.right = null;           this.data = data;        }    }         let head;           /* Given a binary search tree and a number,     inserts a new node with the given number in     the correct place in the tree. Returns the new     root pointer which the caller should then use     (the standard trick to avoid using reference     parameters). */    function insert(node, data) {                   /* 1. If the tree is empty, return a new,             single node */        if (node == null) {            return (new Node(data));        } else {                           /* 2. Otherwise, recur down the tree */            if (data <= node.data) {                node.left = insert(node.left, data);            } else {                node.right = insert(node.right, data);            }               /* return the (unchanged) node pointer */            return node;        }    }       /* Given a non-empty binary search tree,      return the minimum data value found in that     tree. Note that the entire tree does not need     to be searched. */    function minvalue(node) {        if (node === null) return null;        let current = node;           /* loop down to find the leftmost leaf */        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>
Output:
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Minimum value in BST is 1
Time Complexity: O(n) Worst case happens for left skewed trees.Similarly we can get the maximum value by recursively traversing the right node of a binary search tree.
Space complexity: O(n) for stack space, since using recursion 
Find the node with minimum value in a Binary Search Tree | GeeksforGeeks - YouTubeGeeksforGeeks531K subscribersFind the node with minimum value in a Binary Search Tree | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:37•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=GgDwIm1TlvE" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
References: http://cslibrary.stanford.edu/110/BinaryTrees.html 
shrikanth13
DeepikaPathak
Code_Mech
rathbhupendra
rameshtravel07
amartyaghoshgfg
simmytarika5
karthikshindee
polymatir3j
Microsoft
Binary Search Tree
Tree
Microsoft
Binary Search Tree
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.