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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
| /**
* B+树索引实现
*/
public class BPlusTreeIndex<K extends Comparable<K>, V> {
private static final int DEFAULT_ORDER = 4; // B+树的阶数
private Node<K, V> root;
private int order;
public BPlusTreeIndex() {
this(DEFAULT_ORDER);
}
public BPlusTreeIndex(int order) {
this.order = order;
this.root = new LeafNode<>();
}
/**
* 抽象节点类
*/
abstract static class Node<K extends Comparable<K>, V> {
List<K> keys;
boolean isLeaf;
Node(boolean isLeaf) {
this.keys = new ArrayList<>();
this.isLeaf = isLeaf;
}
abstract boolean isFull(int order);
abstract Node<K, V> split(int order);
abstract K getFirstKey();
}
/**
* 内部节点实现
*/
static class InternalNode<K extends Comparable<K>, V> extends Node<K, V> {
List<Node<K, V>> children;
InternalNode() {
super(false);
this.children = new ArrayList<>();
}
@Override
boolean isFull(int order) {
return keys.size() >= order - 1;
}
@Override
Node<K, V> split(int order) {
int mid = keys.size() / 2;
InternalNode<K, V> newNode = new InternalNode<>();
newNode.keys.addAll(keys.subList(mid + 1, keys.size()));
newNode.children.addAll(children.subList(mid + 1, children.size()));
keys.subList(mid, keys.size()).clear();
children.subList(mid + 1, children.size()).clear();
return newNode;
}
@Override
K getFirstKey() {
return keys.isEmpty() ? null : keys.get(0);
}
/**
* 插入子节点
*/
void insertChild(K key, Node<K, V> child) {
int pos = Collections.binarySearch(keys, key);
if (pos < 0) {
pos = -pos - 1;
}
keys.add(pos, key);
children.add(pos + 1, child);
}
}
/**
* 叶子节点实现
*/
static class LeafNode<K extends Comparable<K>, V> extends Node<K, V> {
List<V> values;
LeafNode<K, V> next; // 叶子节点链表,支持范围查询
LeafNode() {
super(true);
this.values = new ArrayList<>();
}
@Override
boolean isFull(int order) {
return keys.size() >= order;
}
@Override
Node<K, V> split(int order) {
int mid = keys.size() / 2;
LeafNode<K, V> newNode = new LeafNode<>();
newNode.keys.addAll(keys.subList(mid, keys.size()));
newNode.values.addAll(values.subList(mid, values.size()));
// 更新链表指针
newNode.next = this.next;
this.next = newNode;
keys.subList(mid, keys.size()).clear();
values.subList(mid, values.size()).clear();
return newNode;
}
@Override
K getFirstKey() {
return keys.isEmpty() ? null : keys.get(0);
}
/**
* 插入键值对
*/
void insert(K key, V value) {
int pos = Collections.binarySearch(keys, key);
if (pos >= 0) {
// 键已存在,更新值
values.set(pos, value);
} else {
// 插入新键值对
pos = -pos - 1;
keys.add(pos, key);
values.add(pos, value);
}
}
/**
* 查找值
*/
V find(K key) {
int pos = Collections.binarySearch(keys, key);
return pos >= 0 ? values.get(pos) : null;
}
}
/**
* 插入操作
*/
public void insert(K key, V value) {
InsertResult<K, V> result = insertInternal(root, key, value);
if (result.newChild != null) {
// 根节点分裂,创建新根
InternalNode<K, V> newRoot = new InternalNode<>();
newRoot.keys.add(result.promotedKey);
newRoot.children.add(root);
newRoot.children.add(result.newChild);
root = newRoot;
}
}
private InsertResult<K, V> insertInternal(Node<K, V> node, K key, V value) {
if (node.isLeaf) {
LeafNode<K, V> leaf = (LeafNode<K, V>) node;
leaf.insert(key, value);
if (leaf.isFull(order)) {
Node<K, V> newLeaf = leaf.split(order);
return new InsertResult<>(newLeaf.getFirstKey(), newLeaf);
}
return new InsertResult<>(null, null);
} else {
InternalNode<K, V> internal = (InternalNode<K, V>) node;
int childIndex = findChildIndex(internal, key);
InsertResult<K, V> result = insertInternal(
internal.children.get(childIndex), key, value);
if (result.newChild != null) {
internal.insertChild(result.promotedKey, result.newChild);
if (internal.isFull(order)) {
Node<K, V> newInternal = internal.split(order);
K promotedKey = internal.keys.remove(internal.keys.size() - 1);
return new InsertResult<>(promotedKey, newInternal);
}
}
return new InsertResult<>(null, null);
}
}
private int findChildIndex(InternalNode<K, V> node, K key) {
int pos = Collections.binarySearch(node.keys, key);
return pos >= 0 ? pos + 1 : -pos - 1;
}
/**
* 查找操作
*/
public V search(K key) {
return searchInternal(root, key);
}
private V searchInternal(Node<K, V> node, K key) {
if (node.isLeaf) {
LeafNode<K, V> leaf = (LeafNode<K, V>) node;
return leaf.find(key);
} else {
InternalNode<K, V> internal = (InternalNode<K, V>) node;
int childIndex = findChildIndex(internal, key);
return searchInternal(internal.children.get(childIndex), key);
}
}
/**
* 范围查询 - B+树的优势
*/
public List<V> rangeQuery(K startKey, K endKey) {
List<V> results = new ArrayList<>();
LeafNode<K, V> current = findLeafNode(startKey);
while (current != null) {
for (int i = 0; i < current.keys.size(); i++) {
K key = current.keys.get(i);
if (key.compareTo(startKey) >= 0 && key.compareTo(endKey) <= 0) {
results.add(current.values.get(i));
} else if (key.compareTo(endKey) > 0) {
return results;
}
}
current = current.next;
}
return results;
}
private LeafNode<K, V> findLeafNode(K key) {
Node<K, V> current = root;
while (!current.isLeaf) {
InternalNode<K, V> internal = (InternalNode<K, V>) current;
int childIndex = findChildIndex(internal, key);
current = internal.children.get(childIndex);
}
return (LeafNode<K, V>) current;
}
/**
* 插入结果辅助类
*/
static class InsertResult<K, V> {
final K promotedKey;
final Node<K, V> newChild;
InsertResult(K promotedKey, Node<K, V> newChild) {
this.promotedKey = promotedKey;
this.newChild = newChild;
}
}
}
|