Skip to content

Commit 1ade338

Browse files
committed
Starting with binary search tree
1 parent 7dfa6a3 commit 1ade338

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="main"/>
4+
<classpathentry kind="src" path="test"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>BinarySearchTree</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
8+
public class BinarySearchTree<T extends Comparable<T>> {
9+
10+
private List<T> bst = new ArrayList<>();
11+
12+
private Comparator<T> customComparator = Comparator.naturalOrder();
13+
14+
public BinarySearchTree(Comparator<T> customComparator) {
15+
this.customComparator = customComparator;
16+
}
17+
18+
public void insert(T e){
19+
if (contains(e)) {
20+
bst.add(e);
21+
Collections.sort(bst, customComparator);
22+
}
23+
}
24+
25+
public T root(){
26+
return bst.get(rootIndex());
27+
}
28+
29+
private int rootIndex(){
30+
return isOdd() ? halfSize() + 1 : halfSize();
31+
}
32+
33+
private int midpoint(List<T> l){
34+
return isOdd(l) ? halfSize(l) + 1 : halfSize(l);
35+
}
36+
37+
private int midpointIndex(List<T> l){
38+
return midpoint(l) - 1;
39+
}
40+
41+
42+
public boolean contains(T e){
43+
List<T> searchTree = bst;
44+
int upperBound = bst.size() - 1;
45+
int lowerBound = 0;
46+
47+
while (lowerBound < upperBound) {
48+
if (e.equals(searchTree.get(midpointIndex(searchTree)))) {
49+
return true;
50+
} else {
51+
if (customComparator.) {
52+
53+
}
54+
55+
56+
}
57+
}
58+
}
59+
60+
public void remove(T e){
61+
bst.remove(e);
62+
}
63+
64+
public int size(){
65+
return bst.size();
66+
}
67+
68+
public List<T> childs(T e){
69+
70+
}
71+
72+
73+
private boolean isOdd() {
74+
return isOdd(bst);
75+
}
76+
77+
private boolean isOdd(List<T> l) {
78+
return halfSize(l) != 0;
79+
}
80+
81+
private int halfSize() {
82+
return halfSize(bst);
83+
}
84+
85+
private int halfSize(List<T> l) {
86+
return l.size()/2;
87+
}
88+
}

0 commit comments

Comments
 (0)