Umesh Prog4
Umesh Prog4
Jadhav
Practical No : 4(B)
Batch : S1
Roll No : 20
Branch : AI & DS
/* Beginning with an empty binary search tree, Construct binary search tree by inserting the values
in the order given. After constructing a binary tree - i. Insert new node, ii. Find number of nodes in
longest path from root, iii. Minimum data value found in the tree, iv. Change a tree so that the roles
of the left and right pointers are swapped at every node, v. Search a value */
#include <iostream>
class node {
public:
int info;
node *left;
node *right;
};
class BST {
public:
node *root;
BST() {
root = NULL;
};
if (root == NULL) {
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
return;
if (tree->info == newnode->info) {
return;
if (tree->left != NULL) {
insert(tree->left, newnode);
} else {
tree->left = newnode;
tree->left->left = NULL;
tree->left->right = NULL;
return;
} else {
if (tree->right != NULL) {
insert(tree->right, newnode);
} else {
tree->right = newnode;
tree->right->left = NULL;
tree->right->right = NULL;
return;
if (root == NULL) {
return 0;
if (root == NULL) {
return -1;
temp = temp->left;
return temp->info;
}
void BST::mirror(node *root) {
if (root != NULL) {
root->left = root->right;
root->right = temp;
mirror(root->left);
mirror(root->right);
if (ptr == NULL) {
return;
if (ptr->info == searchdata) {
search(ptr->right, searchdata);
} else {
search(ptr->left, searchdata);
if (ptr != NULL) {
int main() {
BST bst;
while (1) {
cout << "2. Find Number of Nodes in Longest Path" << endl;
switch (choice) {
case 1: {
temp->left = NULL;
temp->right = NULL;
bst.insert(bst.root, temp);
break;
case 2: {
int h = bst.height(bst.root);
cout << "Number of nodes in longest path: " << h << endl;
break;
}
case 3: {
if (minValue != -1) {
cout << "Minimum value in the tree: " << minValue << endl;
break;
case 4: {
bst.mirror(bst.root);
bst.display(bst.root, 1);
break;
case 5: {
int searchdata;
bst.search(bst.root, searchdata);
break;
case 6:
exit(0);
default:
}
Output :
(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ cd ..
-----------------
Operations on BST
-----------------
1. Insert Element
5. Search an Element
6. Quit
-----------------
Operations on BST
-----------------
1. Insert Element
5. Search an Element
6. Quit
-----------------
Operations on BST
-----------------
1. Insert Element
5. Search an Element
6. Quit
-----------------
Operations on BST
-----------------
1. Insert Element
5. Search an Element
6. Quit
-----------------
Operations on BST
-----------------
1. Insert Element
5. Search an Element
6. Quit
-----------------
Operations on BST
-----------------
1. Insert Element
5. Search an Element
6. Quit
Mirrored Tree:
30
50
70
-----------------
Operations on BST
-----------------
1. Insert Element
5. Search an Element
6. Quit
-----------------
Operations on BST
-----------------
1. Insert Element
5. Search an Element
6. Quit