0% found this document useful (0 votes)
7 views5 pages

P4

The document contains a C++ implementation of a binary search tree (BST) with functionalities such as inserting nodes, finding the longest path, retrieving the minimum value, mirroring the tree, and searching for a value. A menu-driven interface allows users to interact with the BST, demonstrating various operations. The output showcases the results of these operations, including an inorder traversal and the longest path in the tree.

Uploaded by

yebop47991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

P4

The document contains a C++ implementation of a binary search tree (BST) with functionalities such as inserting nodes, finding the longest path, retrieving the minimum value, mirroring the tree, and searching for a value. A menu-driven interface allows users to interact with the BST, demonstrating various operations. The output showcases the results of these operations, including an inorder traversal and the longest path in the tree.

Uploaded by

yebop47991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//Roll No.

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

void insert(int key) {


root = insertRec(root, key);
}

Node* insertRec(Node* node, int key) {


if (!node) return new Node(key);
if (key < node->key) node->left = insertRec(node->left, key);
else node->right = insertRec(node->right, key);
return node;
}

int longestPath(Node* node) {


if (!node) return 0;
return max(longestPath(node->left), longestPath(node->right)) + 1;
}

int findMin(Node* node) {


while (node->left) node = node->left;
return node->key;
}

void mirror(Node* node) {


if (!node) return;
swap(node->left, node->right);
mirror(node->left);
mirror(node->right);
}
bool search(Node* node, int key) {
if (!node) return false;
if (node->key == key) return true;
if (key < node->key) return search(node->left, key);
return search(node->right, key);
}

void inorder(Node* node) {


if (node) {
inorder(node->left);
cout << node->key << " ";
inorder(node->right);
}
}
};

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...
*/

You might also like