KEMBAR78
CODE4 | PDF | Pointer (Computer Programming) | Software Engineering
0% found this document useful (0 votes)
22 views9 pages

CODE4

The document contains C++ code for a binary search tree (BST) implementation, including functions for inserting nodes, finding the longest path, finding the minimum value, swapping left and right pointers, searching for a value, and displaying the tree. The main function provides a user interface to interact with the BST through various options. The code demonstrates basic operations on a BST, allowing users to manipulate and query the tree structure.

Uploaded by

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

CODE4

The document contains C++ code for a binary search tree (BST) implementation, including functions for inserting nodes, finding the longest path, finding the minimum value, swapping left and right pointers, searching for a value, and displaying the tree. The main function provides a user interface to interact with the BST through various options. The code demonstrates basic operations on a BST, allowing users to manipulate and query the tree structure.

Uploaded by

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

CODE:

#include <iostream>

#include <climits>

#include <queue>

using namespace std;

struct TreeNode {

int data;

TreeNode* left;

TreeNode* right;

};

// Function to create a new node

TreeNode* createNode(int value) {

TreeNode* newNode = new TreeNode();

newNode->data = value;

newNode->left = newNode->right = NULL;

return newNode;

// Function to insert a value into the BST

TreeNode* insertNode(TreeNode* root, int value) {

if (root == NULL) {

return createNode(value);

}
if (value < root->data) {

root->left = insertNode(root->left, value);

} else if (value > root->data) {

root->right = insertNode(root->right, value);

return root;

// Function to find the number of nodes in the longest path

int longestPath(TreeNode* root) {

if (root == NULL)

return 0;

int leftHeight = longestPath(root->left);

int rightHeight = longestPath(root->right);

return 1 + max(leftHeight, rightHeight);

// Function to find the minimum value in the BST

int findMin(TreeNode* root) {

if (root == NULL)

return INT_MAX;
int leftMin = findMin(root->left);

int rightMin = findMin(root->right);

return min(root->data, min(leftMin, rightMin));

// Function to swap left and right pointers at every node

void swapLeftRight(TreeNode* root) {

if (root == NULL)

return;

TreeNode* temp = root->left;

root->left = root->right;

root->right = temp;

swapLeftRight(root->left);

swapLeftRight(root->right);

// Function to search for a value in the BST

bool searchValue(TreeNode* root, int value) {

if (root == NULL)

return false;
if (root->data == value)

return true;

if (value < root->data)

return searchValue(root->left, value);

else

return searchValue(root->right, value);

// Function to display elements of the BST in in-order traversal

void displayTree(TreeNode* root) {

if (root == NULL)

return;

queue<TreeNode*> q;

q.push(root);

while (!q.empty()) {

int levelSize = q.size();

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

TreeNode* current = q.front();

q.pop();

cout << current->data << " ";


if (current->left)

q.push(current->left);

if (current->right)

q.push(current->right);

cout << endl;

int main() {

TreeNode* root = NULL;

int choice, value;

do {

cout << "1. Insert new node" << endl;

cout << "2. Find number of nodes in longest path" << endl;

cout << "3. Minimum data value found in the tree" << endl;

cout << "4. Change the tree so that the roles of the left and right pointers are swapped at every
node" << endl;

cout << "5. Search a value" << endl;

cout << "6. Display elements of the tree (in-order traversal)" << endl;

cout << "7. Exit" << endl;

cout << "Enter your choice: ";


cin >> choice;

switch(choice) {

case 1:

cout << "Enter value to insert: ";

cin >> value;

root = insertNode(root, value);

break;

case 2:

cout << "Number of nodes in the longest path: " << longestPath(root) << endl;

break;

case 3:

cout << "Minimum value in the tree: " << findMin(root) << endl;

break;

case 4:

swapLeftRight(root);

cout << "Left and right pointers swapped at every node." << endl;

break;

case 5:

cout << "Enter value to search: ";

cin >> value;

if (searchValue(root, value))

cout << "Value found in the tree." << endl;

else

cout << "Value not found in the tree." << endl;


break;

case 6:

cout << "Displaying the whole constructed tree as binary tree:" << endl;

displayTree(root);

break;

case 7:

cout << "Exiting program." << endl;

break;

default:

cout << "Invalid choice. Please try again." << endl;

} while (choice != 7);

return 0;

OUTPUT:

1. Insert new node

2. Find number of nodes in longest path

3. Minimum data value found in the tree

4. Change the tree so that the roles of the left and right pointers are swapped at every node

5. Search a value

6. Display elements of the tree (in-order traversal)

7. Exit

Enter your choice: 1


Enter value to insert: 2

1. Insert new node

2. Find number of nodes in longest path

3. Minimum data value found in the tree

4. Change the tree so that the roles of the left and right pointers are swapped at every node

5. Search a value

6. Display elements of the tree (in-order traversal)

7. Exit

Enter your choice: 1

Enter value to insert: 3

1. Insert new node

2. Find number of nodes in longest path

3. Minimum data value found in the tree

4. Change the tree so that the roles of the left and right pointers are swapped at every node

5. Search a value

6. Display elements of the tree (in-order traversal)

7. Exit

Enter your choice: 1

Enter value to insert: 5

1. Insert new node

2. Find number of nodes in longest path

3. Minimum data value found in the tree

4. Change the tree so that the roles of the left and right pointers are swapped at every node

5. Search a value

6. Display elements of the tree (in-order traversal)


7. Exit

Enter your choice: 1

Enter value to insert: 6

1. Insert new node

2. Find number of nodes in longest path

3. Minimum data value found in the tree

4. Change the tree so that the roles of the left and right pointers are swapped at every node

5. Search a value

6. Display elements of the tree (in-order traversal)

7. Exit

Enter your choice: 6

Displaying the whole constructed tree as binary tree:

1. Insert new node

2. Find number of nodes in longest path

3. Minimum data value found in the tree

4. Change the tree so that the roles of the left and right pointers are swapped at every node

5. Search a value

6. Display elements of the tree (in-order traversal)

7. Exit

You might also like