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
#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 node with the given data and NULL left and right pointers. */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 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). */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 that tree. Note that the entire tree does not need to 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; } |
chevron_right
filter_none
Java
// Java program to find minimum value node in Binary Search Tree // A binary tree node class 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("The minimum value of BST is " + tree.minvalue(root)); } } // This code has been contributed by Mayank Jaiswal |
chevron_right
filter_none
Python
# Python program to find the node with minimum value in bst # A binary tree node class 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 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). """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 that tree. Note that the entire tree does not need to be searched. """def minValue(node): current = node # loop down to find the lefmost leaf while(current.left is not None): current = current.left return current.data # Driver program root = 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) |
chevron_right
filter_none
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.
References:
http://cslibrary.stanford.edu/110/BinaryTrees.html
Recommended Posts:
- Find the node with maximum value in a Binary Search Tree
- Search a node in Binary Tree
- Insert a node in Binary Search Tree Iteratively
- Postorder predecessor of a Node in Binary Search Tree
- Minimum swap required to convert binary tree to binary search tree
- Sum and Product of minimum and maximum element of Binary Search Tree
- Find mirror of a given node in Binary tree
- Find the Deepest Node in a Binary Tree
- Find n-th node in Preorder traversal of a Binary Tree
- Find the maximum node at a given level in a binary tree
- Find n-th node in Postorder traversal of a Binary Tree
- Find distance from root to given node in a binary tree
- Find the closest element in Binary Search Tree
- Find the closest element in Binary Search Tree | Space Efficient Method
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree



