0% found this document useful (0 votes)
122 views

DSA Lab 12 Solved

The document contains a student's lab journal from a Data Structures and Algorithms course. It includes: 1) A C++ program to implement various tree operations - insert, search, delete, and traversal functions for a binary tree. 2) Additional functions added - one to swap left and right subtrees and another to count the number of leaves in a binary tree. 3) A further function added to count the number of nodes with only one child. 4) An unanswered question about candies distribution in a classroom.

Uploaded by

hamzaayyub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

DSA Lab 12 Solved

The document contains a student's lab journal from a Data Structures and Algorithms course. It includes: 1) A C++ program to implement various tree operations - insert, search, delete, and traversal functions for a binary tree. 2) Additional functions added - one to swap left and right subtrees and another to count the number of leaves in a binary tree. 3) A further function added to count the number of nodes with only one child. 4) An unanswered question about candies distribution in a classroom.

Uploaded by

hamzaayyub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 12
(Fall 2019)

Course: Data Structure and Algorithms Date: 11-12-2019


Course Code: CEL – 220 Max Marks:
Faculty’s Name: Sir Tahir Iqbal Lab Engineer: Sir Shoaib

Name: Hamza Ayyub Enroll No: 03-135182-006

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

node->right = insert(node->right, val);


}
return node;
}
void inorder(tree *temp)
{
if (temp != NULL)
{
inorder(temp->left);
cout << temp->data << "\t";
inorder(temp->right);
}
}
void preorder(tree *temp)
{
if (temp != NULL)
{
cout << temp->data << "\t";
preorder(temp->left);
preorder(temp->right);
}
}
void postorder(tree *temp)
{
if (temp != NULL)
{

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

tree* FindMin(tree* curr)


{
if (curr->left == NULL)
return curr;
else
return FindMin(curr->left);
}

void DeleteNode(tree* temp, tree* root, int num)


{
if (temp == NULL)
cout << "Number not Found";
else if ((temp->data == num))
{
tree *parent, *min;
int number;
if ((temp->left == NULL) && (temp->right == NULL))
{
parent = getParent(root, temp->data);
if (parent->left == temp)
parent->left = NULL;
else if (parent->right == temp)
parent->right = NULL;

delete temp;
}
else if (((temp->left == NULL) && (temp->right != NULL)) || ((temp->left !=
NULL)
&& (temp->right == NULL)))
{

parent = getParent(root, temp->data);


if (temp->left != NULL){

if (parent->left == temp)

parent->left = temp->left;

else if (parent->right == temp)

parent->right = temp->left;

}
else if (temp->right != NULL){
if (parent->left == temp)
parent->left = temp->right;

else if (parent->right == temp)

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

node->right = insert(node->right, val);


}
return node;
}
void inorder(tree *temp)
{
if (temp != NULL)
{
inorder(temp->left);
cout << temp->data << "\t";
inorder(temp->right);
}
}
int leafcount( struct tree* node)
{
if (node == NULL)
return 0;
if (node->left == NULL && node->right == NULL)
return 1;
else
return leafcount(node->left) +
leafcount(node->right);
}
void swap(tree **Left, tree **Right)
{
tree * temp = *Left;
*Left = *Right;
*Right= temp;
}
tree **first;
tree **second;
void Swaping_subtree(tree **cur, int &count)
{

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

node->right = insert(node->right, val);


}
return node;
}

int leaf_count(tree *node)


{

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

node->right = insert(node->right, val);


}
return node;
}
int Monk(tree *node, int k)
{
int count = 0;
int small;
tree *curr = root;
while (curr != NULL)
{
if (curr->left == NULL)
{
count++;
if (count == k)
small = curr->data;
curr = curr->right;
}
else
{

tree *pre = curr->left;


while (pre->right != NULL && pre->right !=curr)
pre = pre->right;
if (pre->right == NULL)
{

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

node->right = insert(node->right, val);


}
return node;
}
void inorder(tree *temp)
{
if (temp != NULL)
{
inorder(temp->left);
cout << temp->data << "\t";
inorder(temp->right);
}
}
int K_small(tree *node, int &c, int &k)
{
if (!node)
return 0;
int left = K_small(root->left, c, k);
if (left)
return left;
c= c + 1;
if (c == k)
return node->data;
K_small(root->right, c, k);
}

int main()
{

cout << "\t\t Inorder " << endl;


cout << endl;
int c = 0;
int k;
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 Small Number In binary tree" << K_small(root, k, c) << endl;
system("pause");
return 0;
}

You might also like