Name – Divyansh Saini
Roll No- 11813001
Section- IT-1
Part 3
Program 1:
1) Construction of Binary tree and insertion of new node in
binary tree.
Code-
#include <iostream>
using namespace std;
class BST
{
int data;
BST *left, *right;
public:
BST();
BST(int);
BST* Insert(BST*, int);
void Inorder(BST*);
};
BST ::BST()
: data(0)
, left(NULL)
, right(NULL)
{
}
BST ::BST(int value)
{
data = value;
left = right = NULL;
}
BST* BST ::Insert(BST* root, int value)
{
if (!root)
{
return new BST(value);
}
if (value > root->data)
{
root->right = Insert(root->right, value);
}
else
{
root->left = Insert(root->left, value);
}
return root;
}
void BST ::Inorder(BST* root)
{
if (!root) {
return;
}
Inorder(root->left);
cout << root->data << endl;
Inorder(root->right);
}
int main()
{
BST b, *root = NULL;
root = b.Insert(root, 40);
b.Insert(root, 30);
b.Insert(root, 20);
b.Insert(root, 50);
b.Insert(root, 70);
b.Insert(root, 60);
b.Insert(root, 10);
b.Insert(root, 15);
b.Inorder(root);
return 0;
}
OUTPUT
2)
Find the no of nodes in the longest path
Code-
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left, *right;
};
Node* getNode(int data)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
void sumOfLongRootToLeafPath(Node* root, int sum,
int len, int& maxLen, int& maxSum)
{
if (!root) {
if (maxLen < len) {
maxLen = len;
maxSum = sum;
} else if (maxLen == len && maxSum < sum)
maxSum = sum;
return;
}
sumOfLongRootToLeafPath(root->left, sum + root->data,
len + 1, maxLen, maxSum);
sumOfLongRootToLeafPath(root->right, sum + root->data,
len + 1, maxLen, maxSum);
}
int sumOfLongRootToLeafPathUtil(Node* root)
{
if (!root)
return 0;
int maxSum = INT_MIN, maxLen = 0;
sumOfLongRootToLeafPath(root, 0, 0, maxLen, maxSum);
return maxLen;
}
int main()
{
Node* root = getNode(4);
root->left = getNode(2);
root->right = getNode(5);
root->left->left = getNode(7);
root->left->right = getNode(1);
root->right->left = getNode(2);
root->right->right = getNode(3);
root->left->right->left = getNode(6);
cout << "No of nodes in the longest path = "
<< sumOfLongRootToLeafPathUtil(root);
return 0;
}
OUTPUT
3)
Minimum data value found in the tree.
Code-
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
struct node* insert(struct node* node, int data)
{
if (node == NULL)
return(newNode(data));
else
{
if (data <= node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
return node;
}
}
int minValue(struct node* node)
{
struct node* current = node;
while (current->left != NULL)
{
current = current->left;
}
return(current->data);
}
int main()
{
struct node* root = NULL;
root = insert(root, 14);
insert(root, 21);
insert(root, 16);
insert(root, 31);
insert(root, 60);
insert(root, 52);
cout << "\n Minimum value in BST is " << minValue(root);
return 0;
}
OUTPUT
4) Search a given value in bst
Code-
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *left, *right;
};
Node* newNode(int key)
{
Node* node = new Node;
node->data = key;
node->left = node->right = nullptr;
return node;
}
Node* insert(Node* root, int key)
{
if (root == nullptr) {
return newNode(key);
}
if (key < root->data) {
root->left = insert(root->left, key);
}
else {
root->right = insert(root->right, key);
}
return root;
}
void searchIterative(Node* root, int key)
{
Node* curr = root;
Node* parent = nullptr;
while (curr != nullptr && curr->data != key)
{
parent = curr;
if (key < curr->data) {
curr = curr->left;
}
else {
curr = curr->right;
}
}
if (curr == nullptr)
{
cout << "Key Not found";
return;
}
if (parent == nullptr) {
cout << "The node with key " << key << " is root node";
}
else if (key < parent->data) {
cout << "The given key is the left node of the node with key "
<< parent->data;
}
else {
cout << "The given key is the right node of the node with key "
<< parent->data;
}
}
int main()
{
int keys[] = { 15, 10, 20, 8, 12, 16, 25 };
Node* root = nullptr;
for (int key: keys) {
root = insert(root, key);
}
searchIterative(root, 25);
return 0;
}
OUTPUT
5) Change a tree so that the roles of the left and right
pointers are swapped at every node
Code-
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode(int data)
{
struct Node* node = (struct Node*)
malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
void mirror(struct Node* node)
{
if (node == NULL)
return;
else
{
struct Node* temp;
mirror(node->left);
mirror(node->right);
temp = node->left;
node->left = node->right;
node->right = temp;
}
}
void inOrder(struct Node* node)
{
if (node == NULL)
return;
inOrder(node->left);
cout << node->data << " ";
inOrder(node->right);
}
int main()
{
struct Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
cout << "Inorder traversal of the constructed"
<< " tree is" << endl;
inOrder(root);
mirror(root);
cout << "\n Inorder traversal of the mirror tree"
<< " is \n";
inOrder(root);
return 0;
}
OUTPUT