1+ /*
2+ Title: BST Class
3+
4+ Problem statement
5+ Implement the BST class which includes following functions -
6+ 1. insert -
7+ Given an element, insert that element in the BST at the correct position. If element is equal to
8+ the data of the node, insert it in the left subtree.
9+
10+ 2. search
11+ Given an element, find if that is present in BST or not. Return true or false.
12+
13+ 3. delete -
14+ Given an element, remove that element from the BST. If the element which is to be deleted has both
15+ children, replace that with the minimum element from right sub-tree.
16+
17+ 4. printTree (recursive) -
18+ Print the BST in in the following format -
19+ For printing a node with data N, you need to follow the exact format -
20+ N:L:x,R:y
21+
22+ where, N is data of any node present in the binary tree. x and y are the values of left and right
23+ child of node N. Print the children only if it is not null.
24+ There is no space in between.
25+ You need to print all nodes in the recursive format in different lines.
26+
27+ Sample Input 1:
28+ 6
29+ 1 2
30+ 1 3
31+ 1 1
32+ 4
33+ 2 2
34+ 4
35+
36+ Sample Output 1:
37+ 2:L:1,R:3
38+ 1:
39+ 3:
40+ 3:L:1,
41+ 1:
42+
43+ Explanation for sample Input 1:
44+ Explanation: The first line 6 indicates that there are 6 operations to be performed on the BST.
45+ The subsequent lines are the operations, which can be understood as follows:
46+ 1 2: Insert 2 into the BST.
47+
48+ 1 3: Insert 3 into the BST.
49+
50+ 1 1: Insert 1 into the BST.
51+
52+ 4: Print the BST in the specified format.
53+
54+ 2 2: Search for 2 in the BST.
55+
56+ 4: Print the BST in the specified format.
57+ */
58+
59+ #include < iostream>
60+ #include < queue>
61+ using namespace std ;
62+
63+ template <typename T>
64+ class BinaryTreeNode {
65+ public:
66+ T data;
67+ BinaryTreeNode<T> *left;
68+ BinaryTreeNode<T> *right;
69+
70+ BinaryTreeNode (T data) {
71+ this ->data = data;
72+ left = NULL ;
73+ right = NULL ;
74+ }
75+ };
76+
77+ class BST {
78+ private:
79+ BinaryTreeNode<int > * root;
80+
81+ public:
82+ BST () {
83+ root = NULL ;
84+ }
85+
86+ /* ----------------- Public Functions of BST -----------------*/
87+ BinaryTreeNode<int > * removeHelper (BinaryTreeNode<int > * node, int data) {
88+ if (node == NULL ) {
89+ return NULL ;
90+ }
91+
92+ if (data < node->data ) {
93+ node->left = removeHelper (node->left , data);
94+ return root;
95+ } else if (data > node->data ) {
96+ node->right = removeHelper (node->right , data);
97+ return node;
98+ } else {
99+ if (node->left == NULL && node->right == NULL ) {
100+ return NULL ;
101+ } else if (node->left == NULL ) {
102+ return node->right ;
103+ } else if (node->right == NULL ) {
104+ return node->left ;
105+ } else {
106+ BinaryTreeNode<int > * minNode = node->right ;
107+ while (minNode->left != NULL ) {
108+ minNode = minNode->left ;
109+ }
110+
111+ node->data = minNode->data ;
112+ node->right = removeHelper (node->right , minNode->data );
113+ return node;
114+ }
115+ }
116+ }
117+
118+ void remove (int data) {
119+ this ->root = removeHelper (this ->root , data);
120+ }
121+
122+ void printHelper (BinaryTreeNode<int > * node) {
123+ if (node == NULL ) {
124+ return ;
125+ }
126+
127+ cout << node->data << " :" ;
128+ if (node->left != NULL ) {
129+ cout << " L:" << node->left ->data << " ," ;
130+ }
131+
132+ if (node->right != NULL ) {
133+ cout << " R:" << node->right ->data ;
134+ }
135+ cout << endl;
136+
137+ printHelper (node->left );
138+ printHelper (node->right );
139+ }
140+
141+ void print () {
142+ printHelper (this ->root );
143+ }
144+
145+ BinaryTreeNode<int > * insertHelper (BinaryTreeNode<int > * node, int data) {
146+ if (node == NULL ) {
147+ BinaryTreeNode<int > * newNode = new BinaryTreeNode<int >(data);
148+ return newNode;
149+ }
150+
151+ if (data <= node->data ) {
152+ node->left = insertHelper (node->left , data);
153+ }
154+
155+ if (data > node->data ) {
156+ node->right = insertHelper (node->right , data);
157+ }
158+
159+ return node;
160+ }
161+
162+ void insert (int data) {
163+ this ->root = insertHelper (this ->root , data);
164+ }
165+
166+ bool searchHelper (BinaryTreeNode<int > * node, int data) {
167+ if (node == NULL ) {
168+ return false ;
169+ }
170+
171+ if (node->data == data) {
172+ return true ;
173+ }
174+
175+ if (data < node->data ) {
176+ return searchHelper (node->left , data);
177+ } else if (data > node->data ) {
178+ return searchHelper (node->right , data);
179+ }
180+ }
181+
182+ bool search (int data) {
183+ return searchHelper (this ->root , data);
184+ }
185+ };
186+
187+ int main () {
188+ BST *tree = new BST ();
189+ int choice, input, q;
190+ cin >> q;
191+ while (q--) {
192+ cin >> choice;
193+ switch (choice) {
194+ case 1 :
195+ cin >> input;
196+ tree->insert (input);
197+ break ;
198+ case 2 :
199+ cin >> input;
200+ tree->remove (input);
201+ break ;
202+ case 3 :
203+ cin >> input;
204+ cout << ((tree->search (input)) ? " true\n " : " false\n " );
205+ break ;
206+ default :
207+ tree->print ();
208+ break ;
209+ }
210+ }
211+ }
0 commit comments