|
| 1 | +package code.collection.tree.btree; |
| 2 | + |
| 3 | +import com.google.common.collect.Lists; |
| 4 | +import org.apache.commons.lang3.tuple.Pair; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +/** |
| 11 | + * 〈B+树〉<p> |
| 12 | + * 〈功能详细描述〉 |
| 13 | + * |
| 14 | + * @author zixiao |
| 15 | + * @date 2019/12/20 |
| 16 | + * @see https://blog.csdn.net/Fmuma/article/details/80287924 |
| 17 | + */ |
| 18 | +public class BPlusTree { |
| 19 | + |
| 20 | + private TreeNode root; |
| 21 | + |
| 22 | + public static int m = 5; |
| 23 | + |
| 24 | + public BPlusTree(int m) { |
| 25 | + BPlusTree.m = m; |
| 26 | + } |
| 27 | + |
| 28 | + public BPlusTree() { |
| 29 | + this(5); |
| 30 | + } |
| 31 | + |
| 32 | + public boolean insert(int key, long dataAddr) { |
| 33 | + LeafNode leaf = null; |
| 34 | + List<IndexNode> parents = null; |
| 35 | + if (root == null) { |
| 36 | + leaf = new LeafNode(); |
| 37 | + root = leaf; |
| 38 | + parents = Collections.EMPTY_LIST; |
| 39 | + } else { |
| 40 | + Pair<LeafNode, List<IndexNode>> pair = findLeaf(key); |
| 41 | + leaf = pair.getKey(); |
| 42 | + parents = pair.getRight(); |
| 43 | + } |
| 44 | + TreeNode highNode = insertLeaf(parents, leaf, key, dataAddr); |
| 45 | + if (highNode != null) { |
| 46 | + root = highNode; |
| 47 | + } |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * 插入数据到叶子 |
| 53 | + * |
| 54 | + * @param parents |
| 55 | + * @param leaf |
| 56 | + * @param key |
| 57 | + * @param dataAddr |
| 58 | + * @return 新生成的最高节点 |
| 59 | + */ |
| 60 | + private TreeNode insertLeaf(List<IndexNode> parents, LeafNode leaf, int key, long dataAddr) { |
| 61 | + leaf.insert(key, dataAddr); |
| 62 | + if (leaf.keySize() < leaf.m()) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + // 若当前结点key的个数小于等于m-1,叶子结点分裂成左右两个叶子结点 |
| 66 | + return split(parents, leaf); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * 节点node分裂 |
| 71 | + * |
| 72 | + * @param parents |
| 73 | + * @param node |
| 74 | + * @return |
| 75 | + */ |
| 76 | + private TreeNode split(List<IndexNode> parents, TreeNode node) { |
| 77 | + Pair<TreeNode, Integer> pair = node.split(); |
| 78 | + TreeNode node2 = pair.getKey(); |
| 79 | + int newIndexKey = pair.getValue(); |
| 80 | + |
| 81 | + if (parents.isEmpty()) { |
| 82 | + return new IndexNode(newIndexKey, node, node2); |
| 83 | + } |
| 84 | + return insertIndex(parents.subList(1, parents.size()), parents.get(0), newIndexKey, node, node2); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * 插入key到索引节点 |
| 89 | + * |
| 90 | + * @param parents |
| 91 | + * @param node |
| 92 | + * @param key |
| 93 | + * @param left |
| 94 | + * @param right |
| 95 | + * @return 新生成的最高节点 |
| 96 | + */ |
| 97 | + private TreeNode insertIndex(List<IndexNode> parents, IndexNode node, int key, TreeNode left, TreeNode right) { |
| 98 | + node.insert(key, left, right); |
| 99 | + if (node.keySize() < node.m()) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + // 若当前结点key的个数小于等于m-1,索引结点分裂成左右两个索引结点 |
| 103 | + return split(parents, node); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * 查找叶关键字对应的叶子节点,及索引父节点 |
| 108 | + * |
| 109 | + * @param key |
| 110 | + * @return |
| 111 | + */ |
| 112 | + private Pair<LeafNode, List<IndexNode>> findLeaf(int key) { |
| 113 | + TreeNode node = root; |
| 114 | + List<IndexNode> parents = new ArrayList<>(); |
| 115 | + while (!node.isLeaf()) { |
| 116 | + parents.add((IndexNode) node); |
| 117 | + node = ((IndexNode) node).findChild(key); |
| 118 | + } |
| 119 | + Collections.reverse(parents); |
| 120 | + return Pair.of((LeafNode) node, parents); |
| 121 | + } |
| 122 | + |
| 123 | + public Long get(int key) { |
| 124 | + LeafNode node = findLeafNode(key); |
| 125 | + for (int i = 0; i < node.keySize(); i++) { |
| 126 | + if (node.keys()[i].equals(key)) { |
| 127 | + return node.getDataAddr()[i]; |
| 128 | + } |
| 129 | + } |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * 查找叶关键字对应的叶子节点 |
| 135 | + * |
| 136 | + * @param key |
| 137 | + * @return |
| 138 | + */ |
| 139 | + private LeafNode findLeafNode(int key) { |
| 140 | + TreeNode node = root; |
| 141 | + while (!node.isLeaf()) { |
| 142 | + node = ((IndexNode) node).findChild(key); |
| 143 | + } |
| 144 | + return (LeafNode) node; |
| 145 | + } |
| 146 | + |
| 147 | + public List<Long> range(int fromKey, int toKey) { |
| 148 | + List<Long> dataList = new ArrayList<>(); |
| 149 | + LeafNode from = findLeafNode(fromKey); |
| 150 | + LeafNode to = null; |
| 151 | + Integer fromLast = from.keys[from.keySize - 1]; |
| 152 | + |
| 153 | + //1 数据在一个节点上 |
| 154 | + if (fromLast > toKey || (to = findLeafNode(toKey)) == from) { |
| 155 | + for (int i = 0; i < from.keySize; i++) { |
| 156 | + if (from.keys[i] >= fromKey && from.keys[i] <= toKey) { |
| 157 | + dataList.add(from.getDataAddr()[i]); |
| 158 | + } |
| 159 | + } |
| 160 | + return dataList; |
| 161 | + } |
| 162 | + |
| 163 | + //2 数据分布在多个节点上 |
| 164 | + LeafNode node = from; |
| 165 | + while (node != null) { |
| 166 | + if (node != from && node != to) { |
| 167 | + //a 既不是开始又不是结束节点,则所有数据都加上 |
| 168 | + for (int i = 0; i < node.keySize; i++) { |
| 169 | + dataList.add(node.getDataAddr()[i]); |
| 170 | + } |
| 171 | + } else if (node == from) { |
| 172 | + //b 开始节点,只取大于fromKey的数据 |
| 173 | + for (int i = 0; i < node.keySize; i++) { |
| 174 | + if (node.keys[i] >= fromKey) { |
| 175 | + dataList.add(node.getDataAddr()[i]); |
| 176 | + } |
| 177 | + } |
| 178 | + } else if (node == to) { |
| 179 | + //c 结束节点,只取小于toKey的数据 |
| 180 | + for (int i = 0; i < node.keySize; i++) { |
| 181 | + if (node.keys[i] <= toKey) { |
| 182 | + dataList.add(node.getDataAddr()[i]); |
| 183 | + } |
| 184 | + } |
| 185 | + break; |
| 186 | + } |
| 187 | + node = node.getNext(); |
| 188 | + } |
| 189 | + return dataList; |
| 190 | + } |
| 191 | + |
| 192 | + public void print() { |
| 193 | + StringBuilder sb = new StringBuilder(); |
| 194 | + append(sb, Lists.newArrayList(root)); |
| 195 | + System.out.println(sb); |
| 196 | + } |
| 197 | + |
| 198 | + private void append(StringBuilder sb, List<TreeNode> nodeList) { |
| 199 | + List<TreeNode> children = new ArrayList<>(); |
| 200 | + for (TreeNode node : nodeList) { |
| 201 | + if (node == null) { |
| 202 | + continue; |
| 203 | + } else if (node.isLeaf()) { |
| 204 | + sb.append("["); |
| 205 | + joinKeys(sb, ",", node.keys()); |
| 206 | + sb.append("]").append("->"); |
| 207 | + } else { |
| 208 | + children.addAll(Lists.newArrayList(((IndexNode) node).children())); |
| 209 | + sb.append("["); |
| 210 | + joinKeys(sb, " ", node.keys()); |
| 211 | + sb.append("]"); |
| 212 | + sb.append("\t"); |
| 213 | + } |
| 214 | + } |
| 215 | + sb.append("\n\r"); |
| 216 | + if (!children.isEmpty()) { |
| 217 | + append(sb, children); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + private void joinKeys(StringBuilder sb, CharSequence delimiter, Integer[] keys) { |
| 222 | + boolean first = true; |
| 223 | + for (Integer key : keys) { |
| 224 | + if (key == null) { |
| 225 | + break; |
| 226 | + } |
| 227 | + if (first) { |
| 228 | + first = false; |
| 229 | + } else { |
| 230 | + sb.append(delimiter); |
| 231 | + } |
| 232 | + sb.append(key); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + public static void main(String[] args) { |
| 237 | + BPlusTree bPlusTree = new BPlusTree(5); |
| 238 | + /** |
| 239 | + * m=5 |
| 240 | + * [11] |
| 241 | + * [7 9] [13 15] |
| 242 | + * [5,6]->[7,8]->[9,10]->[11,12]->[13,14]->[15,16,17] |
| 243 | + */ |
| 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); |
| 257 | + bPlusTree.print(); |
| 258 | + |
| 259 | + /** |
| 260 | + * [11 , 17] |
| 261 | + * [7, 9,] [13, 15] [19, 21] |
| 262 | + * [5,6]->[7,8] ->[9,10]->[11,12]->[13,14]->[15,16]->[17,18]->[19,20]->[21,22,23] |
| 263 | + */ |
| 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); |
| 270 | + bPlusTree.print(); |
| 271 | + |
| 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)); |
| 275 | + |
| 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(); |
| 281 | + |
| 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(); |
| 287 | + |
| 288 | + } |
| 289 | + |
| 290 | +} |
0 commit comments