0% found this document useful (0 votes)
8 views3 pages

DSL 4

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)
8 views3 pages

DSL 4

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

Name : Om Santosh Songire

SE Computer B
Roll No : 128
Assignment No : 4

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

Program :
#include <iostream>
using namespace std;
struct Node {
int val;
//int NULL = 0;
//int x = 0;
Node* left;
Node* right;
Node(int key) {
val = key;
left = right = NULL;
}
};
Node* insert(Node* root, int key) {
if (root == NULL)
return new Node(key);
if (key < root->val)
root->left = insert(root->left, key);
else
root->right = insert(root->right, key);
return root;
}
int height(Node* root) {
if (root == NULL)
return 0;
return 1 + max(height(root->left), height(root->right));
}
int findMin(Node* root) {
while (root->left != NULL)
root = root->left;
return root->val;
}
void mirror(Node* root) {
if (root == NULL)
return;

Node* temp = root->left;


root->left = root->right;
root->right = temp;
mirror(root->left); mirror(root->right);
}
bool search(Node* root, int key) {
if (root == NULL)
return false;
if (root->val == key)
return true;
if (key < root->val)
return search(root->left, key);
return search(root->right, key);
}
void inorder(Node* root) {
if (root != NULL) {
inorder(root->left);
cout << root->val << " ";
inorder(root->right);
}
}
int main() {
Node* root = NULL;
int n, value;
cout << "Enter number of elements in BST: ";
cin >> n;
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> value;
root = insert(root, value);
}
int newVal;
cout << "Enter a value to insert: ";
cin >> newVal;
root = insert(root, newVal);
cout << "Number of nodes in the longest path: " << height(root) << endl;
cout << "Minimum value in BST: " << findMin(root) << endl;
mirror(root);
cout << "Inorder traversal after mirroring: ";
inorder(root);
cout << endl;
int key;
cout << "Enter a value to search: ";
cin >> key;
cout << "Searching for " << key << ": " << (search(root, key) ? "Found" : "Not Found") <<
endl;
return 0;
}
Output:

(base) comp-proj-sys04@compprojsys04-OptiPlex-3010:~$ g++ dsal4.cpp


(base) comp-proj-sys04@compprojsys04-OptiPlex-3010:~$ ./a.out
Enter number of elements in BST: 5
Enter the elements: 22
26
45
56
92
Enter a value to insert: 26
Number of nodes in the longest path: 5
Minimum value in BST: 22
Inorder traversal after mirroring: 92 56 45 26 26 22
Enter a value to search: 99
Searching for 99: Not Found

You might also like