Skip to content

Commit a004747

Browse files
authored
Merge pull request TheAlgorithms#1302 from moetezz/moetezsk
implement search in AVL tree
2 parents a43765d + 59488a1 commit a004747

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)