0% found this document useful (0 votes)
43 views4 pages

Dsa Lab 11 064

The document describes a C++ program that implements binary search tree operations like insertion, searching, and finding the minimum value. It defines Node and BT classes where the BT class contains methods for inserting a node, displaying the tree inorder, searching for a specific value, and finding the minimum value in the tree. The main function tests these methods by building a sample tree with input values, searching for a value, and finding the minimum value.

Uploaded by

Warisha Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

Dsa Lab 11 064

The document describes a C++ program that implements binary search tree operations like insertion, searching, and finding the minimum value. It defines Node and BT classes where the BT class contains methods for inserting a node, displaying the tree inorder, searching for a specific value, and finding the minimum value in the tree. The main function tests these methods by building a sample tree with input values, searching for a value, and finding the minimum value.

Uploaded by

Warisha Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

DATA STRUCTURES AND

ALGORITHMS
WARISHA MALIK
064 BSCS
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