Skip to content

Commit b15402b

Browse files
committed
Added binary tree implementation for Java, started grouping data structure implementations by language
1 parent 47a0dec commit b15402b

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
public class Node<T extends Comparable> {
2+
3+
public Node left;
4+
public Node right;
5+
public T data;
6+
7+
public Node(T data){
8+
this.left = null;
9+
this.right = null;
10+
this.data = data;
11+
}
12+
13+
public void printTree(){
14+
if (left != null){
15+
left.printTree();
16+
}
17+
System.out.println(data);
18+
if (right != null){
19+
right.printTree();
20+
}
21+
}
22+
23+
public void insert(T data){
24+
if (this.data != null){
25+
if (this.data.compareTo(data) < 0){
26+
if (this.left == null){
27+
this.left = new Node<T>(data);
28+
return;
29+
} else{
30+
this.left.insert(data);
31+
}
32+
}
33+
else if (this.data.compareTo(data) > 0){
34+
if (this.right == null){
35+
this.right = new Node<T>(data);
36+
return;
37+
} else{
38+
this.right.insert(data);
39+
}
40+
}
41+
}
42+
else{
43+
this.data = data;
44+
}
45+
}
46+
47+
public void search_value(T data){
48+
if (this.data == data){
49+
System.out.println(String.format("Value %s is found", data));
50+
} else if (this.data.compareTo(data) < 0){
51+
this.left.search_value(data);
52+
return;
53+
} else if (this.data.compareTo(data) > 0){
54+
this.right.search_value(data);
55+
return;
56+
} else{
57+
System.out.println(String.format("Value %s does not exist!", data));
58+
return;
59+
}
60+
}
61+
root = Node(Integer(12));
62+
63+
}
File renamed without changes.

0 commit comments

Comments
 (0)