Skip to content

Commit 59488a1

Browse files
Moetez SkouriMoetez Skouri
Moetez Skouri
authored and
Moetez Skouri
committed
implement search with search helper
1 parent fc8e36c commit 59488a1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

DataStructures/Trees/AVLTree.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)