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
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest 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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.