Practical No. - 4 - B
Practical No. - 4 - B
// Node structure
struct Node {
int data;
Node* left;
Node* right;
};
class BST {
public:
Node* root;
BST() : root(nullptr) {}
if (node == nullptr) {
} else {
return node;
}
if (node == nullptr) {
return 0;
current = current->left;
return current->data;
if (node == nullptr) {
return;
swap(node->left, node->right);
mirror(node->left);
mirror(node->right);
}
if (node == nullptr) {
return false;
if (node->data == value) {
return true;
};
int main() {
BST tree;
tree.insert(tree.root, 3);
tree.insert(tree.root, 10);
tree.insert(tree.root, 1);
tree.insert(tree.root, 6);
tree.insert(tree.root, 14);
tree.insert(tree.root, 4);
tree.insert(tree.root, 7);
tree.insert(tree.root, 13);
tree.insert(tree.root, 2);
// ii. Find number of nodes in longest path from root
cout << "Height of tree: " << tree.findHeight(tree.root) - 1 << endl; // Subtracting 1 to get the number
of edges
cout << "Minimum value in tree: " << tree.findMin(tree.root) << endl;
// iv. Change a tree so that the roles of the left and right pointers are swapped at every node
tree.mirror(tree.root);
// v. Search a value
int searchValue = 7;
cout << "Value " << searchValue << (tree.search(tree.root, searchValue) ? " found" : " not found") << "
in the tree." << endl;
return 0;