0% found this document useful (0 votes)
5 views10 pages

Umesh Prog4

The document describes a C++ program that implements a binary search tree (BST) with various operations such as insertion, finding the height, minimum value, mirroring the tree, and searching for an element. It includes a main function that provides a menu-driven interface for users to interact with the BST. The program demonstrates the functionality through user input and outputs the results of the operations performed.

Uploaded by

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

Umesh Prog4

The document describes a C++ program that implements a binary search tree (BST) with various operations such as insertion, finding the height, minimum value, mirroring the tree, and searching for an element. It includes a main function that provides a menu-driven interface for users to interact with the BST. The program demonstrates the functionality through user input and outputs the results of the operations performed.

Uploaded by

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

Name : Umesh .V.

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>

using namespace std;

class node {

public:

int info;

node *left;

node *right;

};

class BST {

public:

node *root;

BST() {

root = NULL;

void insert(node *, node *);

int height(node *);

int min(node *);


void mirror(node *);

void search(node *, int);

void display(node *, int);

};

void BST::insert(node *tree, node *newnode) {

if (root == NULL) {

root = new node;

root->info = newnode->info;

root->left = NULL;

root->right = NULL;

cout << "Root Node is Added" << endl;

return;

if (tree->info == newnode->info) {

cout << "Element already in the tree" << endl;

return;

if (tree->info > newnode->info) {

if (tree->left != NULL) {

insert(tree->left, newnode);

} else {

tree->left = newnode;

tree->left->left = NULL;

tree->left->right = NULL;

cout << "Node Added To Left" << endl;

return;

} else {

if (tree->right != NULL) {

insert(tree->right, newnode);
} else {

tree->right = newnode;

tree->right->left = NULL;

tree->right->right = NULL;

cout << "Node Added To Right" << endl;

return;

int BST::height(node *root) {

if (root == NULL) {

return 0;

int htleft = height(root->left);

int htright = height(root->right);

return max(htleft, htright) + 1;

int BST::min(node *root) {

if (root == NULL) {

cout << "Tree is empty" << endl;

return -1;

node *temp = root;

while (temp->left != NULL) {

temp = temp->left;

return temp->info;

}
void BST::mirror(node *root) {

if (root != NULL) {

node *temp = root->left;

root->left = root->right;

root->right = temp;

mirror(root->left);

mirror(root->right);

void BST::search(node *ptr, int searchdata) {

if (ptr == NULL) {

cout << "Element not found..." << endl;

return;

if (ptr->info == searchdata) {

cout << "Element Found..." << endl;

} else if (ptr->info < searchdata) {

search(ptr->right, searchdata);

} else {

search(ptr->left, searchdata);

void BST::display(node *ptr, int level) {

if (ptr != NULL) {

display(ptr->right, level + 1);

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

cout << " ";

cout << ptr->info << endl;

display(ptr->left, level + 1);


}

int main() {

BST bst;

int choice, num;

while (1) {

cout << "-----------------" << endl;

cout << "Operations on BST" << endl;

cout << "-----------------" << endl;

cout << "1. Insert Element" << endl;

cout << "2. Find Number of Nodes in Longest Path" << endl;

cout << "3. Minimum Value in the Tree" << endl;

cout << "4. Mirror the Tree" << endl;

cout << "5. Search an Element" << endl;

cout << "6. Quit" << endl;

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1: {

node *temp = new node();

cout << "Enter the number to be inserted: ";

cin >> temp->info;

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: {

int minValue = bst.min(bst.root);

if (minValue != -1) {

cout << "Minimum value in the tree: " << minValue << endl;

break;

case 4: {

bst.mirror(bst.root);

cout << "Tree has been mirrored." << endl;

cout << "Mirrored Tree:" << endl;

bst.display(bst.root, 1);

break;

case 5: {

int searchdata;

cout << "Enter the element to be searched: ";

cin >> searchdata;

bst.search(bst.root, searchdata);

break;

case 6:

exit(0);

default:

cout << "Wrong choice" << endl;


}

}
Output :

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ cd ..

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:/home$ cd computer/

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ g++ Program4.cpp

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ ./a.out

-----------------

Operations on BST

-----------------

1. Insert Element

2. Find Number of Nodes in Longest Path

3. Minimum Value in the Tree

4. Mirror the Tree

5. Search an Element

6. Quit

Enter your choice: 1

Enter the number to be inserted: 50

Root Node is Added

-----------------

Operations on BST

-----------------

1. Insert Element

2. Find Number of Nodes in Longest Path

3. Minimum Value in the Tree

4. Mirror the Tree

5. Search an Element

6. Quit

Enter your choice: 1

Enter the number to be inserted: 30

Node Added To Left

-----------------

Operations on BST
-----------------

1. Insert Element

2. Find Number of Nodes in Longest Path

3. Minimum Value in the Tree

4. Mirror the Tree

5. Search an Element

6. Quit

Enter your choice: 1

Enter the number to be inserted: 70

Node Added To Right

-----------------

Operations on BST

-----------------

1. Insert Element

2. Find Number of Nodes in Longest Path

3. Minimum Value in the Tree

4. Mirror the Tree

5. Search an Element

6. Quit

Enter your choice: 2

Number of nodes in longest path: 2

-----------------

Operations on BST

-----------------

1. Insert Element

2. Find Number of Nodes in Longest Path

3. Minimum Value in the Tree

4. Mirror the Tree

5. Search an Element

6. Quit

Enter your choice: 3


Minimum value in the tree: 30

-----------------

Operations on BST

-----------------

1. Insert Element

2. Find Number of Nodes in Longest Path

3. Minimum Value in the Tree

4. Mirror the Tree

5. Search an Element

6. Quit

Enter your choice: 4

Tree has been mirrored.

Mirrored Tree:

30

50

70

-----------------

Operations on BST

-----------------

1. Insert Element

2. Find Number of Nodes in Longest Path

3. Minimum Value in the Tree

4. Mirror the Tree

5. Search an Element

6. Quit

Enter your choice: 5

Enter the element to be searched: 70

Element not found...

-----------------

Operations on BST

-----------------
1. Insert Element

2. Find Number of Nodes in Longest Path

3. Minimum Value in the Tree

4. Mirror the Tree

5. Search an Element

6. Quit

Enter your choice: 6

You might also like