-
Notifications
You must be signed in to change notification settings - Fork 73
/
Node.java
42 lines (37 loc) · 1.05 KB
/
Node.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
class Node {
float value;
Node left;
Node right;
Node(float value) {
this.value = value;
}
void addNode(float value) {
if (value < this.value) {
if (this.left == null) {
this.left = new Node(value);
return;
}
this.left.addNode(value);
}
if (value > this.value) {
if (this.right == null) {
this.right = new Node(value);
return;
}
this.right.addNode(value);
}
}
void prefix(Node node) {
if (node.left != null) prefix(node.left);
System.out.println(node.value);
if(node.right != null) prefix(node.right);
}
boolean search(Node node,float value) {
if (value < node.value) {
if (node.left != null) return search(node.left, value);
} else if (value > node.value) {
if(node.right != null) return search(node.right, value);
} else return value == node.value;
return false;
}
}