0% found this document useful (0 votes)
12 views14 pages

Unit 4 BST

The document outlines multiple programming problems involving binary search trees (BSTs) in C++. Each problem includes a description of the task, input/output formats, sample test cases, and constraints, focusing on operations like insertion, deletion, traversal, and searching within the BST.

Uploaded by

Kartik Sharma
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)
12 views14 pages

Unit 4 BST

The document outlines multiple programming problems involving binary search trees (BSTs) in C++. Each problem includes a description of the task, input/output formats, sample test cases, and constraints, focusing on operations like insertion, deletion, traversal, and searching within the BST.

Uploaded by

Kartik Sharma
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/ 14

Single File Programming Question

Problem Statement

Josh is fascinated by 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 second line contains 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.

Refer to the sample output for the formatting specifications.

Code constraints :

1 ≤ t ≤ 100

1 <= data <= 1000

Sample test cases :

Input 1 :

10 5 15 3 7

Output 1 :

10 5 3 7 15

Input 2 :

8 3 10 1 6 14 4

Output 2 :

8 3 1 6 4 10 14
1. #include <iostream>
2. using namespace std;
3.
4. struct Node {
5. int data;
6. Node* left;
7. Node* right;
8. };
9.
10. void preOrder(Node* root) {
11. if (root == nullptr) {
12. return;
13. }
14. cout << root->data << " ";
15. preOrder(root->left);
16. preOrder(root->right);
17. }
18.
19. Node* insert(Node* root, int data) {
20. if (root == nullptr) {
21. Node* newNode = new Node;
22. newNode->data = data;
23. newNode->left = newNode->right = nullptr;
24. return newNode;
25. }
26.
27. if (data < root->data) {
28. root->left = insert(root->left, data);
29. } else if (data > root->data) {
30. root->right = insert(root->right, data);
31. }
32.
33. return root;
34. }
35.
36. int main() {
37. Node* root = nullptr;
38.
39. int t;
40. int data;
41.
42. cin >> t;
43.
44. while (t-- > 0) {
45. cin >> data;
46. root = insert(root, data);
47. }
48.
49. preOrder(root);
50. return 0;
51. }
52.
Single File Programming Question

Problem Statement

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.

Preethi needs a program that allows her to:

1. Create a binary search tree by inserting a series of characters.

2. Delete a specific character from the binary search tree.

3. Print the characters in the BST in an in-order traversal.

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.

The second line of input consists of N unique characters separated by a space.

The third line of input consists of the character M to be deleted.

Output format :

The output displays the in-order traversal of the given inputs after deleting the character M.

Refer to the sample output for formatting specifications.

Code constraints :

1 ≤ N ≤ 50

Sample test cases :

Input 1 :

ZEWTY

Output 1 :

ETWZ

Input 2 :
7

A2Bb#cD

Output 2 :

2ABDbc

Input 3 :

t4%uK

Output 3 :

%4Kt
1. #include <iostream>
2. using namespace std;
3.
4. struct Node {
5. char data;
6. Node* left;
7. Node* right;
8. };
9.
10. Node* insert(Node* root, char data) {
11. if (root == NULL) {
12. root = new Node();
13. root->data = data;
14. root->left = root->right = NULL;
15. } else if (data <= root->data) {
16. root->left = insert(root->left, data);
17. } else {
18. root->right = insert(root->right, data);
19. }
20. return root;
21. }
22.
23. Node* findMin(Node* root) {
24. while (root->left != NULL) {
25. root = root->left;
26. }
27. return root;
28. }
29.
30. Node* deleteNode(Node* root, char data) {
31. if (root == NULL) {
32. return root;
33. }
34.
35. if (data < root->data) {
36. root->left = deleteNode(root->left, data);
37. } else if (data > root->data) {
38. root->right = deleteNode(root->right, data);
39. } else {
40. // Node with one or no child
41. if (root->left == NULL) {
42. Node* temp = root->right;
43. delete root;
44. return temp;
45. } else if (root->right == NULL) {
46. Node* temp = root->left;
47. delete root;
48. return temp;
49. }
50.
51. // Node with two children: Get the inorder successor (smallest
52. // in the right subtree)
53. Node* temp = findMin(root->right);
54.
55. // Copy the inorder successor's content to this node
56. root->data = temp->data;
57.
58. // Delete the inorder successor
59. root->right = deleteNode(root->right, temp->data);
60. }
61. return root;
62. }
63.
64. void inorder(Node* root) {
65. if (root != NULL) {
66. inorder(root->left);
67. cout << root->data << " ";
68. inorder(root->right);
69. }
70. }
71.
72. int main() {
73. int size;
74. cin >> size;
75. Node* root = NULL;
76. char input, char_to_delete;
77. while (size != 0) {
78. cin >> input;
79. root = insert(root, input);
80. size--;
81. }
82. cin >> char_to_delete;
83. root = deleteNode(root, char_to_delete);
84. inorder(root);
85. cout << endl;
86. return 0;
87. }
88.
Single File Programming Question

Problem Statement

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?

For example, consider a tree with the following structure:

/\

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.

You can assume that the input integers are unique.

Output format :

The output displays the sum of all the leaf nodes in the binary search tree.

Refer to the sample output for the formatting specifications.

Code constraints :

All input integers will be positive integers (greater than 0).

The number of nodes in the binary tree is at most 100.

The value of each node is at most 100.

Sample test cases :

Input 1 :

3
7

-1

