1
1
package com .thealgorithms .datastructures .trees ;
2
2
3
+ import com .thealgorithms .datastructures .trees .BinaryTree .Node ;
4
+
3
5
/**
4
6
*
5
7
*
6
8
* <h1>Binary Search Tree (Recursive)</h1>
7
9
*
8
10
* An implementation of BST recursively. In recursive implementation the checks
9
- * are down the tree First root is checked if not found then its childs are
11
+ * are down the tree First root is checked if not found then its children are
10
12
* checked Binary Search Tree is a binary tree which satisfies three properties:
11
13
* left child is less than root node, right child is grater than root node, both
12
- * left and right childs must themselves be a BST.
14
+ * left and right children must themselves be a BST.
13
15
*
14
16
* <p>
15
17
* I have made public functions as methods and to actually implement recursive
@@ -31,30 +33,8 @@ public class BSTRecursive {
31
33
root = null ;
32
34
}
33
35
34
- /**
35
- * main function for tests
36
- */
37
- public static void main (String [] args ) {
38
- BSTRecursive tree = new BSTRecursive ();
39
- tree .add (5 );
40
- tree .add (10 );
41
- tree .add (9 );
42
- assert !tree .find (4 ) : "4 is not yet present in BST" ;
43
- assert tree .find (10 ) : "10 should be present in BST" ;
44
- tree .remove (9 );
45
- assert !tree .find (9 ) : "9 was just deleted from BST" ;
46
- tree .remove (1 );
47
- assert !tree .find (
48
- 1
49
- ) : "Since 1 was not present so find deleting would do no change" ;
50
- tree .add (20 );
51
- tree .add (70 );
52
- assert tree .find (70 ) : "70 was inserted but not found" ;
53
- /*
54
- Will print in following order
55
- 5 10 20 70
56
- */
57
- tree .inorder ();
36
+ public Node getRoot () {
37
+ return root ;
58
38
}
59
39
60
40
/**
@@ -82,7 +62,7 @@ private Node delete(Node node, int data) {
82
62
Node temp = node .left ;
83
63
node .left = null ;
84
64
node = temp ;
85
- } else { // both child are present
65
+ } else { // both children are present
86
66
Node temp = node .right ;
87
67
// Find leftmost child of right subtree
88
68
while (temp .left != null ) {
@@ -114,60 +94,6 @@ private Node insert(Node node, int data) {
114
94
return node ;
115
95
}
116
96
117
- /**
118
- * Recursively print Preorder traversal of the BST
119
- *
120
- * @param node the root node
121
- */
122
- private void preOrder (Node node ) {
123
- if (node == null ) {
124
- return ;
125
- }
126
- System .out .print (node .data + " " );
127
- if (node .left != null ) {
128
- preOrder (node .left );
129
- }
130
- if (node .right != null ) {
131
- preOrder (node .right );
132
- }
133
- }
134
-
135
- /**
136
- * Recursively print Postorder travesal of BST.
137
- *
138
- * @param node the root node
139
- */
140
- private void postOrder (Node node ) {
141
- if (node == null ) {
142
- return ;
143
- }
144
- if (node .left != null ) {
145
- postOrder (node .left );
146
- }
147
- if (node .right != null ) {
148
- postOrder (node .right );
149
- }
150
- System .out .print (node .data + " " );
151
- }
152
-
153
- /**
154
- * Recursively print Inorder traversal of BST.
155
- *
156
- * @param node the root node
157
- */
158
- private void inOrder (Node node ) {
159
- if (node == null ) {
160
- return ;
161
- }
162
- if (node .left != null ) {
163
- inOrder (node .left );
164
- }
165
- System .out .print (node .data + " " );
166
- if (node .right != null ) {
167
- inOrder (node .right );
168
- }
169
- }
170
-
171
97
/**
172
98
* Serach recursively if the given value is present in BST or not.
173
99
*
@@ -206,33 +132,6 @@ public void remove(int data) {
206
132
this .root = delete (this .root , data );
207
133
}
208
134
209
- /**
210
- * To call inorder traversal on tree
211
- */
212
- public void inorder () {
213
- System .out .println ("Inorder traversal of this tree is:" );
214
- inOrder (this .root );
215
- System .out .println (); // for next line
216
- }
217
-
218
- /**
219
- * To call postorder traversal on tree
220
- */
221
- public void postorder () {
222
- System .out .println ("Postorder traversal of this tree is:" );
223
- postOrder (this .root );
224
- System .out .println (); // for next li
225
- }
226
-
227
- /**
228
- * To call preorder traversal on tree.
229
- */
230
- public void preorder () {
231
- System .out .println ("Preorder traversal of this tree is:" );
232
- preOrder (this .root );
233
- System .out .println (); // for next li
234
- }
235
-
236
135
/**
237
136
* To check if given value is present in tree or not.
238
137
*
@@ -246,23 +145,4 @@ public boolean find(int data) {
246
145
System .out .println (data + " not found." );
247
146
return false ;
248
147
}
249
-
250
- /**
251
- * The Node class used for building binary search tree
252
- */
253
- private static class Node {
254
-
255
- int data ;
256
- Node left ;
257
- Node right ;
258
-
259
- /**
260
- * Constructor with data as parameter
261
- */
262
- Node (int d ) {
263
- data = d ;
264
- left = null ;
265
- right = null ;
266
- }
267
- }
268
148
}
0 commit comments