Program to find minimum element in BST (Binary Search Tree)

 Here we are going to write a program to find minimum element in bst (binary search tree). Below you will find this code in c and c++. This program is very simple. If you remember, in a bst, for each node, all the elements in left subtree is less than the root node and all the elements in right subtree is greater than the root node. So we do not have to check right subtree at all as we are not going to get our minimum element from that part. We only need to check left subtree. Again, in a bst, for each node, all the elements in left subtree is less than the root node so the minimum element will be the last one. Here's an example

Example                              5

                                         /        \

                                      4            6

                                    /    \            \

                                 3       2           7

                               /

                            1

Output - 1


Similarly, if you want to find the maximum element in bst then you know that in a bst, for each node, all the elements in right subtree is greater than the root node.  So we do not have to check left subtree at all as we are not going to get our maximum element from that part. 

Check this Program - Program to find maximum element in BST (Binary Search Tree)

We will do this program i.e., Minimum element in BST using iterative way 

Program to find minimum element in BST (Binary Search Tree) in C

#include <stdio.h>
using namespace std;

struct node{
    int data;
    node *left;
    node *right;
};

struct node* create(int data)
{
    struct node* node = (struct node*)malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return (node);
}

node* buildTree(node* root, int data)
{
    if (root == NULL)
        return (create(data));
    else {

        if (data <= root->data)
            root->left = buildTree(root->left, data);
        else
            root->right = buildTree(root->right, data);
        return root;
    }
}

int minValue(node* root) {
    if(root==NULL){
        return -1;
    }
    
    node* cur = root;
 
    while (cur->left != NULL) {
        cur = cur->left;
    }
    return (cur->data);
}

int main(){
    struct node* root = NULL;
    root = buildTree(root, 5);
    buildTree(root, 4);
    buildTree(root, 6);
    buildTree(root, 3);
    buildTree(root, 2);
    buildTree(root, 1);
    buildTree(root, 7);

    int mn=minValue(root);
    printf("Minimum Element: %d",mn);

    return 0;
}


Output

Minimum Element: 1


Program to find minimum element in BST (Binary Search Tree) in Cpp

#include <bits/stdc++.h>
using namespace std;

class node
{
public:
    int data;
    node *left;
    node *right;

    // constructor
    node(int val)
    {
        data = val;
        left = NULL;
        right = NULL;
    }
};
node* buildTree(node* root, int data)
{
    if (root == NULL)
        return (new node(data));
    else {

        if (data <= root->data)
            root->left = buildTree(root->left, data);
        else
            root->right = buildTree(root->right, data);
        return root;
    }
}

int minValue(node* root) {
    if(root==NULL){
        return -1;
    }
    
    node* cur = root;
 
    while (cur->left != NULL) {
        cur = cur->left;
    }
    return (cur->data);
}

int main(){
    node* root = NULL;
    root = buildTree(root, 5);
    buildTree(root, 4);
    buildTree(root, 6);
    buildTree(root, 3);
    buildTree(root, 2);
    buildTree(root, 1);
    buildTree(root, 7);

    int mn=minValue(root);
    cout<<"Minimum Element: "<<mn;

    return 0;
}


Output

Minimum Element: 1


Post a Comment

If you have any doubts, please let me know

Previous Post Next Post