3 Adsl
3 Adsl
3 Adsl
- 22230143 A
ASSIGNMENT 3 ADSL
a) Create Binary Search Tree(BST).Find height of the tree and print leaf nodes. Find
mirror image, print original and mirror image using level-wise printing.
CODE:
#include <iostream>
#include <queue>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
cout << "Height of the tree: " << height(root) << endl;
cout << "Leaf nodes: ";
printLeafNodes(root);
cout << endl;
return 0;
}
OUTPUT:
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A
b) Construct Binary Search Tree and find the min and max value of BST.
CODE:
#include <iostream>
#include <climits>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
if (root == nullptr) {
return INT_MIN;
}
if (root->right == nullptr) {
return root->data;
}
return findMax(root->right);
}
int main() {
Node* root = nullptr;
root = insert(root, 5);
insert(root, 3);
insert(root, 7);
insert(root, 2);
insert(root, 4);
insert(root, 6);
insert(root, 8);
cout << "Minimum value in BST: " << findMin(root) << endl;
cout << "Maximum value in BST: " << findMax(root) << endl;
return 0;
}
OUTPUT:
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A
c) Create a BST and find inorder successor and inorder predecessor of specific node.
CODE:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
successor = root;
root = root->left;
} else if (target->data > root->data) {
root = root->right;
} else {
break;
}
}
return successor;
}
int main() {
Node* root = nullptr;
root = insert(root, 5);
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A
insert(root, 3);
insert(root, 7);
insert
OUTPUT:
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A
FAQs
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A