jain12

minimum value in BST

Mar 11th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. class Node{
  4.   public:
  5.       int data;
  6.       Node *left,*right;
  7.       Node(int key){
  8.         data=key;
  9.         left=right=NULL;
  10.         }
  11.   };
  12.  
  13. void Insert(Node **root_ref,int key){
  14.   if(*root_ref==NULL){
  15.     *root_ref=new Node(key);
  16.     return;
  17.     }
  18.   if((*root_ref)->data<key)
  19.     Insert(&((*root_ref)->right),key);
  20.   else
  21.      if((*root_ref)->data>key)
  22.        Insert(&((*root_ref)->left),key);
  23.   }
  24.  
  25.   int MinimumValue(Node *root){
  26.     if(root==NULL)
  27.       return INT_MIN;
  28.     Node *temp=root;
  29.     while(temp->left!=NULL)
  30.       temp=temp->left;
  31.     return temp->data;
  32.     }
  33.  
  34. int main(){
  35.  Node *root=NULL;
  36.  Insert(&root,5);
  37.  Insert(&root,7);
  38.  Insert(&root,4);
  39.  Insert(&root,8);
  40.  Insert(&root,1);
  41.  Insert(&root,3);
  42.  cout<<"minimum value in tree is :"<<MinimumValue(root);
  43.  }
Add Comment
Please, Sign In to add comment