0% found this document useful (0 votes)
15 views9 pages

Lab - 2 (Fouzia)

lab task

Uploaded by

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

Lab - 2 (Fouzia)

lab task

Uploaded by

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

Lab no: 02

Lab Name: Implementing Binary Search, and BST=> min(), max() function
and preorder and post-order functions.
Binary Search:
#include<bits/stdc++.h>
using namespace std;
int binarySearch(int A[], int lo, int hi, int x)
{
int mid;
if(lo>hi)
{

return -1;
}
mid = (lo + hi)/2;

if(x == A[mid])
{
return mid;
}
else if(x <A[mid])
{
binarySearch (A, lo, mid-1, x);
}
else
{
binarySearch( A, mid+1, hi, x);
}

void selectionSort(int num[], int n)


{
for(int i = 0; i<n-1; i++)
{
int minIndex = i;
for(int j = i +1; j<n; j++)
{
if(num[j]<num[minIndex])
{
minIndex = j;
}
}
int temp = num[i];
num[i] = num[minIndex];
num[minIndex] = temp;
}
}

int main()
{
system("color f0");

cout<<"How many item do you want to insert? ";


int n;

cin>>n;
cout<<"Insert please: "<<endl;
int arr[n];
for(int i = 0; i<n; i++)
{

cin>>arr[i];
}

selectionSort(arr, n);

cout<<endl<<"Your Array: [ ";


for(int i = 0; i<n; i++)
{
cout<<arr[i]<<" ";
}
cout<<"]"<<endl<<endl;;

cout<<"Which value do you want to search? ";


int num;
cin>>num;

int a = binarySearch(arr, 0, n, num);


if(a>=0)
cout<<endl<<"Item found at index: "<<a<<endl;
else
cout<<endl<<"Item not found" <<endl;

}
Output for above code:

BST implementation:

#include <iostream>
using namespace std;

class BST
{
private:
struct node
{
int data;
node* left;
node* right;
};
node* root;

//Inserting an item in BST


node* insert(int x, node* t)
{
if(t == NULL)
{
t = new node;
t->data = x;
t->left = t->right = NULL;
}
else if(x < t->data)
t->left = insert(x, t->left);
else if(x > t->data)
t->right = insert(x, t->right);
return t;
}

//Searching an item in BST


node* search(node* t, int x)
{
if(t == NULL || t->data == x)
return t;
if(t->data < x)
return search(t->right,x);
return search(t->left,x);
}

node* findMin(node* t)
{
if(t == NULL)
return NULL;
else if(t->left == NULL)
return t;
else
return findMin(t->left);
}

node* findMax(node* t)
{
if(t == NULL)
return NULL;
else if(t->right == NULL)
return t;
else
return findMax(t->right);
}

node* remove(int x, node* t)


{
node* temp;
if(t == NULL)
return NULL;
else if(x < t->data)
t->left = remove(x, t->left);
else if(x > t->data)
t->right = remove(x, t->right);
else if(t->left && t->right)
{
temp = findMin(t->right);
t->data = temp->data;
t->right = remove(t->data, t->right);
}
else
{
temp = t;
if(t->left == NULL)
t = t->right;
else if(t->right == NULL)
t = t->left;
delete temp;
}
return t;
}

void inorder(node* t)
{
if(t == NULL)
return;
inorder(t->left);
cout << t->data << " ";
inorder(t->right);
}
void preorder(node* t)
{
if(t == NULL)
return;
cout << t->data << " ";
preorder(t->left);
preorder(t->right);
}

void postorder(node* t)
{
if(t == NULL)
return;
postorder(t->left);
postorder(t->right);
cout << t->data << " ";
}

public:
BST()
{
root = NULL;
}

void insert(int x)
{
root = insert(x, root);
}

void remove(int x)
{
root = remove(x, root);
}

void display()
{
inorder(root);
cout << endl;
}
void search(int x)
{
node* result = search(root, x);
if (result != NULL)
cout << "Key found" << endl;
else
cout << "Key not found" << endl;
}

int findMin()
{
node* minNode = findMin(root);
if(minNode != NULL)
return minNode->data;
else
{
cout << "Tree is empty." << endl;
return -1; // Or any other value indicating error
}
}

int findMax()
{
node* maxNode = findMax(root);
if(maxNode != NULL)
return maxNode->data;
else
{
cout << "Tree is empty." << endl;
return -1; // Or any other value indicating error
}
}

void displayPreorder()
{
preorder(root);
cout << endl;
}

void displayPostorder()
{
postorder(root);
cout << endl;
}
};

int main()
{
system("color f0");
BST tree;
//tree.insert(20);
cout<<"Enter the number of nodes to be inserted in the tree:";
int n;
cin>>n;
cout<<"Enter items\n";
while(n--)
{
int data;
cin>>data;
tree.insert(data);
}
cout<<"Display items: (inorder) ";
tree.display();
cout<<endl<<"Enter the key to search in the tree: ";
int key;
cin>>key;
tree.search(key);

//tree.remove(25);
cout<<endl<<"Item/key to be erased: ";
cin>>key;
tree.remove(key);
tree.display();

cout <<endl<< "Minimum value: " << tree.findMin() << endl;


cout <<endl<< "Maximum value: " << tree.findMax() << endl;

cout <<endl<< "Preorder traversal: ";


tree.displayPreorder();
cout<<endl << "Postorder traversal: ";
tree.displayPostorder();

return 0;
}

Output for above code:

Conclusion:
Understanding the Binary Search Algorithm is like finding a word in a dictionary by repeatedly
dividing the search range in half until you find the desired word. It's highly efficient for large
datasets with a time complexity of O(log n).

Binary Search Trees (BSTs) organize data hierarchically, much like a family tree. Each node
has two children: one with smaller values and one with larger values. This structure allows for
quick searching, insertion, and deletion operations, making BSTs invaluable for managing
dynamic sets of data.

You might also like