-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleaf.java
More file actions
54 lines (45 loc) · 1.26 KB
/
leaf.java
File metadata and controls
54 lines (45 loc) · 1.26 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
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class leaf extends genericNode {
protected ArrayList<String> valueList;
protected leaf leafNext;
protected leaf leafPrevious;
public leaf(Double firstKey, String firstValue) {
LeafNodeCheck = true;
keyList = new ArrayList<Double>();
valueList = new ArrayList<String>();
keyList.add(firstKey);
valueList.add(firstValue);
}
public leaf(List<Double> newKeys, List<String> newValues) {
LeafNodeCheck = true;
keyList = new ArrayList<Double>(newKeys);
valueList = new ArrayList<String>(newValues);
}
/**
* This method inserts key/value pair into the leaf and keeps it sorted
* @param key
* @param value
*
*/
public void InsertAndSort(Double key, String value) {
if (key.compareTo(keyList.get(0)) < 0) {
keyList.add(0, key);
valueList.add(0, value);
} else if (key.compareTo(keyList.get(keyList.size() - 1)) > 0) {
keyList.add(key);
valueList.add(value);
} else {
ListIterator<Double> iterator = keyList.listIterator();
while (iterator.hasNext()) {
if (iterator.next().compareTo(key) > 0) {
int position = iterator.previousIndex();
keyList.add(position, key);
valueList.add(position, value);
break;
}
}
}
}
}