11package com .thealgorithms .datastructures .trees ;
22
3+ import com .thealgorithms .datastructures .trees .BinaryTree .Node ;
4+
35/**
46 *
57 *
68 * <h1>Binary Search Tree (Recursive)</h1>
79 *
810 * 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
1012 * checked Binary Search Tree is a binary tree which satisfies three properties:
1113 * 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.
1315 *
1416 * <p>
1517 * I have made public functions as methods and to actually implement recursive
@@ -31,30 +33,8 @@ public class BSTRecursive {
3133 root = null ;
3234 }
3335
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 ;
5838 }
5939
6040 /**
@@ -82,7 +62,7 @@ private Node delete(Node node, int data) {
8262 Node temp = node .left ;
8363 node .left = null ;
8464 node = temp ;
85- } else { // both child are present
65+ } else { // both children are present
8666 Node temp = node .right ;
8767 // Find leftmost child of right subtree
8868 while (temp .left != null ) {
@@ -114,60 +94,6 @@ private Node insert(Node node, int data) {
11494 return node ;
11595 }
11696
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-
17197 /**
17298 * Serach recursively if the given value is present in BST or not.
17399 *
@@ -206,33 +132,6 @@ public void remove(int data) {
206132 this .root = delete (this .root , data );
207133 }
208134
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-
236135 /**
237136 * To check if given value is present in tree or not.
238137 *
@@ -246,23 +145,4 @@ public boolean find(int data) {
246145 System .out .println (data + " not found." );
247146 return false ;
248147 }
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- }
268148}
0 commit comments