4678-Assignment-5
4678-Assignment-5
class Node {
public:
int key;
Node* left;
Node* right;
class BST {
public:
void insert(Node*& root, int key) {
Node* newNode = new Node(key);
if (root == nullptr) {
root = newNode;
}
else {
Node* cur = root;
Node* parent = nullptr;
while (cur != nullptr) {
parent = cur;
if (key < cur->key) {
cur = cur->left;
}
else {
cur = cur->right;
}
}
if (key < parent->key) {
parent->left = newNode;
}
else {
parent->right = newNode;
}
}
}
if (cur != nullptr) {
cout << "Matching result is found" << endl;
}
else {
cout << "Key not found" << endl;
}
}
inorderDisplay(root->left);
cout << root->key << " ";
inorderDisplay(root->right);
}
postorderDisplay(root->left);
postorderDisplay(root->right);
cout << root->key << " ";
}
return cur;
}
return cur;
}
if (cur->left != nullptr) {
return findMax(cur->left);
}
else {
Node* predecessor = nullptr;
Node* ancestor = root;
while (ancestor->key != cur->key) {
if (cur->key > ancestor->key) {
predecessor = ancestor;
ancestor = ancestor->right;
}
else {
ancestor = ancestor->left;
}
}
if (!predecessor) {
return cur; // the node is the maximum, return itself
}
return predecessor;
}
}
// Node with two children: Get the inorder successor (smallest in the right subtree)
Node* temp = findMin(root->right);
// Copy the inorder successor's content to this node
root->key = temp->key;
queue<Node*> q;
q.push(root);
int height = 0;
while (!q.empty()) {
int levelSize = q.size();
height++;
int main() {
BST bst;
Node* root = nullptr;
int choice, key, order, height;
do {
cout << "\nMenu:\n";
cout << "1. Display\n";
cout << "2. Search for a key\n";
cout << "3. Min\n";
cout << "4. Max\n";
cout << "5. Insertion\n";
cout << "6. Deletion\n";
cout << "7. Successor\n";
cout << "8. Predecessor\n";
cout << "9. Height of tree\n";
cout << "10. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Choose display order:\n";
cout << "1. Inorder\n";
cout << "2. Preorder\n";
cout << "3. Postorder\n";
cin >> order;
bst.display(root, order);
break;
case 2:
cout << "Enter key to search: ";
cin >> key;
bst.searchNode(root, key);
break;
case 3:
{
Node* minNode = bst.findMin(root);
if (minNode != nullptr)
cout << "Minimum value in the BST: " << minNode->key << endl;
else
cout << "BST is empty, cannot find minimum" << endl;
}
break;
case 4:
{
Node* maxNode = bst.findMax(root);
if (maxNode != nullptr)
cout << "Maximum value in the BST: " << maxNode->key << endl;
else
cout << "BST is empty, cannot find maximum" << endl;
}
break;
case 5:
cout << "Enter key to insert: ";
cin >> key;
bst.insert(root, key);
break;
case 6:
cout << "Enter key to delete: ";
cin >> key;
root = bst.deleteNode(root, key);
break;
case 7:
cout << "Enter key to find successor: ";
cin >> key;
{
Node* successor = bst.findSuccessor(root, key);
if (successor) {
cout << "Successor of " << key << " is " << successor->key << endl;
}
else {
cout << "The value whose successor you want to find is not present in the BST" <<
endl;
}
}
break;
case 8:
cout << "Enter key to find predecessor: ";
cin >> key;
{
Node* predecessor = bst.findPredecessor(root, key);
if (predecessor) {
cout << "Predecessor of " << key << " is " << predecessor->key << endl;
}
else {
cout << "The value whose predecessor you want to find is not present in the BST"
<< endl;
}
}
break;
case 9:
height = bst.findHeight(root);
if (height == 0) {
cout << "BST is empty, so height is " << height << endl;
}
else {
cout << "Height of BST is " << height << endl;
}
break;
case 10:
cout << "Exiting...\n";
return 0;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 10);
return 0;
}
OUTPUT: