Skip to content

Commit f11c66c

Browse files
committed
b+树
1 parent ef3420d commit f11c66c

File tree

5 files changed

+51
-108
lines changed

5 files changed

+51
-108
lines changed

src/main/java/code/collection/tree/ArrayCompleteBinTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Arrays;
44

55
/**
6-
* 〈一句话功能简述〉<p>
6+
* 〈数组实现完全二叉树〉<p>
77
* 〈功能详细描述〉
88
*
99
* @author zixiao

src/main/java/code/collection/tree/BinarySearchTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
/**
8-
* 〈一句话功能简述〉<p>
8+
* 〈二分查找树〉<p>
99
* 〈功能详细描述〉
1010
*
1111
* @author zixiao

src/main/java/code/collection/tree/ListCompleteBinTree.java

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

src/main/java/code/collection/tree/btree/BPlusTree.java

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
/**
1111
* 〈B+树〉<p>
12-
* 〈功能详细描述〉
13-
*
12+
* h = log m (N), m越大,h越小
1413
* @author zixiao
1514
* @date 2019/12/20
1615
* @see https://blog.csdn.net/Fmuma/article/details/80287924
@@ -19,7 +18,7 @@ public class BPlusTree {
1918

2019
private TreeNode root;
2120

22-
public static int m = 5;
21+
public static int m;
2322

2423
public BPlusTree(int m) {
2524
BPlusTree.m = m;
@@ -147,11 +146,10 @@ private LeafNode findLeafNode(int key) {
147146
public List<Long> range(int fromKey, int toKey) {
148147
List<Long> dataList = new ArrayList<>();
149148
LeafNode from = findLeafNode(fromKey);
150-
LeafNode to = null;
151-
Integer fromLast = from.keys[from.keySize - 1];
149+
Integer fromMaxKey = from.keys[from.keySize - 1];
152150

153151
//1 数据在一个节点上
154-
if (fromLast > toKey || (to = findLeafNode(toKey)) == from) {
152+
if (fromMaxKey >= toKey) {
155153
for (int i = 0; i < from.keySize; i++) {
156154
if (from.keys[i] >= fromKey && from.keys[i] <= toKey) {
157155
dataList.add(from.getDataAddr()[i]);
@@ -161,6 +159,7 @@ public List<Long> range(int fromKey, int toKey) {
161159
}
162160

163161
//2 数据分布在多个节点上
162+
LeafNode to = findLeafNode(toKey);
164163
LeafNode node = from;
165164
while (node != null) {
166165
if (node != from && node != to) {
@@ -241,50 +240,54 @@ public static void main(String[] args) {
241240
* [7 9] [13 15]
242241
* [5,6]->[7,8]->[9,10]->[11,12]->[13,14]->[15,16,17]
243242
*/
244-
bPlusTree.insert(5, 1);
245-
bPlusTree.insert(6, 2);
246-
bPlusTree.insert(7, 3);
247-
bPlusTree.insert(8, 4);
248-
bPlusTree.insert(9, 5);
249-
bPlusTree.insert(10, 6);
250-
bPlusTree.insert(11, 7);
251-
bPlusTree.insert(12, 8);
252-
bPlusTree.insert(13, 9);
253-
bPlusTree.insert(14, 10);
254-
bPlusTree.insert(15, 11);
255-
bPlusTree.insert(16, 12);
256-
bPlusTree.insert(17, 13);
243+
for (int i = 5; i <= 17; i++) {
244+
bPlusTree.insert(i, i+100);
245+
}
257246
bPlusTree.print();
258247

259248
/**
260249
* [11 , 17]
261-
* [7, 9,] [13, 15] [19, 21]
250+
* [7, 9] [13, 15] [19, 21]
262251
* [5,6]->[7,8] ->[9,10]->[11,12]->[13,14]->[15,16]->[17,18]->[19,20]->[21,22,23]
263252
*/
264-
bPlusTree.insert(18, 14);
265-
bPlusTree.insert(19, 15);
266-
bPlusTree.insert(20, 16);
267-
bPlusTree.insert(21, 17);
268-
bPlusTree.insert(22, 18);
269-
bPlusTree.insert(23, 19);
253+
for (int i = 18; i <= 23; i++) {
254+
bPlusTree.insert(i, i+100);
255+
}
270256
bPlusTree.print();
271257

272-
System.out.println("Get key " + 4 + ": " + bPlusTree.get(4));
273-
System.out.println("Get key " + 9 + ": " + bPlusTree.get(9));
274-
System.out.println("Get key " + 23 + ": " + bPlusTree.get(23));
258+
/**
259+
* [11 17 23]
260+
* [7 9] [13 15] [19 21] [25 27 29]
261+
* [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]->
262+
*/
263+
for (int i = 24; i <= 31; i++) {
264+
bPlusTree.insert(i, i+100);
265+
}
266+
bPlusTree.print();
275267

276-
System.out.print("Range key [" + 5 + ", " + 6 + "]: ");
277-
bPlusTree.range(5, 6).forEach(l -> {
278-
System.out.print(l + " ");
279-
});
280-
System.out.println();
268+
printGet(bPlusTree,4);
269+
printGet(bPlusTree,9);
270+
printGet(bPlusTree,23);
281271

282-
System.out.print("Range key [" + 10 + ", " + 13 + "]: ");
283-
bPlusTree.range(10, 13).forEach(l -> {
284-
System.out.print(l + " ");
285-
});
286-
System.out.println();
272+
printRange(bPlusTree,5, 6);
273+
printRange(bPlusTree,17, 21);
274+
printRange(bPlusTree,29, 40);
275+
printRange(bPlusTree,32, 40);
287276

277+
for (int i = 32; i <= 100; i++) {
278+
bPlusTree.insert(i, i+100);
279+
}
280+
bPlusTree.print();
281+
}
282+
283+
private static void printGet(BPlusTree bPlusTree, int key){
284+
System.out.println("Get key " + key + ": " + bPlusTree.get(key));
285+
}
286+
287+
private static void printRange(BPlusTree bPlusTree, int from, int to){
288+
System.out.print("Range key [" + from + ", " + to + "]: ");
289+
bPlusTree.range(from, to).forEach(l -> System.out.print(l + " "));
290+
System.out.println();
288291
}
289292

290293
}

src/main/java/code/collection/tree/btree/IndexNode.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ public TreeNode[] children(){
3535
}
3636

3737
public TreeNode findChild(int key){
38-
for (int i = 0; i < keySize; i++) {
39-
if(key < keys[i]){
38+
if (key < keys[0]) {
39+
return children[0];
40+
} else if (key == keys[0]) {
41+
return children[1];
42+
}
43+
for (int i = 1; i < keySize; i++) {
44+
if (key > keys[i-1] && key < keys[i]) {
4045
return children[i];
4146
}else if(key == keys[i]){
4247
return children[i+1];

0 commit comments

Comments
 (0)