-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBPlusTree.java
More file actions
192 lines (161 loc) · 6.37 KB
/
BPlusTree.java
File metadata and controls
192 lines (161 loc) · 6.37 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import java.util.ArrayList;
import java.util.List;
class BPlusTreeNode {
boolean isLeaf;
List<Long> keys;
List<BPlusTreeNode> children;
BPlusTreeNode nextLeaf;
List<Trade> trades;
SegmentTree segTree;
public BPlusTreeNode(boolean isLeaf) {
this.isLeaf = isLeaf;
this.keys = new ArrayList<>();
this.children = new ArrayList<>();
this.trades = new ArrayList<>();
this.nextLeaf = null;
}
}
// Helper class to store and return query results
class QueryResult {
public int totalVolume = 0;
public double maxPrice = 0.0;
public double minPrice = Double.MAX_VALUE;
public boolean foundTrades = false;
}
public class BPlusTree {
private BPlusTreeNode root;
private int order;
public BPlusTree(int order) {
this.order = order;
this.root = new BPlusTreeNode(true);
}
// O(log N) search to find the correct leaf node
private BPlusTreeNode findLeafNode(BPlusTreeNode node, long timestamp) {
if (node.isLeaf) return node;
int i = 0;
while (i < node.keys.size() && timestamp >= node.keys.get(i)) {
i++;
}
return findLeafNode(node.children.get(i), timestamp);
}
// Simplified O(N) parent search - acknowledge the O(log N) alternative for presentation
private BPlusTreeNode findParent(BPlusTreeNode node, BPlusTreeNode child) {
if (node == null || node.isLeaf) return null;
for (BPlusTreeNode currentChild : node.children) {
if (currentChild == child) return node;
if (!currentChild.isLeaf) {
BPlusTreeNode parent = findParent(currentChild, child);
if (parent != null) return parent;
}
}
return null;
}
// --- CORE MUTATION METHODS ---
// O(log N) - Single insertion
public void insert(Trade trade) {
BPlusTreeNode node = findLeafNode(root, trade.getTimestamp());
// Find correct position to insert (maintains sorted order)
int i = 0;
while (i < node.trades.size() && node.trades.get(i).getTimestamp() < trade.getTimestamp()) {
i++;
}
node.trades.add(i, trade);
node.keys.add(i, trade.getTimestamp());
// Rebuild the segment tree for the leaf node
node.segTree = new SegmentTree(node.trades);
// Handle node splitting if it's full
if (node.trades.size() > order - 1) {
split(node);
}
}
// Simplified split logic
private void split(BPlusTreeNode node) {
BPlusTreeNode parent = findParent(root, node);
if (parent == null) {
parent = new BPlusTreeNode(false);
parent.children.add(node);
root = parent;
}
int midIndex = node.trades.size() / 2;
BPlusTreeNode newNode = new BPlusTreeNode(true);
newNode.trades = new ArrayList<>(node.trades.subList(midIndex, node.trades.size()));
newNode.keys = new ArrayList<>(node.keys.subList(midIndex, node.keys.size()));
node.trades.subList(midIndex, node.trades.size()).clear();
node.keys.subList(midIndex, node.keys.size()).clear();
newNode.nextLeaf = node.nextLeaf;
node.nextLeaf = newNode;
node.segTree = new SegmentTree(node.trades);
newNode.segTree = new SegmentTree(newNode.trades);
long newKey = newNode.keys.get(0);
int parentIndex = 0;
while (parentIndex < parent.keys.size() && newKey > parent.keys.get(parentIndex)) {
parentIndex++;
}
parent.keys.add(parentIndex, newKey);
parent.children.add(parentIndex + 1, newNode);
}
// O(log N + O(log B)) - Update
public boolean update(long timestamp, double newPrice, int newVolume) {
BPlusTreeNode node = findLeafNode(root, timestamp);
int index = -1;
for (int i = 0; i < node.trades.size(); i++) {
if (node.trades.get(i).getTimestamp() == timestamp) {
index = i;
break;
}
}
if (index != -1) {
Trade tradeToUpdate = node.trades.get(index);
tradeToUpdate.setPrice(newPrice);
tradeToUpdate.setVolume(newVolume);
node.segTree.updateTrade(index, tradeToUpdate);
return true;
}
return false;
}
// O(log N + O(B)) - Simplified Deletion
public boolean delete(long timestamp) {
BPlusTreeNode node = findLeafNode(root, timestamp);
int index = -1;
for (int i = 0; i < node.trades.size(); i++) {
if (node.trades.get(i).getTimestamp() == timestamp) {
index = i;
break;
}
}
if (index != -1) {
node.trades.remove(index);
node.keys.remove(index);
node.segTree = new SegmentTree(node.trades);
// NOTE: Missing B+ Tree Merge/Redistribution logic goes here.
return true;
}
return false;
}
// --- RANGE QUERY (O(log N + k * log B)) ---
public QueryResult rangeQuery(long startTimestamp, long endTimestamp) {
BPlusTreeNode currentNode = findLeafNode(root, startTimestamp);
QueryResult result = new QueryResult();
while (currentNode != null) {
int startIndex = 0;
while (startIndex < currentNode.trades.size() && currentNode.trades.get(startIndex).getTimestamp() < startTimestamp) {
startIndex++;
}
int endIndex = currentNode.trades.size() - 1;
while (endIndex >= 0 && currentNode.trades.get(endIndex).getTimestamp() > endTimestamp) {
endIndex--;
}
if (endIndex >= startIndex) {
result.foundTrades = true;
result.totalVolume += currentNode.segTree.querySumVolume(startIndex, endIndex);
result.maxPrice = Math.max(result.maxPrice, currentNode.segTree.queryMaxPrice(startIndex, endIndex));
result.minPrice = Math.min(result.minPrice, currentNode.segTree.queryMinPrice(startIndex, endIndex));
}
if (currentNode.keys.get(currentNode.keys.size() - 1) >= endTimestamp) {
break;
}
currentNode = currentNode.nextLeaf;
}
return result;
}
}