forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
61 lines (56 loc) · 1.57 KB
/
Node.java
File metadata and controls
61 lines (56 loc) · 1.57 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
public class Node<T extends Comparable> {
public Node left;
public Node right;
public T data;
public Node(T data){
this.left = null;
this.right = null;
this.data = data;
}
public void printTree(){
if (left != null){
left.printTree();
}
System.out.println(data);
if (right != null){
right.printTree();
}
}
public void insert(T data){
if (this.data != null){
if (this.data.compareTo(data) < 0){
if (this.left == null){
this.left = new Node<T>(data);
return;
} else{
this.left.insert(data);
}
}
else if (this.data.compareTo(data) > 0){
if (this.right == null){
this.right = new Node<T>(data);
return;
} else{
this.right.insert(data);
}
}
}
else{
this.data = data;
}
}
public void search_value(T data){
if (this.data == data){
System.out.println(String.format("Value %s is found", data));
} else if (this.data.compareTo(data) < 0){
this.left.search_value(data);
return;
} else if (this.data.compareTo(data) > 0){
this.right.search_value(data);
return;
} else{
System.out.println(String.format("Value %s does not exist!", data));
return;
}
}
}