Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4b3bc1c

Browse files
committed
Moving BST files
1 parent 7553987 commit 4b3bc1c

File tree

5 files changed

+81
-112
lines changed

5 files changed

+81
-112
lines changed

‎classical/data-structures/binary-search-tree/java/.classpath‎

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎classical/data-structures/binary-search-tree/java/.project‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎classical/data-structures/binary-search-tree/java/main/BinarySearchTree.java‎

Lines changed: 0 additions & 88 deletions
This file was deleted.

‎java/.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/bin/
2+
.project
3+
.classpath

‎java/main/BinarySearchTree.java‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class BinarySearchTree {
8+
9+
private List<Integer> bst = new ArrayList<>();
10+
11+
public void insert(int e){
12+
if (contains(e)) {
13+
bst.add(e);
14+
Collections.sort(bst);
15+
}
16+
}
17+
18+
public int root(){
19+
return bst.get(rootIndex());
20+
}
21+
22+
private int rootIndex(){
23+
return isOdd() ? halfSize() + 1 : halfSize();
24+
}
25+
26+
private int midpoint(List<Integer> l){
27+
return isOdd(l) ? halfSize(l) + 1 : halfSize(l);
28+
}
29+
30+
private int midpointIndex(List<Integer> l){
31+
return midpoint(l) - 1;
32+
}
33+
34+
public boolean contains(int e){
35+
List<Integer> searchTree = bst;
36+
int upperBound = bst.size();
37+
int lowerBound = 0;
38+
39+
while (lowerBound < upperBound) {
40+
searchTree = searchTree.subList(lowerBound, upperBound);
41+
Integer middleman = searchTree.get(midpointIndex(searchTree));
42+
if (middleman.equals(e)) {
43+
return true;
44+
} else {
45+
if (e > middleman) {
46+
lowerBound = middleman + 1;
47+
} else {
48+
upperBound = middleman - 1;
49+
}
50+
}
51+
}
52+
return false;
53+
}
54+
55+
public void remove(int e){
56+
bst.remove(e);
57+
}
58+
59+
public int size(){
60+
return bst.size();
61+
}
62+
63+
private boolean isOdd() {
64+
return isOdd(bst);
65+
}
66+
67+
private boolean isOdd(List<Integer> l) {
68+
return halfSize(l) != 0;
69+
}
70+
71+
private int halfSize() {
72+
return halfSize(bst);
73+
}
74+
75+
private int halfSize(List<Integer> l) {
76+
return l.size()/2;
77+
}
78+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /