From 59488a106392ba35abbf72fdf37a24a325ec9679 Mon Sep 17 00:00:00 2001 From: Moetez Skouri Date: Tue, 5 May 2020 00:39:49 +0100 Subject: [PATCH] implement search with search helper --- DataStructures/Trees/AVLTree.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/DataStructures/Trees/AVLTree.java b/DataStructures/Trees/AVLTree.java index b496be696..2e7119604 100644 --- a/DataStructures/Trees/AVLTree.java +++ b/DataStructures/Trees/AVLTree.java @@ -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) { AVLTree tree = new AVLTree();