You are on page 1of 4

DATA STRUCTURES AND

ALGORITHMS
TASK 1

INPUT

#include<iostream>
using namespace std;
class node{
public:
int data;
node* left = NULL;
node* right = NULL;
};
class bt
{ public:

node* Insert(node* root, int value)


{
if(root == NULL)
{
root = new node;
root->data = value;
return root;
}

// Insert data.
if(value<root->data){

if(root->left == NULL)
{
root->left = Insert(root->left, value);

}
else{

root->left = Insert(root->left, value);


}
}
if(value>root->data){
if(root->right==NULL){
root->right = Insert(root->right, value);
}
else{
root->right = Insert(root->right, value);
}
}
return root;
}

void displayin(node* root)


{
if(!root) {
return;
}
displayin(root->left);
cout << root->data<< endl;
displayin(root->right);
}

bool search_specific(node* nodee, int key)


{
if (nodee == NULL)
return false;

if (nodee->data == key)
return true;
bool res1 = search_specific(nodee->left, key);
if(res1) return true;

bool res2 = search_specific(nodee->right, key);

return res2;
}
int findMin(node* root)
{
//code
if(root==NULL)
{
return INT_MAX;
}
int res=root->data;
int left=findMin(root->left);
int right=findMin(root->right);
if(left<res)
{
res=left;
}
if(right<res)
{
res=right;
}
return res;
}

};
// Driver code
int main()
{
node *root = NULL;
bt b;
int num,value;
cout<<"\n Enter the number of nodes: ";
cin>>num;
cout<<"Enter the value of nodes : ";
cin>>value;
root = b.Insert(root,value);
for(int i =0;i<num-1;i++){
cout<<"\n Enter the value of nodes : ";
cin>>value;
b.Insert(root,value);
}
cout<<"\n Displaying data in tree : "<<endl;
b.displayin(root);

cout<<"\n Search specific :";


bool sp;
cout<<"\nEnter the number you want to search : ";
int in;
cin>>in;
sp = b.search_specific(root,in);
if(sp==true){
cout<<"The specific number is found . ";
}
else{
cout<<"The specific number is not found .";
}
int min;
min = b.findMin(root);
cout<<"\n The minimum number in tree is : "<<min;
return 0;
}

OUTPUT

You might also like