C++ Program to Find the Node with Minimum Value in a Binary Search Tree

This C++ program, displays the minimum element present in a binary search tree.

Here is the source code of the C++ program which creates a binary search tree on the basis of the inputs given and goes on traversing left from the root until the least value is encountered. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is also shown below.

  1. /*
  2.  * C++ Program to Find the Minimum value of Binary Search Tree
  3.  */
  4. #include <iostream>
  5. using namespace std;
  6. #include <conio.h>
  7. struct tree
  8. {
  9.     tree *l, *r;
  10.     int data;
  11. }*root = NULL, *p = NULL, *np = NULL, *q;
  12.  
  13.  
  14. void create()
  15. {
  16.     int value, c = 0;   
  17.     while (c < 7)
  18.     {
  19.         if (root == NULL)
  20.         {
  21.             root = new tree;
  22.             cout<<"enter value of root node\n";
  23.             cin>>root->data;
  24.             root->r=NULL;
  25.             root->l=NULL;
  26.         }
  27.         else
  28.         {
  29.             p = root;
  30.             cout<<"enter value of node\n";
  31.             cin>>value;
  32.             while(true)
  33.             {
  34.                 if (value < p->data)
  35.                 {
  36.                     if (p->l == NULL)
  37.                     {
  38.                         p->l = new tree;
  39.                         p = p->l;
  40.                         p->data = value;
  41.                         p->l = NULL;
  42.                         p->r = NULL;
  43.                         cout<<"value entered in left\n";
  44.                         break;
  45.                     }
  46. 		    else if (p->l != NULL)
  47. 		    {
  48. 		        p = p->l;
  49. 		    }
  50.                 }
  51. 		else if (value > p->data)
  52. 		{
  53. 		    if (p->r == NULL)
  54. 		    {
  55. 		        p->r = new tree;
  56. 		        p = p->r;
  57.                         p->data = value;
  58.                         p->l = NULL;
  59.                         p->r = NULL;
  60. 		        cout<<"value entered in right\n";
  61. 	                break;
  62. 		    }
  63. 		    else if (p->r != NULL)
  64. 		    {
  65.                         p = p->r;
  66. 		    }
  67. 		}
  68. 	    }
  69.         }
  70.         c++;
  71.     }
  72. }
  73. int inorder(tree *p)
  74. {
  75.     int min;
  76.     while (p->l != NULL)
  77.     {
  78.         p = p->l;
  79.     }
  80.     return(p->data);
  81. }
  82. int main()
  83. {
  84.  create();
  85.  x=inorder(root);
  86.  cout<<"Minimum value in tree:"<<x<<endl;
  87.  getch();
  88. }

Output
enter value of root node
8
enter value of node
9
value entered in right
enter value of node
6
value entered in left
enter value of node
5
value entered in left
enter value of node
10
value entered in right
enter value of node
4
value entered in left
enter value of node
3
value entered in left
Minimum value in tree:3

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.