0% found this document useful (0 votes)
10 views10 pages

3 Adsl

The document contains code and explanations for three assignments on binary search trees: 1) Creating a BST, finding its height, leaf nodes, and generating its mirror image. 2) Constructing a BST and finding its minimum and maximum values. 3) Creating a BST and finding the inorder successor and predecessor of a given node. Pseudocode and functions are provided to insert nodes, find height, print leaf nodes, generate mirror images, and find minimum, maximum, inorder successor and predecessor values. Sample outputs are listed but not shown.

Uploaded by

sohamkawadkar31
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)
10 views10 pages

3 Adsl

The document contains code and explanations for three assignments on binary search trees: 1) Creating a BST, finding its height, leaf nodes, and generating its mirror image. 2) Constructing a BST and finding its minimum and maximum values. 3) Creating a BST and finding the inorder successor and predecessor of a given node. Pseudocode and functions are provided to insert nodes, find height, print leaf nodes, generate mirror images, and find minimum, maximum, inorder successor and predecessor values. Sample outputs are listed but not shown.

Uploaded by

sohamkawadkar31
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/ 10

Name- Soham Kawadkar Div: SY-9 Roll no.

- 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) {}
};

// Function to insert a new node in BST


Node* insert(Node* root, int data) {
if (root == nullptr) {
return new Node(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}

// Function to find height of the tree


int height(Node* root) {
if (root == nullptr) {
return 0;
}
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return max(leftHeight, rightHeight) + 1;
}
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A

// Function to print leaf nodes


void printLeafNodes(Node* root) {
if (root == nullptr) {
return;
}
if (root->left == nullptr && root->right == nullptr) {
cout << root->data << " ";
return;
}
printLeafNodes(root->left);
printLeafNodes(root->right);}
// Function to mirror a tree
Node* mirror(Node* root) {
if (root == nullptr) {
return nullptr; }
Node* temp = root->left;
root->left = mirror(root->right);
root->right = mirror(temp);
return root;}

// Function to print level-wise


void printLevelWise(Node* root) {
if (root == nullptr) {
return;
}
queue<Node*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
Node* current = q.front();
q.pop();
cout << current->data << " ";
if (current->left) {
q.push(current->left);
}
if (current->right) {
q.push(current->right);
}
}
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A

cout << endl;


}}
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 << "Height of the tree: " << height(root) << endl;
cout << "Leaf nodes: ";
printLeafNodes(root);
cout << endl;

cout << "Original tree:" << endl;


printLevelWise(root);

Node* mirrorRoot = mirror(root);


cout << "Mirror image:" << endl;
printLevelWise(mirrorRoot);

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) {}
};

// Function to insert a new node in BST


Node* insert(Node* root, int data) {
if (root == nullptr) {
return new Node(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}

// Function to find minimum value in BST


int findMin(Node* root) {
if (root == nullptr) {
return INT_MAX;
}
if (root->left == nullptr) {
return root->data;
}
return findMin(root->left);
}

// Function to find maximum value in BST


int findMax(Node* root) {
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A

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) {}
};

// Function to insert a new node in BST


Node* insert(Node* root, int data) {
if (root == nullptr) {
return new Node(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}

// Function to find inorder successor


Node* findInorderSuccessor(Node* root, Node* target) {
if (root == nullptr) {
return nullptr;
}
if (target->right != nullptr) {
Node* current = target->right;
while (current->left != nullptr) {
current = current->left;
}
return current;
}
Node* successor = nullptr;
while (root != nullptr) {
if (target->data < root->data) {
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A

successor = root;
root = root->left;
} else if (target->data > root->data) {
root = root->right;
} else {
break;
}
}
return successor;
}

// Function to find inorder predecessor


Node* findInorderPredecessor(Node* root, Node* target) {
if (root == nullptr) {
return nullptr;
}
if (target->left != nullptr) {
Node* current = target->left;
while (current->right != nullptr) {
current = current->right;
}
return current;
}
Node* predecessor = nullptr;
while (root != nullptr) {
if (target->data > root->data) {
predecessor = root;
root = root->right;
} else if (target->data < root->data) {
root = root->left;
} else {
break;
}
}
return predecessor;
}

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

You might also like