DSA - Unit 4 - Lecture 28 Coding
DSA - Unit 4 - Lecture 28 Coding
admin.lpucolab438.examly.io/course
No. of Sections: 1
No. of Questions: 14
Total Duration: 90 min
Section 1 - Coding
Section Summary
No. of Questions: 14
Duration: 90 min
Additional Instructions:
None
Q1.
Problem Statement
You need to implement a program to work with a Binary Search Tree (BST). The program
should provide the following functionalities:
Create a BST: The user will provide the number of nodes n to be inserted into the
BST. Then, the user will enter n integer values that should be inserted into the BST.
After inserting the values, the program should display a message indicating that the
BST with n nodes is ready to use.
Inorder Traversal: The program should perform an inorder traversal of the BST
and display the nodes in ascending order.
Preorder Traversal: The program should perform a preorder traversal of the BST
and display the nodes.
1/95
Postorder Traversal: The program should perform a postorder traversal of the BST
and display the nodes.
Implement the necessary functions and handle any possible error conditions.
Input Format
The first input consists of the choice.
If the choice is 1, enter the number of elements (n) and the space-separated elements (i)
to be inserted into the tree.
(By default, all the test cases have all the choices from 1-5, but the n and i values keep
changing)
Output Format
The output prints the results based on the choice. Depending on the choice made by the
user, the program should output the appropriate messages or display the nodes of the
BST.
Constraints
The BST can have duplicate elements.
2/95
The integer values can be in the range of 1 to102.
1
5
12 78 96 34 55
2
3
4
5
1
9
7 9 6 3 2 1 4 5 8
2
4
5
6
5
Wrong choice
Akil is studying binary trees and wants to create a program that allows him to build a
binary tree from user input and then calculate its height. He would like to implement the
binary tree using a linked representation where each node contains an integer value. Akil
wants to practice his coding skills and optimize the code to handle large inputs efficiently.
3/95
Help Akil by designing a program that fulfills his requirements.
Input Format
The input consists of a sequence of integers, where each integer represents a node value
in the binary tree.
The input terminates when Akil enters a value of -1, which indicates the end of the input
sequence.
Output Format
The output displays a single integer, which represents the height of the binary tree.
Constraints
The input integers will be non-negative, except for the termination value of -1.
Each node value will fit within the range of a 32-bit signed integer.
1
2
3
4
-1
6
3
1
4
2
9
7
8
10
12
15
14
-1
4/95
Problem Statement
Nirmala is interested in analyzing binary trees. She wants to write a program that takes a
series of positive integers as input and constructs a binary tree from them. After
constructing the tree, she needs to find the largest element present in the tree.
Nirmala seeks your help in writing the code for this task.
Input Format
The input consists of a series of positive integers, terminated by a negative integer (-1).
Each positive integer represents a node value that should be inserted into the tree.
The negative integer (-1) marks the end of the input and should not be included in the
tree.
Output Format
The output displays a single line containing the value of the largest element in the
constructed tree.
Constraints
All input integers (except for the terminating -1) will be positive integers.
1
2
3
4
-1
Maximum element is 4
5/95
6
3
1
4
2
9
7
8
10
12
15
14
-1
Maximum element is 15
Imagine you are working on a database management system for a hospital. The system
stores patient records in a binary search tree, where each node represents a patient and
the key values are based on some attribute (e.g., patient ID, age, or medical condition).
Your task is to write a program that finds the kth smallest patient based on the chosen
attribute.
50
/ \
30 70
/ \ / \
20 40 60 80
In this tree, each node represents a patient ID. For example, patient IDs 20, 30, 40, 50,
60, 70, and 80 are stored in the tree in ascending order. Your program should be able to
find the kth-smallest patient ID when given a value for k. For instance, if k = 3, the
program should return 40 as the 3rd-smallest patient ID.
Input Format
The input consists of a series of positive integers (greater than zero) separated by space.
6/95
The input ends when a value (-1) is entered.
The integers represent the elements to be inserted into the binary search tree.
Output Format
After inserting all the elements into the binary search tree, the program should take an
additional input k representing the k-th smallest element to be found in the BST.
Constraints
The input integers will be positive and greater than zero.
The number of elements in the binary search tree will be at most 1000.
20
30
40
50
60
70
80
-1
3
1
2
3
4
5
-1
2
7/95
Imagine you are working on a library management system that stores information about
books in a binary search tree. Each node in the tree represents a book, and the key
values are based on the ISBN (International Standard Book Number) of the books. Your
task is to write a program that searches for a book based on its ISBN.
50
/ \
30 70
/\ / \
20 40 60 80
In this tree, each node represents a book's ISBN. The books are stored in such a way
that the left subtree of any node contains ISBNs smaller than the node's ISBN, and the
right subtree contains ISBNs greater than the node's ISBN. For example, in the given
tree, ISBN 30 is located in the left subtree of the root node, while ISBN 70 is located in
the right subtree.
Your program should be able to search for a given ISBN in the tree and return whether or
not it exists in the database. For instance, if the program is given ISBN 40, it should
return a "found" message indicating that the book is present in the database.
Input Format
The input consists of a series of positive integers (greater than zero) separated by space.
The integers represent the elements to be inserted into the binary search tree.
Output Format
After inserting all the elements into the binary search tree, the program should take an
additional input representing the key to be searched in the BST.
If the key is present in the binary search tree, print <key> is present in the BST.
8/95
If the key is not present in the binary search tree, print <key> is not present in the BST.
Constraints
The input integers will be positive and greater than zero.
The number of elements in the binary search tree will be at most 100.
50
30
70
20
40
60
-1
30
6
3
1
2
4
-1
10
Ragul wants to build a binary search tree (BST) and perform a key search operation on it.
He needs your help to accomplish this. Write a program that helps Ragul create a BST
and search for a specific key within it.
Input Format
The first line of input consists of the number of nodes n.
9/95
The second line of input consists of n uniquevalues for nodes, separated by a space.
Output Format
The output displays one of the following messages based on whether the key is found in
the binary search tree or not in the following format:
1. If the key is found in the binary search tree, print "The key <<key value>> is found
in the binary search tree"
2. If the key is not found in the binary search tree, print "The key <<key value>> is
not found in the binary search tree"
Constraints
1 ≤ numNodes ≤ 10
7
101 102 103 105 106 108 110
102
7
101 102 103 105 106 108 110
115
Preethi is fascinated by trees, especially binary search trees. She wants to create a
binary search tree (BST) of characters and perform deletion operations on it. She wants
you to help her write a program.
10/95
Preethi needs a program that allows her to:
Note
1. Each character inserted into the binary search tree is a number, special character,
lowercase letter, or uppercase letter. The characters are inserted based on their
respective ASCII value.
2. All inserted characters into the binary search tree will be unique.
Input Format
The first line of input consists of the number of characters N.
Output Format
The output displays the in-order traversal of the given inputs after deleting the character
M.
Constraints
1 ≤ N ≤ 50
5
Z E W T Y
Y
E T W Z
11/95
7
A 2 B b # c D
#
2 A B D b c
Kumar is studying data structures and is currently learning about Binary Search Trees
(BST). He wants to implement the operations of creating and displaying a Binary Search
Tree.
Write a program that takes a sequence of positive integers as input and constructs a
Binary Search Tree using these integers. After constructing the BST, the program should
display the nodes of the BST in ascending order.
Input Format
The input consists of a series of positive integers greater than zero, separated by a
space.
The integers represent the elements to be inserted into the binary search tree.
Output Format
The output displays the elements of the binary search tree in ascending order (sorted
order), separated by space.
Constraints
The input integers > 0.
The number of elements in the binary search tree will be at most 100.
1
2
3
4
5
-1
12/95
1 2 3 4 5
7
6
4
5
3
-1
3 4 5 6 7
Josh is fascinated by binary trees, and he has learned about inserting elements into a
binary search tree and performing pre-order traversal. He decided to write a program to
practice these concepts. Help Josh write a program that inserts elements into a binary
search tree (BST) and performs a pre-order traversal.
Input Format
The first line of input contains an integer t, representing the number of elements Josh
wants to insert into the BST.
The following t lines each contain t integer values representing the data to be inserted
into the BST, separated by a space.
Output Format
The output displays a single line with space-separated integers representing the pre-
order traversal of the BST.
Constraints
1 ≤ t ≤ 100
5
10 5 15 3 7
10 5 3 7 15
13/95
7
8 3 10 1 6 14 4
8 3 1 6 4 10 14
Revi is fascinated by binary search trees (BSTs) and wants to perform some operations
on them. He wishes to construct a binary search tree from a series of positive integers
and calculate the sum of all the nodes in the BST.
Write a program to help Revi implement these operations using the BST concept.
Input Format
The input consists of a series of positive integers (greater than zero) separated by a
space.
The integers represent the elements to be inserted into the binary search tree.
Output Format
The output displays an integer value, which represents the sum of all the nodes in the
BST.
Constraints
The input integers will be positive and greater than zero.
The number of elements in the binary search tree will be at most 100.
5
3
7
2
4
9
-1
14/95
Sample Input Sample Output
6
3
1
4
2
-1
Kathir is learning about binary search trees (BSTs) in his computer science class. He has
just implemented a simple program to create a binary search tree and calculate the sum
of all its leaf nodes. Can you help him validate his code and solve some scenarios to test
it?
/\
3 7
/\ \
2 4 9
In this tree, the leaf nodes are 2, 4, and 9. The program should compute the sum of these
leaf node elements, which is 2 + 4 + 9 = 15.
Input Format
The input consists of a series of positive integers as input, each on a separate line.
The input terminates when Kathir enters -1. This signifies the end of input.
Output Format
The output displays the sum of all the leaf nodes in the binary search tree in the following
format:
15/95
"Sum of all leaf nodes are <<value>>"
Constraints
All input integers will be positive integers (greater than 0).
5
3
7
2
4
9
-1
6
3
1
4
2
-1
Gayu is studying data structures, and she wants to implement a program that performs
various operations on a Binary Search Tree (BST).
Gayu to create a Binary Search Tree (BST) and perform the following operations:
1. Create BST: Gayu can create a BST by providing the number of nodes (n) and a list
of n integer values. The program constructs the BST using these values.
16/95
2. Print Postorder: Gayu can print the elements of the BST in postorder traversal,
which means that the program will display the values in the left subtree, then the
right subtree, and finally the root of each subtree.
3. Exit: Gayu can exit the program.
Input Format
The first input consists of the choice.
If the choice is 1, enter the number of elements (n) and the space-separated elements (i)
to be inserted into the tree.
(By default, all the test cases have all the choices from 1–3, but the n and i values keep
changing.)
Output Format
The first line of output consists of the following format:
"BST with %d nodes is ready to use!\n", where %d is the number of nodes in the
BST.
"Wrong choice\n"
Constraints
17/95
The BST can have duplicate elements.
1
5
12 78 96 34 55
2
3
1
9
7 9 6 3 2 1 4 5 8
2
5
3
Kamal is developing a program that finds the Kth smallest element in a Binary Search
Tree (BST). He plans to use the Morris Traversal technique to optimize the search
process. Your task is to assist Kamal in verifying and testing his code.
Input Format
18/95
The first n lines of input consist of a series of positive integers (greater than zero)
separated by space.
The last input of an integer represents the elements to be inserted into the binary search
tree.
Output Format
After inserting all the elements into the binary search tree, the program should take an
additional input k representing the k-th smallest element to be found in the BST.
Constraints
The input integers will be positive and greater than zero.
The number of elements in the binary search tree will be at most 1000.
20
30
40
50
60
70
80
-1
3
1
2
3
4
5
-1
2
19/95
Problem Statement
Arun is developing a program that uses a Binary Search Tree (BST) to manage a
collection of integers. He has written the code to create a BST and search for elements
within it, but he needs your assistance in testing its functionality.
You are tasked with helping Arun by designing a program that performs the following
actions:
Input Format
The first n lines of input consist of a series of positive integers (greater than zero)
separated by space.
The last line of input consists of an integer representing the elements to be inserted into
the binary search tree.
Output Format
The output displays one of the following messages:
If the key is present in the binary search tree, print <key> is present in the BST.
If the key is not present in the binary search tree, print <key> is not present in the BST.
Constraints
The input integers will be positive and greater than zero.
The number of elements in the binary search tree will be at most 100.
20/95
50
30
70
20
40
60
-1
30
6
3
1
2
4
-1
10
Section 1 - Coding
Q1 Test Case Input Output
1
12
123 456 789 987 654 321 147 258 369 741 852 963
2
3
4
5
1
6
18 98 77 34 55 26
2
3
4
5
21/95
BST with 6 nodes is ready to use!
BST Traversal in INORDER
18 26 34 55 77 98
BST Traversal in PREORDER
18 98 77 34 26 55
BST Traversal in POSTORDER
26 55 34 77 98 18
1
10
789 987 654 321 147 258 369 741 852 963
2
3
4
5
1
6
12 23 45 56 78 89
2
3
4
5
1
12
12 23 45 56 78 89 98 87 65 54 32 21
2
3
4
5
22/95
BST with 12 nodes is ready to use!
BST Traversal in INORDER
12 21 23 32 45 54 56 65 78 87 89 98
BST Traversal in PREORDER
12 23 21 45 32 56 54 78 65 89 87 98
BST Traversal in POSTORDER
21 32 54 65 87 98 89 78 56 45 23 12
1
5
12 78 96 34 55
2
3
4
5
1
9
7 9 6 3 2 1 4 5 8
2
4
5
6
5
Wrong choice
Solution
23/95
#include <iostream>
#include<stdlib.h>
using namespace std;
struct tnode
{
int data;
struct tnode *right;
struct tnode *left;
};
int main()
{
struct tnode *root = NULL;
int choice, item, n, i;
do
{
cin>>choice;
switch(choice)
{
case 1:
root = NULL;
cin>>n;
for(i = 1; i <= n; i++)
{
cin>>item;
root = CreateBST(root,item);
}
cout<<"BST with "<<n<<" nodes is ready to use!"<<"\n";
break;
case 2:
cout<<"BST Traversal in INORDER"<<"\n";
Inorder(root);
cout<<"\n";
break;
case 3:
cout<<"BST Traversal in PREORDER"<<"\n";
Preorder(root);
cout<<"\n";
break;
case 4:
cout<<"BST Traversal in POSTORDER"<<"\n";
Postorder(root);
cout<<"\n";
break;
case 5:
exit(0);
break;
default:
cout<<"Wrong choice"<<"\n";
break;
}
24/95
} while(1);
return 0;
}
return(root);
}
}
25/95
Q2 Test Case Input Output
1
2
3
-1
1
-1
1
4
7
6
8
9
3
2
1
-1
56
32
14
9
-1
3
9
7
5
6
4
32
12
96
-1
26/95
Weightage - 25 Input Output
1
2
3
4
5
6
7
8
9
13
15
16
14
-1
1
2
3
4
-1
6
3
1
4
2
9
7
8
10
12
15
14
-1
Solution
27/95
#include <iostream>
using namespace std;
struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;
void append(int d) {
TreeNode* newnode = new TreeNode(d);
if (head == nullptr) {
head = newnode;
} else {
TreeNode* temp = head;
while (true) {
if (temp->left != nullptr) {
temp = temp->left;
} else {
temp->left = newnode;
break;
}
}
}
}
deleteTree(node->left);
deleteTree(node->right);
delete node;
}
int main() {
int d;
do {
28/95
cin >> d;
if (d > 0)
append(d);
} while (d != -1);
int h = height(head);
cout << "Height of the tree is " << h;
deleteTree(head);
head = nullptr;
return 0;
}
1
2
3
-1
Maximum element is 3
1
-1
Maximum element is 1
1
4
7
6
8
9
3
2
1
-1
Maximum element is 9
56
32
14
9
-1
Maximum element is 56
29/95
3
9
7
5
6
4
32
12
96
-1
Maximum element is 96
1
2
3
4
5
6
7
8
9
13
15
16
14
-1
Maximum element is 16
1
2
3
4
-1
Maximum element is 4
30/95
6
3
1
4
2
9
7
8
10
12
15
14
-1
Maximum element is 15
Solution
31/95
#include <iostream>
#include <climits>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
void append(int d)
{
struct node* newnode = new node;
newnode->data = d;
newnode->left = NULL;
newnode->right = NULL;
if (head == NULL)
{
head = newnode;
}
else
{
struct node* temp = head;
while (temp->left != NULL || temp->right != NULL)
{
if (temp->left != NULL)
{
temp = temp->left;
}
else
{
temp = temp->right;
}
}
if (temp->left == NULL)
temp->left = newnode;
else
temp->right = newnode;
}
}
32/95
maxVal = max(maxVal, max(leftMax, rightMax));
return maxVal;
}
int main()
{
int d;
do
{
cin >> d;
if (d > 0)
append(d);
} while (d != -1);
return 0;
}
50
30
20
40
70
60
80
-1
4
50
30
20
40
70
60
80
-1
6
33/95
5
3
7
2
4
9
-1
4
6
3
2
1
4
7
8
9
10
-1
2
6
3
2
1
4
7
8
9
10
-1
9
6
3
2
1
-1
4
34/95
20
30
40
50
60
70
80
-1
3
1
2
3
4
5
-1
2
Solution
35/95
#include<stdio.h>
using namespace std;
struct Node
{
int data;
Node *left,*right;
};
void append(Node **rootadd,int data)
{
Node *temp,*newnode;
temp = *rootadd;
newnode = new Node();
newnode->left = NULL;
newnode->data = data;
newnode->right = NULL;
if(*rootadd == NULL)
*rootadd = newnode;
else
{
while(1)
{
if(data > temp->data)
{
if(temp->right != NULL)
{
temp = temp->right;
}
else
{
temp->right = newnode;
break;
}
}
else
{
if(temp->left != NULL)
{
temp = temp->left;
}
else
{
temp->left = newnode;
break;
}
}
}
}
}
int KSmallestUsingMorris(Node *root, int k)
{
int count = 0;
int ksmall = -1;
Node *curr = root;
while (curr != NULL)
{
if (curr->left == NULL)
36/95
{
count++;
if (count==k)
ksmall = curr->data;
curr = curr->right;
}
else
{
Node *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)
ksmall = curr->data;
curr = curr->right;
}
}
}
return ksmall;
}
int main()
{
Node *root = NULL;
int data,k;
do
{
sacnf("%d",&data);
if(data > 0)
append(&root,data);
}while(data > 0);
cin>>k;
printf("Smallest kth value %d",KSmallestUsingMorris(root,k));
return 0;
}
37/95
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *left,*right;
};
void append(Node **rootadd,int data)
{
Node *temp,*newnode;
temp = *rootadd;
newnode = new Node();
newnode->left = NULL;
newnode->data = data;
newnode->right = NULL;
if(*rootadd == NULL)
*rootadd = newnode;
else
{
while(1)
{
if(data > temp->data)
{
if(temp->right != NULL)
{
temp = temp->right;
}
else
{
temp->right = newnode;
break;
}
}
else
{
if(temp->left != NULL)
{
temp = temp->left;
}
else
{
temp->left = newnode;
break;
}
}
}
}
}
int KSmallestUsingMorris(Node *root, int k)
{
int count = 0;
int ksmall = -1;
Node *curr = root;
while (curr != NULL)
{
38/95
if (curr->left == NULL)
{
count++;
if (count==k)
ksmall = curr->data;
curr = curr->right;
}
else
{
Node *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)
ksmall = curr->data;
curr = curr->right;
}
}
}
return ksmall;
}
int main()
{
Node *root = NULL;
int data,k;
do
{
cin>>data;
if(data > 0)
append(&root,data);
}while(data > 0);
cin>>k;
cout<<"Smallest kth value "<<KSmallestUsingMorris(root,k);
return 0;
}
6
3
1
2
4
-1
4
39/95
Weightage - 10 Input Output
6
3
1
2
4
-1
3
6
3
2
1
4
10
9
8
7
15
-1
10
6
3
2
1
4
10
9
8
7
15
-1
14
40/95
6
3
2
1
4
10
9
8
7
15
-1
7
6
3
2
1
4
10
9
8
7
15
-1
16
50
30
70
20
40
60
-1
30
6
3
1
2
4
-1
10
41/95
Solution
42/95
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*left;
struct node*right;
};
struct node*root;
void append(int d)//3
{
struct node*newnode = (struct node*)malloc(sizeof(struct node));
struct node*temp = root;//6
newnode->data = d;//3
newnode->left = NULL;
newnode->right = NULL;
if(root == NULL)//6
{
root = newnode;//6
}
else
{
while(true)
{
if(d < temp->data)
{
if(temp->left != NULL)
{
temp = temp->left;
}
else
{
temp->left = newnode;//6->3
break;
}
}else
{
if(temp->right != NULL)
{
temp = temp->right;
}else
{
temp->right = newnode;
break;
}
}
}
}
}
bool iterativesearch(struct node*root,int key)
{
while(root != NULL)//10
{
if(key > root->data)
{
root = root->right;//10
43/95
}
else if(key < root->data)
{
root = root->left;
}
else
return true;
}
return false;
}
int main()
{
int d,search;
do
{
scanf("%d",&d);//3
if(d > 0)
append(d);
}while(d != -1);//-1
scanf("%d",&search);
if(iterativesearch(root,search))
cout<<search<<" is present in the BST";
else
cout<<search<<" is not present in the BST";
return 0;
}
44/95
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
struct node*left;
struct node*right;
};
struct node*root;
void append(int d)//3
{
struct node*newnode = (struct node*)malloc(sizeof(struct node));
struct node*temp = root;//6
newnode->data = d;//3
newnode->left = NULL;
newnode->right = NULL;
if(root == NULL)//6
{
root = newnode;//6
}
else
{
while(true)
{
if(d < temp->data)
{
if(temp->left != NULL)
{
temp = temp->left;
}
else
{
temp->left = newnode;//6->3
break;
}
}else
{
if(temp->right != NULL)
{
temp = temp->right;
}else
{
temp->right = newnode;
break;
}
}
}
}
}
bool iterativesearch(struct node*root,int key)
{
while(root != NULL)//10
{
if(key > root->data)
{
45/95
root = root->right;//10
}
else if(key < root->data)
{
root = root->left;
}
else
return true;
}
return false;
}
int main()
{
int d,search;
do
{
cin>>d;//3
if(d > 0)
append(d);
}while(d != -1);//-1
cin>>search;
if(iterativesearch(root,search))
cout<<search<<" is present in the BST";
else
cout<<search<<" is not present in the BST";
return 0;
}
5
60 40 80 30 50
14
5
60 40 80 30 50
50
9
45 25 65 15 35 55 75 5 95
45
46/95
9
45 25 65 15 35 55 75 5 95
100
10
100 50 150 25 75 125 175 60 74 85
150
10
100 50 150 25 75 125 175 60 74 85
15
7
101 102 103 105 106 108 110
102
7
101 102 103 105 106 108 110
115
Solution
47/95
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
};
return root;
}
int main() {
Node* root = NULL;
int numNodes, value, key;
48/95
cin >> key;
return 0;
}
49/95
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
return root;
}
int main() {
struct Node* root = NULL;
int numNodes, value, key;
scanf("%d", &numNodes);
50/95
scanf("%d", &key);
return 0;
}
10
! @ # $ % ^ & * ( )
@
! # $ % & ( ) * ^
20
m a l r g z q k i d h f j n o p e A Y U
U
A Y a d e f g h i j k l m n o p q r z
30
C D 1 9 2 G 7 K P F T A H 5 N Z Q M X R B Y E 6 J 8 U V L !
C
! 1 2 5 6 7 8 9 A B D E F G H J K L M N P Q R T U V X Y Z
50
q R 1 b S c 2 t V u x H N i 9 g Y 7 O K P 8 W 6 Z a j 5 m E 4 B d C L f X p n h 0
J k 3 o I v M y s
0
1 2 3 4 5 6 7 8 9 B C E H I J K L M N O P R S V W X Y Z a b c d f g h i j k m n o
p q s t u v x y
5
Z E W T Y
Y
E T W Z
51/95
7
A 2 B b # c D
#
2 A B D b c
Solution
52/95
#include <iostream>
using namespace std;
struct Node {
char data;
Node* left;
Node* right;
};
53/95
void inorder(Node* root) {
if (root != NULL) {
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}
int main() {
int size;
cin >> size;
Node* root = NULL;
char input, char_to_delete;
while(size!=0) {
cin >> input;
root = insert(root, input);
size--;
}
cin >> char_to_delete;
root = deleteNode(root, char_to_delete);
inorder(root);
cout << endl;
return 0;
}
54/95
#include <stdio.h>
#include <stdlib.h>
struct Node {
char data;
struct Node* left;
struct Node* right;
};
55/95
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
}
return root;
}
int main() {
int size;
scanf("%d", &size);
struct Node* root = NULL;
char input, char_to_delete;
while (size != 0) {
scanf(" %c", &input);
root = insert(root, input);
size--;
}
scanf(" %c", &char_to_delete);
root = deleteNode(root, char_to_delete);
inorder(root);
printf("\n");
return 0;
}
6
3
2
1
4
7
8
9
10
-1
1 2 3 4 6 7 8 9 10
5
3
7
2
4
9
-1
56/95
2 3 4 5 7 9
6
3
2
1
-1
1 2 3 6
11
56
45
78
-1
11 45 56 78
7
3
6
9
4
1
5
4
9
-1
1 3 4 4 5 6 7 9 9
79
46
64
95
12
145
63
32
-1
12 32 46 63 64 79 95 145
57/95
1
2
3
4
5
-1
1 2 3 4 5
7
6
4
5
3
-1
3 4 5 6 7
Solution
58/95
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct node
{
int data;
struct node*left;
struct node*right;
};
struct node*root;
void append(int d)//3
{
struct node*newnode = (struct node*)malloc(sizeof(struct node));
struct node*temp = root;//6
newnode->data = d;//3
newnode->left = NULL;
newnode->right = NULL;
if(root == NULL)//6
{
root = newnode;//6
}
else
{
while(true)
{
if(d < temp->data)
{
if(temp->left != NULL)
{
temp = temp->left;
}
else
{
temp->left = newnode;//6->3
break;
}
}else
{
if(temp->right != NULL)
{
temp = temp->right;
}else
{
temp->right = newnode;
break;
}
}
}
}
}
void display(struct node*root)
{
if(root == NULL)
{
return;
}
59/95
else
{
display(root->left);
printf("%d ",root->data);
display(root->right);
}
}
int main()
{
int d;
do
{
scanf("%d",&d);//3
if(d > 0)
append(d);
}while(d != -1);//-1
display(root);
return 0;
}
60/95
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
struct node*left;
struct node*right;
};
struct node*root;
void append(int d)
{
struct node*newnode = (struct node*)malloc(sizeof(struct node));
struct node*temp = root;
newnode->data = d;
newnode->left = NULL;
newnode->right = NULL;
if(root == NULL)
{
root = newnode;
}
else
{
while(true)
{
if(d < temp->data)
{
if(temp->left != NULL)
{
temp = temp->left;
}
else
{
temp->left = newnode;
break;
}
}else
{
if(temp->right != NULL)
{
temp = temp->right;
}else
{
temp->right = newnode;
break;
}
}
}
}
}
void display(struct node*root)
{
if(root == NULL)
{
return;
}
61/95
else
{
display(root->left);
cout<<root->data<<" ";
display(root->right);
}
}
int main()
{
int d;
do
{
cin>>d;//3
if(d > 0)
append(d);
}while(d != -1);//-1
display(root);
return 0;
}
11
9 3 5 1 8 12 16 11 2 4 6
9 3 1 2 5 4 8 6 12 11 16
7
1 8 12 16 11 2 4
1 8 2 4 12 11 16
8
8 4 9 1 2 3 6 5
8 4 1 2 3 6 5 9
26
23 16 15 9 6 17 10 13 8 26 18 2 22 24 12 5 20 25 21 4 19 1 3 14 7 11
23 16 15 9 6 2 1 5 4 3 8 7 10 13 12 11 14 17 18 22 20 19 21 26 24 25
9
7 9 4 2 6 3 5 1 8
7 4 2 1 3 6 5 9 8
62/95
1
5
5
10 5 15 3 7
10 5 3 7 15
7
8 3 10 1 6 14 4
8 3 1 6 4 10 14
Solution
63/95
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node {
int data;
struct node *left;
struct node *right;
};
if(root == NULL){
root = malloc(sizeof(struct node));
root->data = data;
root->left = NULL;
root->right = NULL;
int main() {
int t;
int data;
scanf("%d", &t);
while(t-- > 0) {
scanf("%d", &data);
root = insert(root, data);
}
64/95
preOrder(root);
return 0;
}
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
};
int main() {
Node* root = nullptr;
int t;
int data;
cin >> t;
preOrder(root);
return 0;
}
65/95
1
2
3
4
5
-1
10
20
30
40
50
-1
1
3
5
7
4
8
9
-1
79
46
13
25
28
-1
79
95
48
12
26
35
65
48
-1
66/95
Sum of all nodes in the BST is 360
76
67
98
15
11
13
15
14
61
63
56
-1
5
3
7
2
4
9
-1
6
3
1
4
2
-1
Solution
67/95
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*left;
struct node*right;
};
struct node*root;
void append(int d)
{
struct node*newnode = (struct node*)malloc(sizeof(struct node));
struct node*temp = root;
newnode->data = d;//2
newnode->left = NULL;
newnode->right = NULL;
if(root == NULL)
{
root = newnode;
}
else
{
while(true)
{
if(d < temp->data)
{
if(temp->left != NULL)
{
temp = temp->left;
}else
{
temp->left = newnode;
break;
}
}else
{
if(temp->right != NULL)
{
temp = temp->right;
}else
{
temp->right = newnode;
break;
}
}
}
}
}
int addBT(node* root)
{
if(root == NULL)
return 0;
return (root->data+addBT(root->left)+addBT(root->right));
68/95
}
int main()
{
int d;
do
{
scanf("%d",&d);
if(d > 0)
append(d);
}while(d != -1);
int sum = addBT(root);
printf("Sum of all nodes are %d",sum);
return 0;
}
69/95
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
return root;
}
int main()
{
int d;
do
{
cin >> d;
if (d > 0)
root = insert(root, d);
} while (d != -1);
70/95
return 0;
}
1
2
3
4
5
-1
10
20
30
40
50
-1
1
3
5
7
4
8
9
-1
79
46
13
25
28
-1
71/95
79
95
48
12
26
35
65
48
-1
76
67
98
15
11
13
15
14
61
63
56
-1
5
3
7
2
4
9
-1
6
3
1
4
2
-1
Solution
72/95
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*left;
struct node*right;
};
struct node*root;
void append(int d)
{
struct node*newnode = (struct node*)malloc(sizeof(struct node));
struct node*temp = root;
newnode->data = d;
newnode->left = NULL;
newnode->right = NULL;
if(root == NULL)
{
root = newnode;
}
else
{
while(true)
{
if(d < temp->data)
{
if(temp->left != NULL)
{
temp = temp->left;
}else
{
temp->left = newnode;
break;
}
}else
{
if(temp->right != NULL)
{
temp = temp->right;
}else
{
temp->right = newnode;
break;
}
}
}
}
}
void leafsum(node *root,int *sum)
{
if(!root)
return;
if(!root->left && !root->right)
*sum+= root->data;
leafsum(root->left,sum);
leafsum(root->right,sum);
73/95
}
int main()
{
int d;
do
{
scanf("%d",&d);
if(d > 0)
append(d);
}while(d != -1);
int sum = 0;
leafsum(root,&sum);
printf("Sum of all leaf nodes are %d",sum);
return 0;
}
74/95
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* root;
void append(int d)
{
struct node* newnode = createNode(d);
struct node* temp = root;
if (root == NULL)
{
root = newnode;
}
else
{
while (true)
{
if (d < temp->data)
{
if (temp->left != NULL)
{
temp = temp->left;
}
else
{
temp->left = newnode;
break;
}
}
else
{
if (temp->right != NULL)
{
temp = temp->right;
}
else
{
temp->right = newnode;
break;
}
}
75/95
}
}
}
int main()
{
int d;
do
{
cin >> d;
if (d > 0)
append(d);
} while (d != -1);
int sum = 0;
leafsum(root, &sum);
cout << "Sum of all leaf nodes are " << sum;
return 0;
}
6
5
Wrong choice
1
12
123 456 789 987 654 321 147 258 369 741 852 963
2
3
1
6
18 98 77 34 55 26
2
3
76/95
BST with 6 nodes is ready to use!
BST Traversal in POSTORDER
26 55 34 77 98 18
1
10
789 987 654 321 147 258 369 741 852 963
2
3
1
6
12 23 45 56 78 89
2
3
1
12
12 23 45 56 78 89 98 87 65 54 32 21
2
3
1
5
12 78 96 34 55
2
3
77/95
1
9
7 9 6 3 2 1 4 5 8
2
5
3
Solution
78/95
#include <iostream>
#include <stdlib.h>
struct tnode {
int data;
struct tnode *right;
struct tnode *left;
};
int main() {
struct tnode *root = NULL;
int choice, item, n, i;
do {
cin >> choice;
switch (choice) {
case 1:
root = NULL;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> item;
root = CreateBST(root, item);
}
cout << "BST with " << n << " nodes (without duplicates) is ready
to use!" << "\n";
break;
case 2:
cout << "BST Traversal in POSTORDER" << "\n";
79/95
Postorder(root);
cout << "\n";
break;
case 3:
exit(0);
break;
default:
cout << "Wrong choice" << "\n";
break;
}
} while (1);
return 0;
}
80/95
#include <stdio.h>
#include <stdlib.h>
struct tnode {
int data;
struct tnode *right;
struct tnode *left;
};
return (root);
}
}
int main() {
struct tnode *root = NULL;
int choice, item, n, i;
do {
scanf("%d", &choice);
switch (choice) {
case 1:
root = NULL;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &item);
root = CreateBST(root, item);
}
printf("BST with %d nodes is ready to use!\n", n);
break;
case 2:
printf("BST Traversal in POSTORDER\n");
Postorder(root);
printf("\n");
break;
81/95
case 3:
exit(0);
break;
default:
printf("Wrong choice\n");
break;
}
} while (1);
return 0;
}
50
30
20
40
70
60
80
-1
4
50
30
20
40
70
60
80
-1
6
5
3
7
2
4
9
-1
4
82/95
6
3
2
1
4
7
8
9
10
-1
2
6
3
2
1
4
7
8
9
10
-1
9
6
3
2
1
-1
4
20
30
40
50
60
70
80
-1
3
83/95
1
2
3
4
5
-1
2
Solution
84/95
#include<stdio.h>
using namespace std;
struct Node
{
int data;
Node *left,*right;
};
void append(Node **rootadd,int data)
{
Node *temp,*newnode;
temp = *rootadd;
newnode = new Node();
newnode->left = NULL;
newnode->data = data;
newnode->right = NULL;
if(*rootadd == NULL)
*rootadd = newnode;
else
{
while(1)
{
if(data > temp->data)
{
if(temp->right != NULL)
{
temp = temp->right;
}
else
{
temp->right = newnode;
break;
}
}
else
{
if(temp->left != NULL)
{
temp = temp->left;
}
else
{
temp->left = newnode;
break;
}
}
}
}
}
int KSmallestUsingMorris(Node *root, int k)
{
int count = 0;
int ksmall = -1;
Node *curr = root;
while (curr != NULL)
{
if (curr->left == NULL)
85/95
{
count++;
if (count==k)
ksmall = curr->data;
curr = curr->right;
}
else
{
Node *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)
ksmall = curr->data;
curr = curr->right;
}
}
}
return ksmall;
}
int main()
{
Node *root = NULL;
int data,k;
do
{
sacnf("%d",&data);
if(data > 0)
append(&root,data);
}while(data > 0);
cin>>k;
printf("Smallest kth value %d",KSmallestUsingMorris(root,k));
return 0;
}
86/95
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *left, *right;
};
87/95
while (curr != NULL)
{
if (curr->left == NULL)
{
count++;
if (count == k)
ksmall = curr->data;
curr = curr->right;
}
else
{
Node *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)
ksmall = curr->data;
curr = curr->right;
}
}
}
return ksmall;
}
int main()
{
Node *root = NULL;
int data, k;
do
{
cin >> data;
if (data > 0)
append(&root, data);
} while (data > 0);
cin >> k;
cout << "Smallest kth value " << KSmallestUsingMorris(root, k);
return 0;
}
6
3
1
2
4
-1
4
88/95
4 is present in the BST
6
3
1
2
4
-1
3
6
3
2
1
4
10
9
8
7
15
-1
10
6
3
2
1
4
10
9
8
7
15
-1
14
89/95
6
3
2
1
4
10
9
8
7
15
-1
7
6
3
2
1
4
10
9
8
7
15
-1
16
50
30
70
20
40
60
-1
30
6
3
1
2
4
-1
10
90/95
Solution
91/95
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*left;
struct node*right;
};
struct node*root;
void append(int d)//3
{
struct node*newnode = (struct node*)malloc(sizeof(struct node));
struct node*temp = root;//6
newnode->data = d;//3
newnode->left = NULL;
newnode->right = NULL;
if(root == NULL)//6
{
root = newnode;//6
}
else
{
while(true)
{
if(d < temp->data)
{
if(temp->left != NULL)
{
temp = temp->left;
}
else
{
temp->left = newnode;//6->3
break;
}
}else
{
if(temp->right != NULL)
{
temp = temp->right;
}else
{
temp->right = newnode;
break;
}
}
}
}
}
bool iterativesearch(struct node*root,int key)
{
while(root != NULL)//10
{
if(key > root->data)
{
root = root->right;//10
92/95
}
else if(key < root->data)
{
root = root->left;
}
else
return true;
}
return false;
}
int main()
{
int d,search;
do
{
scanf("%d",&d);//3
if(d > 0)
append(d);
}while(d != -1);//-1
scanf("%d",&search);
if(iterativesearch(root,search))
cout<<search<<" is present in the BST";
else
cout<<search<<" is not present in the BST";
return 0;
}
93/95
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
struct node*left;
struct node*right;
};
struct node*root;
void append(int d)//3
{
struct node*newnode = (struct node*)malloc(sizeof(struct node));
struct node*temp = root;//6
newnode->data = d;//3
newnode->left = NULL;
newnode->right = NULL;
if(root == NULL)//6
{
root = newnode;//6
}
else
{
while(true)
{
if(d < temp->data)
{
if(temp->left != NULL)
{
temp = temp->left;
}
else
{
temp->left = newnode;//6->3
break;
}
}else
{
if(temp->right != NULL)
{
temp = temp->right;
}else
{
temp->right = newnode;
break;
}
}
}
}
}
bool iterativesearch(struct node*root,int key)
{
while(root != NULL)//10
{
if(key > root->data)
{
94/95
root = root->right;//10
}
else if(key < root->data)
{
root = root->left;
}
else
return true;
}
return false;
}
int main()
{
int d,search;
do
{
cin>>d;//3
if(d > 0)
append(d);
}while(d != -1);//-1
cin>>search;
if(iterativesearch(root,search))
cout<<search<<" is present in the BST";
else
cout<<search<<" is not present in the BST";
return 0;
}
95/95