File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -202,6 +202,29 @@ private void reheight(Node node) {
202202 }
203203 }
204204
205+ public boolean search (int key ) {
206+ Node result = searchHelper (this .root ,key );
207+ if (result != null )
208+ return true ;
209+
210+ return false ;
211+ }
212+
213+ private Node searchHelper (Node root , int key )
214+ {
215+ //root is null or key is present at root
216+ if (root ==null || root .key ==key )
217+ return root ;
218+
219+ // key is greater than root's key
220+ if (root .key > key )
221+ return searchHelper (root .left , key ); // call the function on the node's left child
222+
223+ // key is less than root's key then
224+ //call the function on the node's right child as it is greater
225+ return searchHelper (root .right , key );
226+ }
227+
205228 public static void main (String [] args ) {
206229 AVLTree tree = new AVLTree ();
207230
You can’t perform that action at this time.
0 commit comments