P4
P4
: 12
//Assignment No. : 04
/* Problem Statement: Beginning with an empty binary search tree, Construct binary
search tree by inserting the values in the order given.
After constructing a binary tree -
-Insert new node
-Find number of nodes in longest path from root
-Minimum data value found in the tree
-Change a tree so that the roles of the left and right pointers are swapped at
every node
-Search a value*/
#include <iostream>
using namespace std;
class Node {
public:
int key;
Node *left, *right;
Node(int key) {
this->key = key;
left = right = nullptr;
}
};
class BST {
public:
Node *root;
BST() { root = nullptr; }
int main() {
BST bst;
int choice, value;
do {
cout<<"==========MENU==============";
cout << "\n1. Insert\n2. Display Inorder\n3. Longest Path\n4. Find Min\n5.
Mirror Tree\n6. Search\n7. Exit\n";
cout<<"============================\n";
cout<<"Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to insert: ";
cin >> value;
bst.insert(value);
break;
case 2:
cout << "Inorder Traversal: ";
bst.inorder(bst.root);
cout << endl;
break;
case 3:
cout << "Longest path: " << bst.longestPath(bst.root) << endl;
break;
case 4:
cout << "Minimum value: " << bst.findMin(bst.root) << endl;
break;
case 5:
bst.mirror(bst.root);
cout << "Tree mirrored.\n";
break;
case 6:
cout << "Enter value to search: ";
cin >> value;
cout << (bst.search(bst.root, value) ? "Found\n" : "Not Found\n");
break;
case 7:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 7);
return 0;
}
/*
**********OUTPUT**************
PS C:\Users\Admin\OneDrive\Desktop\DSA> g++ binary.cpp -o 1
PS C:\Users\Admin\OneDrive\Desktop\DSA> ./1
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 1
Enter value to insert: 78
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 1
Enter value to insert: 95
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 1
Enter value to insert: 62
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 1
Enter value to insert: 32
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 1
Enter value to insert: 46
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 1
Enter value to insert: 8
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 2
Inorder Traversal: 8 32 46 62 78 95
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 3
Longest path: 4
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 4
Minimum value: 8
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 6
Enter value to search: 95
Found
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 5
Tree mirrored.
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 2
Inorder Traversal: 95 78 62 46 32 8
==========MENU==============
1. Insert
2. Display Inorder
3. Longest Path
4. Find Min
5. Mirror Tree
6. Search
7. Exit
============================
Enter choice: 7
Exiting...
*/