Output 1 :

Sum of all leaf nodes are 15

Input 2 :

-1

Output 2 :

Sum of all leaf nodes are 6


1. #include <iostream>
2. using namespace std;
3.
4. struct node {
5. int data;
6. struct node* left;
7. struct node* right;
8. };
9.
10. struct node* root;
11.
12. // Function to create a new node
13. struct node* createNode(int d) {
14. struct node* newNode = new node;
15. newNode->data = d;
16. newNode->left = newNode->right = NULL;
17. return newNode;
18. }
20. // Function to insert a node into the BST
21. struct node* insert(struct node* node, int data) {
22. if (node == NULL) {
23. return createNode(data);
24. }
25.
26. if (data < node->data) {
27. node->left = insert(node->left, data);
28. } else if (data > node->data) {
29. node->right = insert(node->right, data);
30. }
31.
32. return node;
33. }
34. void append(int d) {
35. root = insert(root, d);
36. }
37.
38.
39. void leafsum(node* root, int* sum) {
40. if (!root)
41. return;
42.
43. if (!root->left && !root->right)
44. *sum += root->data;
45.
46. leafsum(root->left, sum);
47. leafsum(root->right, sum);
48. }
49.
50. int main() {
51. int d;
52. do {
53. cin >> d;
54. if (d > 0)
55. append(d);
56. } while (d != -1);
57.
58. int sum = 0;
59. leafsum(root, &sum);
60. cout << "Sum of all leaf nodes are " << sum;
61.
62. return 0;
63. }
Single File Programming Question

Problem Statement

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.

Company Tags: Wipro

Input format :

The first line of input consists of the number of nodes n.

The second line of input consists of n unique values for nodes, separated by a space.

The third line of input consists of the key to be searched.

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"

Refer to the sample output for the exact format.

Code constraints :

1 ≤ n ≤ 10

Each node value is a unique integer.

1 <= key <= 1000

Sample test cases :

Input 1 :

101 102 103 105 106 108 110

102

Output 1 :
The key 102 is found in the binary search tree

Input 2 :

101 102 103 105 106 108 110

115

Output 2 :

The key 115 is not found in the binary search tree

Note :
1. #include <iostream>
2. using namespace std;
4. struct Node {
5. int data;
6. Node* left;
7. Node* right;
8. };
10. Node* createNode(int value) {
11. Node* newNode = new Node;
12. newNode->data = value;
13. newNode->left = newNode->right = NULL;
14. return newNode;
15. }
16.
17. Node* insertNode(Node* root, int value) {
18. if (root == NULL) {
19. return createNode(value);
20. }
21.
22. if (value < root->data) {
23. root->left = insertNode(root->left, value);
24. } else if (value > root->data) {
25. root->right = insertNode(root->right, value);
26. }
27.
28. return root;
29. }
30.
31. bool searchKey(Node* root, int key) {
32. if (root == NULL) {
33. return false;
34. }
35.
36. if (root->data == key) {
37. return true;
38. } else if (key < root->data) {
39. return searchKey(root->left, key);
40. } else {
41. return searchKey(root->right, key);
42. }
43. }
44.
45. int main() {
46. Node* root = NULL;
47. int numNodes, value, key;
48.
49. cin >> numNodes;
50.
51. for (int i = 0; i < numNodes; i++) {
52. cin >> value;
53. root = insertNode(root, value);
54. }
55.
56. cin >> key;
57.
58. bool found = searchKey(root, key);
59. if (found) {
60. cout << "The key " << key << " is found in the binary search tree";
61. } else {
62. cout << "The key " << key << " is not found in the binary search tree";
63. }
64.
65. return 0;
66. }
Single File Programming Question

Problem Statement

You are provided with a collection of lowercase letters. Your objective is to create a Binary Search Tree (BST)
from these letters.

Once the BST is constructed, perform an in-order traversal of the tree to display the characters in ascending
alphabetical order.

Input format :

The first line of input consists of a positive integer N, representing the number of letters.

The second line consists of N characters, representing the letters to be inserted into the tree.

Output format :

The output prints the in-order traversal of the given characters.

Code constraints :

1 ≤ N ≤ 26

The input characters will be in lowercase.

Sample test cases :

Input 1 :

kbacm

Output 1 :

abckm

Input 2 :

yqweghk

Output 2 :

eghkqwy
1. #include <iostream>
2. using namespace std;
3.
4. struct TreeNode {
5. char val;
6. TreeNode* left;
7. TreeNode* right;
8. };
9.
10. TreeNode* insert(TreeNode* root, char val) {
11. if (root == nullptr) {
12. root = new TreeNode;
13. root->val = val;
14. root->left = root->right = nullptr;
15. } else if (val < root->val) {
16. root->left = insert(root->left, val);
17. } else {
18. root->right = insert(root->right, val);
19. }
20. return root;
21. }
22.
23. void inorderTraversal(TreeNode* root) {
24. if (root == nullptr) {
25. return;
26. }
27. inorderTraversal(root->left);
28. cout << root->val << " ";
29. inorderTraversal(root->right);
30. }
31.
32. int main() {
33. TreeNode* root = nullptr;
34. int numNodes;
35. char val;
36.
37. cin >> numNodes;
38.
39. for (int i = 0; i < numNodes; i++) {
40. cin >> val;
41. root = insert(root, val);
42. }
43.
44. inorderTraversal(root);
45.
46. return 0;
47. }
48.

You might also like