-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinarySearchTree.java
49 lines (44 loc) · 1.04 KB
/
BinarySearchTree.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
48
49
class Node {
Node left, right;
int data;
public Node(int data) {
this.data = data;
}
public void insert(int value) {
if(value <= data) {
if(left == null)
left = new Node(value);
else
left.insert(value);
} else {
if(right == null)
right = new Node(value);
else
right.insert(value);
}
}
public boolean has(int value) {
if(value == data)
return true;
else if(value < data) {
if(left == null)
return false;
else
return left.has(value);
} else {
if(right == null)
return false;
else
return right.has(value);
}
}
public void printInOrder() {
if(left != null) {
left.printInOrder();
}
System.out.println(data);
if(right != null) {
right.printInOrder();
}
}
}