Lab - 2 (Fouzia)
Lab - 2 (Fouzia)
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);
}
int main()
{
system("color f0");
cin>>n;
cout<<"Insert please: "<<endl;
int arr[n];
for(int i = 0; i<n; i++)
{
cin>>arr[i];
}
selectionSort(arr, n);
}
Output for above code:
BST implementation:
#include <iostream>
using namespace std;
class BST
{
private:
struct node
{
int data;
node* left;
node* right;
};
node* root;
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);
}
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();
return 0;
}
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.