-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeafNode.java
57 lines (49 loc) · 1.22 KB
/
LeafNode.java
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
package BPlusTree;
import java.util.ArrayList;
import java.util.List;
import BPlusTree.Node;
import BPlusTree.LeafKey;
public class LeafNode extends Node {
List<LeafKey> leafKeys;
LeafNode leftLeafNode;//used for range retrieval
LeafNode rightLeafNode;
public LeafNode() {
leftLeafNode = new LeafNode();
rightLeafNode = new LeafNode();
}
public LeafNode(LeafNode left, LeafNode right) {
super();
isLeafNode = true;
leafKeys = new ArrayList<LeafKey>();
leftLeafNode = left;
rightLeafNode = right;
}
//adds key value pair in an ordered way to the invoking leaf node
public boolean addKeyValToLeafNode(double key, String val) {
//detects if key exists already
boolean isDuplicate = false;
int i;
//get position to insert
for(i=0; i<leafKeys.size(); i++) {
if(leafKeys.get(i).keyValue == key) {
//duplicate key
isDuplicate = true;
break;
}
else if(leafKeys.get(i).keyValue > key) {
//breaks when greater value encountered. no point in looping then
break;
}
}
//duplicate, just add data. unsorted
if(isDuplicate) {
leafKeys.get(i).leafData.add(val);
return true;
}
//add new key value pair
else {
leafKeys.add(i, new LeafKey(key, val));
return true;
}
}
}