#include <iostream>
using namespace std;
struct Node
{
int data;
Node *left;
Node *right;
};
// Create a new node
Node* createNode(int value)
{
Node *newNode = new Node;
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Insert a node
Node* insert(Node *root, int value)
{
if (root == NULL)
return createNode(value);
if (value < root->data)
root->left = insert(root->left, value);
else if (value > root->data)
root->right = insert(root->right, value);
return root;
}
// Search an element
void search(Node *root, int key)
{
if (root == NULL)
{
cout << "Element not found";
return;
}
if (root->data == key)
{
cout << "Element found";
}
else if (key < root->data)
{
search(root->left, key);
}
else
{
search(root->right, key);
}
}
// Find minimum value
int findMin(Node *root)
{
while (root->left != NULL)
{
root = root->left;
}
return root->data;
}
// Find maximum value
int findMax(Node *root)
{
while (root->right != NULL)
{
root = root->right;
}
return root->data;
}
// Display BST using Inorder
void inorder(Node *root)
{
if (root != NULL)
{
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}
int main()
{
Node *root = NULL;
int choice, value;
do
{
cout << "\n\n--- BINARY SEARCH TREE ---";
cout << "\n1. Insert";
cout << "\n2. Search";
cout << "\n3. Minimum";
cout << "\n4. Maximum";
cout << "\n5. Display";
cout << "\n6. Exit";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter value: ";
cin >> value;
root = insert(root, value);
cout << "Node inserted";
break;
case 2:
cout << "Enter value to search: ";
cin >> value;
search(root, value);
break;
case 3:
if (root == NULL)
cout << "Tree is empty";
else
cout << "Minimum value = " << findMin(root);
break;
case 4:
if (root == NULL)
cout << "Tree is empty";
else
cout << "Maximum value = " << findMax(root);
break;
case 5:
cout << "BST Elements: ";
inorder(root);
break;
case 6:
cout << "Program ended";
break;
default:
cout << "Invalid choice";
}
} while (choice != 6);
return 0;
}




