implement search with search helper

This commit is contained in:
Moetez Skouri 2020-05-05 00:39:49 +01:00
parent fc8e36c6e3
commit 59488a1063

View File

@ -202,6 +202,29 @@ public class AVLTree {
} }
} }
public boolean search(int key) {
Node result = searchHelper(this.root,key);
if(result != null)
return true ;
return false ;
}
private Node searchHelper(Node root, int key)
{
//root is null or key is present at root
if (root==null || root.key==key)
return root;
// key is greater than root's key
if (root.key > key)
return searchHelper(root.left, key); // call the function on the node's left child
// key is less than root's key then
//call the function on the node's right child as it is greater
return searchHelper(root.right, key);
}
public static void main(String[] args) { public static void main(String[] args) {
AVLTree tree = new AVLTree(); AVLTree tree = new AVLTree();