DSA Lab 12 Solved
DSA Lab 12 Solved
Question # 1:
Write a program in C++ to implement Tree; it should have the following listed
functionalities within:
An insert( ? ) function to insert values in a Binary Tree.
A search( ) function to find maximum number from Binary Tree.
Delete Operation in BST.
In-Order Traversal in Tree
Pre-Order Traversal in BST.
Post-Order Traversal In Tree.
Code:
#include<iostream>
using namespace std;
struct tree
{
int data;
tree *left;
tree *right;
};
tree *root = NULL;
tree* insert(tree *node, int val)
{
if (node == NULL)
{
tree *temp = new tree;
temp->data = val;
temp->left = NULL;
temp->right = NULL;
node = temp;
}
else if (val < node->data)
{
node->left = insert(node->left, val);
}
else if (val >node->data)
{
postorder(temp->left);
postorder(temp->right);
cout << temp->data << "\t";
}
}
void search(tree *node, int val)
{
if (node == NULL)
{
cout << "Numer not found" << endl;
}
else if (node->data == val)
{
cout << "Number Found" << endl;
}
else if (node->data > val)
{
search(node->left, val);
}
else if (node->data < val)
{
search(node->right, val);
}
}
tree* getParent(tree *cur, int num)
{
if ((cur->left->data == num) || (cur->right->data == num))
return cur;
else if (num < cur->data)
return getParent(cur->left, num);
else
return getParent(cur->right, num);
}
delete temp;
}
else if (((temp->left == NULL) && (temp->right != NULL)) || ((temp->left !=
NULL)
&& (temp->right == NULL)))
{
if (parent->left == temp)
parent->left = temp->left;
parent->right = temp->left;
}
else if (temp->right != NULL){
if (parent->left == temp)
parent->left = temp->right;
parent->right = temp->right;
}
delete temp;
}
else if ((temp->left != NULL) && (temp->right != NULL))
{
min = FindMin(temp->right);
number = min->data;
DeleteNode(temp, root, min->data);
temp->data = number;
}
}
else if (num < temp->data)
DeleteNode(temp->left, root, num);
else
DeleteNode(temp->right, root, num);
}
int main()
{
int num;
cout << "\t\t Inorder " << endl;
cout << endl;
root = insert(root, 5);
insert(root, 4);
insert(root, 3);
insert(root, 6);
insert(root, 1);
insert(root, 8);
insert(root, 2);
insert(root, 15);
inorder(root);
cout << endl;
cout << "\t\t Preorder" << endl;
cout << endl;
preorder(root);
cout << endl;
cout << "\t\t Postorder" << endl;
cout << endl;
postorder(root);
cout << endl;
cout << "\t\tEnter Searching Number" << endl;
cin >> num;
search(root, num);
cout << endl;
cout << "\t\tEnter Number for Delete" << endl;
cin >> num;
DeleteNode(root, root, num);
cout << endl;
cout << "\t\t Inorder" << endl;
inorder(root);
cout << endl;
cout << "\t\t Preorder" << endl;
cout << endl;
preorder(root);
cout << endl;
cout << "\t\t Postorder" << endl;
cout << endl;
postorder(root);
cout << endl;
system("pause");
return 0;
}
Output:
Question # 2:
a.) Write a function, swap subtrees, that swaps all of the left and right sub-trees of a binary
tree.
b.) Write a function, leavesCount, which takes as a parameter a pointer to the root node of a
binary tree and returns the number of leaves in a binary tree.
Code:
#include<iostream>
using namespace std;
struct tree
{
int data;
tree *left;
tree *right;
};
tree *root = NULL;
tree* insert(tree *node, int val)
{
if (node == NULL)
{
tree *temp = new tree;
temp->data = val;
temp->left = NULL;
temp->right = NULL;
node = temp;
}
else if (val < node->data)
{
node->left = insert(node->left, val);
}
else if (val >node->data)
{
if (!(*cur))
return;
if (!(*cur)->left&&!(*cur)->right)
{
second = cur;
count++;
if (count % 2 == 0)
swap(first, second);
else
first = second;
}
if ((*cur)->left)
Swaping_subtree(&(*cur)->left, count);
if ((*cur)->right)
Swaping_subtree(&(*cur)->right, count);
}
int main()
{
int i = 0;
cout << "\t\t Inorder " << endl;
cout << endl;
root = insert(root, 5);
insert(root, 4);
insert(root, 3);
insert(root, 6);
insert(root, 1);
insert(root, 8);
insert(root, 2);
insert(root, 15);
inorder(root);
cout << endl;
Swaping_subtree(&root, i);
inorder(root);
cout << endl;
cout << "\t\tLeaf in this tree\t\t" <<leafcount(root)<<endl;
system("pause");
return 0;
}
Output:
Question # 3:
Extending your program further, now write a function, single Parent, which returns the number
of nodes in a binary tree that have only one child.
Code:
#include<iostream>
using namespace std;
struct tree
{
int data;
tree *left;
tree *right;
};
tree *root = NULL;
tree* insert(tree *node, int val)
{
if (node == NULL)
{
tree *temp = new tree;
temp->data = val;
temp->left = NULL;
temp->right = NULL;
node = temp;
}
else if (val < node->data)
{
node->left = insert(node->left, val);
}
else if (val >node->data)
{
if (node== NULL)
return 0;
int temp = 0;
if ((root->left == NULL && root->right != NULL) ||(root->left != NULL && root-
>right == NULL))
temp++;
temp+= (leaf_count(root->left) +leaf_count(root->right));
return temp;
}
int main()
{
root = insert(root, 5);
insert(root, 6);
insert(root, 14);
insert(root, 1);
insert(root, 0);
insert(root, 12);
cout << "Node which have one child only" << leaf_count(root) << endl;
system("pause");
return 0;
}
Question # 4:
Ahmed is standing at the door of his classroom. There are currently N students in the class, ith
student got Ai candies.There are still M more students to come. At every instant, a student
enters the class and wishes to be seated with a student who has exactly the same number of
candies. For each student, Monk shouts YES if such a student is found, NO otherwise.
Input:
First line contains an integer T. T test cases follow.
First line of each case contains two space-separated integers N and M.
Second line contains N + M space-separated integers, the candies of the students.
Output:
For each test case, output M new line, Monk's answer to the M students.
Print "YES" (without the quotes) or "NO" (without the quotes) pertaining to the Monk's answer.
Constraints:
1 ≤ T ≤ 10
1 ≤ N, M ≤ 105
0 ≤ Ai ≤ 1012
Code:
#include<iostream>
using namespace std;
struct tree
{
int data;
tree *left;
tree *right;
};
tree *root = NULL;
tree* insert(tree *node, int val)
{
if (node == NULL)
{
tree *temp = new tree;
temp->data = val;
temp->left = NULL;
temp->right = NULL;
node = temp;
}
else if (val < node->data)
{
node->left = insert(node->left, val);
}
else if (val >node->data)
{
pre->right = curr;
curr = curr->left;
}
else
{
pre->right = NULL;
count++;
if (count == k)
{
small = curr->data;
curr = curr->right;
}
}
return small;
}
int main()
{
int candies=0, k=0;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
if (Monk(root, k) > candies)
{
cout << "Yes" << Monk(root, k) << "\t";
}
else
cout << "NO" << "\t";
system("pause");
return 0;
Question # 5:
Find the Kth smallest element in the binary search tree.
Code:
#include<iostream>
using namespace std;
struct tree
{
int data;
tree *left;
tree *right;
};
tree *root = NULL;
tree* insert(tree *node, int val)
{
if (node == NULL)
{
tree *temp = new tree;
temp->data = val;
temp->left = NULL;
temp->right = NULL;
node = temp;
}
else if (val < node->data)
{
node->left = insert(node->left, val);
}
else if (val >node->data)
{
int main()
{