-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.java
47 lines (44 loc) · 1.24 KB
/
BST.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
/*
Given a binary search tree in an array form constructed as an inorder traversal, return true
if the element is present in the array else return false
e.g. searchTree(10, [5,10,20]) ==> true
searchTree(80, [5,10,20]) ==> false
*/
class BST {
static boolean searchBST(List<Integer> input, int elem, int start, int end)
{
int mid = (start + end) / 2;
if (start > end)
return false;
else if (input.get(mid) == elem)
return true;
else if (elem > input.get(mid))
return searchBST(input, elem, mid+1, end);
else if (elem < input.get(mid))
return searchBST(input, elem, 0, mid-1);
return false;
}
public static void main(String[] args)
{
List<Integer> input = new ArrayList<Integer>();
for (String arg : args)
{
input.add(Integer.parseInt(arg));
}
System.out.println("\nInput BST: ");
int elem = 0;
for (int i = 0; i < input.size(); i++)
{
if (i == input.size() - 1)
elem = input.get(i);
else
System.out.print(input.get(i) + " ");
}
System.out.println("\nElement to search: " + elem + "\n");
if (searchBST(input, elem, 0, input.size() - 2))
System.out.println(elem + " is a valid node in BST");
else
System.out.println("Oops!! " + elem + " does not exist in BST");
}
}