DSL 4
DSL 4
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;