CODE:
#include <iostream>
#include <climits>
#include <queue>
using namespace std;
struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;
};
// Function to create a new node
TreeNode* createNode(int value) {
TreeNode* newNode = new TreeNode();
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
// Function to insert a value into the BST
TreeNode* insertNode(TreeNode* root, int value) {
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->left = insertNode(root->left, value);
} else if (value > root->data) {
root->right = insertNode(root->right, value);
return root;
// Function to find the number of nodes in the longest path
int longestPath(TreeNode* root) {
if (root == NULL)
return 0;
int leftHeight = longestPath(root->left);
int rightHeight = longestPath(root->right);
return 1 + max(leftHeight, rightHeight);
// Function to find the minimum value in the BST
int findMin(TreeNode* root) {
if (root == NULL)
return INT_MAX;
int leftMin = findMin(root->left);
int rightMin = findMin(root->right);
return min(root->data, min(leftMin, rightMin));
// Function to swap left and right pointers at every node
void swapLeftRight(TreeNode* root) {
if (root == NULL)
return;
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
swapLeftRight(root->left);
swapLeftRight(root->right);
// Function to search for a value in the BST
bool searchValue(TreeNode* root, int value) {
if (root == NULL)
return false;
if (root->data == value)
return true;
if (value < root->data)
return searchValue(root->left, value);
else
return searchValue(root->right, value);
// Function to display elements of the BST in in-order traversal
void displayTree(TreeNode* root) {
if (root == NULL)
return;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int levelSize = q.size();
for (int i = 0; i < levelSize; ++i) {
TreeNode* current = q.front();
q.pop();
cout << current->data << " ";
if (current->left)
q.push(current->left);
if (current->right)
q.push(current->right);
cout << endl;
int main() {
TreeNode* root = NULL;
int choice, value;
do {
cout << "1. Insert new node" << endl;
cout << "2. Find number of nodes in longest path" << endl;
cout << "3. Minimum data value found in the tree" << endl;
cout << "4. Change the tree so that the roles of the left and right pointers are swapped at every
node" << endl;
cout << "5. Search a value" << endl;
cout << "6. Display elements of the tree (in-order traversal)" << endl;
cout << "7. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1:
cout << "Enter value to insert: ";
cin >> value;
root = insertNode(root, value);
break;
case 2:
cout << "Number of nodes in the longest path: " << longestPath(root) << endl;
break;
case 3:
cout << "Minimum value in the tree: " << findMin(root) << endl;
break;
case 4:
swapLeftRight(root);
cout << "Left and right pointers swapped at every node." << endl;
break;
case 5:
cout << "Enter value to search: ";
cin >> value;
if (searchValue(root, value))
cout << "Value found in the tree." << endl;
else
cout << "Value not found in the tree." << endl;
break;
case 6:
cout << "Displaying the whole constructed tree as binary tree:" << endl;
displayTree(root);
break;
case 7:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
} while (choice != 7);
return 0;
OUTPUT:
1. Insert new node
2. Find number of nodes in longest path
3. Minimum data value found in the tree
4. Change the tree so that the roles of the left and right pointers are swapped at every node
5. Search a value
6. Display elements of the tree (in-order traversal)
7. Exit
Enter your choice: 1
Enter value to insert: 2
1. Insert new node
2. Find number of nodes in longest path
3. Minimum data value found in the tree
4. Change the tree so that the roles of the left and right pointers are swapped at every node
5. Search a value
6. Display elements of the tree (in-order traversal)
7. Exit
Enter your choice: 1
Enter value to insert: 3
1. Insert new node
2. Find number of nodes in longest path
3. Minimum data value found in the tree
4. Change the tree so that the roles of the left and right pointers are swapped at every node
5. Search a value
6. Display elements of the tree (in-order traversal)
7. Exit
Enter your choice: 1
Enter value to insert: 5
1. Insert new node
2. Find number of nodes in longest path
3. Minimum data value found in the tree
4. Change the tree so that the roles of the left and right pointers are swapped at every node
5. Search a value
6. Display elements of the tree (in-order traversal)
7. Exit
Enter your choice: 1
Enter value to insert: 6
1. Insert new node
2. Find number of nodes in longest path
3. Minimum data value found in the tree
4. Change the tree so that the roles of the left and right pointers are swapped at every node
5. Search a value
6. Display elements of the tree (in-order traversal)
7. Exit
Enter your choice: 6
Displaying the whole constructed tree as binary tree:
1. Insert new node
2. Find number of nodes in longest path
3. Minimum data value found in the tree
4. Change the tree so that the roles of the left and right pointers are swapped at every node
5. Search a value
6. Display elements of the tree (in-order traversal)
7. Exit