0% found this document useful (0 votes)
2 views

4678-Assignment-5

The document contains a C++ implementation of a Binary Search Tree (BST) with various functionalities including insertion, deletion, searching, and traversal methods (inorder, preorder, postorder). It also includes functions to find the minimum and maximum values, as well as the successor and predecessor of a given key. The main function provides a menu-driven interface for users to interact with the BST operations.

Uploaded by

fatimaraja1258
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

4678-Assignment-5

The document contains a C++ implementation of a Binary Search Tree (BST) with various functionalities including insertion, deletion, searching, and traversal methods (inorder, preorder, postorder). It also includes functions to find the minimum and maximum values, as well as the successor and predecessor of a given key. The main function provides a menu-driven interface for users to interact with the BST operations.

Uploaded by

fatimaraja1258
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

SUBMITTED BY: NOOR FATIMA

SUBMITTED TO: DR. NADEEM


REG NO. 4678-FOC/BSCS/F22
ASSIGNMENT#5
TREE DATA STRUCTURE
// BINARY SEARCH TREE
#include <iostream>
#include <queue>

using namespace std;

class Node {
public:
int key;
Node* left;
Node* right;

Node(int key) : key(key), left(nullptr), right(nullptr) {}


};

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;
}
}
}

void searchNode(Node* root, int key) {


if (root == nullptr) {
cout << "BST is empty, cannot search" << endl;
return;
}

Node* cur = root;


while (cur != nullptr && cur->key != key) {
if (key < cur->key)
cur = cur->left;
else
cur = cur->right;
}

if (cur != nullptr) {
cout << "Matching result is found" << endl;
}
else {
cout << "Key not found" << endl;
}
}

void inorderDisplay(Node* root) {


//base case
if (root == nullptr) {
return;
}

inorderDisplay(root->left);
cout << root->key << " ";
inorderDisplay(root->right);
}

void preorderDisplay(Node* root) {


if (root == nullptr) {
return;
}

cout << root->key << " ";


preorderDisplay(root->left);
preorderDisplay(root->right);
}

void postorderDisplay(Node* root) {


if (root == nullptr) {
return;
}

postorderDisplay(root->left);
postorderDisplay(root->right);
cout << root->key << " ";
}

void display(Node* root, int order) {


if (order == 1)
inorderDisplay(root);
else if (order == 2)
preorderDisplay(root);
else
postorderDisplay(root);
}

Node* findMin(Node* root) {


if (root == nullptr) return nullptr;

Node* cur = root;


while (cur->left != nullptr) {
cur = cur->left;
}

return cur;
}

Node* findMax(Node* root) {


if (root == nullptr) return nullptr;

Node* cur = root;


while (cur->right != nullptr) {
cur = cur->right;
}

return cur;
}

Node* findSuccessor(Node* root, int key) {


//search
Node* cur = root;
while (cur != nullptr && cur->key != key) {
if (key < cur->key)
cur = cur->left;
else
cur = cur->right;
}

if (!cur) return nullptr;


//case 1
if (cur->right != nullptr) {
return findMin(cur->right);
}
//case2
else {
Node* successor = nullptr;
Node* ancestor = root;
while (ancestor->key != cur->key) {
if (cur->key < ancestor->key) {
successor = ancestor;
ancestor = ancestor->left;
}
else {
ancestor = ancestor->right;
}
}
if (!successor) {
return cur; //the node is the maximum, return itself
}
return successor;
}
}

Node* findPredecessor(Node* root, int key) {


//search
Node* cur = root;
while (cur != nullptr && cur->key != key) {
if (key < cur->key)
cur = cur->left;
else
cur = cur->right;
}

if (!cur) return nullptr;


//case 1

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* deleteNode(Node* root, int value) {


if (!root) return root;

if (value < root->key) {


root->left = deleteNode(root->left, value);
}
else if (value > root->key) {
root->right = deleteNode(root->right, value);
}
else {
// Node with only one child or no child
if (!root->left) {
Node* temp = root->right;
delete root;
return temp;
}
else if (!root->right) {
Node* temp = root->left;
delete root;
return temp;
}

// 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;

// Delete the inorder successor


root->right = deleteNode(root->right, temp->key);
}
return root;
}

int findHeight(Node* root) {


if (!root) return 0;

queue<Node*> q;
q.push(root);
int height = 0;

while (!q.empty()) {
int levelSize = q.size();
height++;

for (int i = 0; i < levelSize; i++) {


Node* node = q.front();
q.pop();
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return 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:

You might also like