Unit 4 BST
Unit 4 BST
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.
Code constraints :
1 ≤ t ≤ 100
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.
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 :
Output format :
The output displays the in-order traversal of the given inputs after deleting the character M.
Code constraints :
1 ≤ N ≤ 50
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?
/\
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.
Code constraints :
Input 1 :
3
7
-1
Output 1 :
Input 2 :
-1
Output 2 :
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.
Input format :
The second line of input consists of n unique values 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"
Code constraints :
1 ≤ n ≤ 10
Input 1 :
102
Output 1 :
The key 102 is found in the binary search tree
Input 2 :
115
Output 2 :
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 :
Code constraints :
1 ≤ N ≤ 26
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.