java / C++ B+树代码
C++ 代码
#include <>
JAVA 代码
package org.test.me.struct; /**
* author: shuly
* create: 2018-09-11 上午11:38
* Description: 和 b+tree 的区别是 没有 维护叶子结点的 链表结构
*/ @SuppressWarnings("unchecked")
public class SimpleBPTree<K extends Comparable<K>,V> { private static final class Node {
int m;
Entry[] children;
Node(int max) {
this.m = 0;
children = new Entry[max];
} Entry popHead() {
Entry ans = children[0];
for(int i = 0; i < m - 1; ++i){
children[i] = children[i+1];
}
children[--m] = null;
return ans;
} void pushHead(Entry key) {
for(int i = m ; i > 0 ; --i) {
children[i] = children[i-1];
}
children[0] = key;
++m;
} Entry popBack() {
Entry ans = children[--m];
children[m] = null;
return ans;
} void pushBack(Entry key) {
children[m++] = key;
} void pushBack(Node one) {
for(int i = 0 ; i < one.m; ++i) {
children[m++] = one.children[i];
}
} Entry pop(int i) {
Entry ans = children[i];
for(; i + 1< m; ++i) {
children[i] = children[i+1];
}
children[--m] = null;
return ans;
}
} private static class Entry {
private Comparable key;
private Object val;
private Node next; // 里面结点的值大于等于 Key
public Entry(Comparable key, Object val, Node next) {
this.key = key;
this.val = val;
this.next = next;
}
} private static int M = 6;
private static int MIN = M / 2;
private int size;
private int height;
private Node root; public SimpleBPTree() {
root = new Node(M);
size = 0;
height = 1;
} public int size() {
return size;
} public boolean isEmpty() {
return size() == 0;
} public void put(K k, V v) {
assert k != null;
Node aha = insert(root, k, v, 1);
++ size;
if (aha != null) {
Node R = new Node(M);
R.children[0] = new Entry(root.children[0].key, null, root);
R.children[1] = new Entry(aha.children[0].key, null, aha);
R.m = 2;
this.root = R;
++ height;
}
} public V get(K key) {
assert key != null;
return find(root, key, 1);
} public V remove(K key) {
assert key != null;
V ans = delete(root, key, 1);
if (root.m == 1 && height != 1) {
root = root.children[0].next;
-- height;
}
return ans;
} private V delete(Node now, K k, int h) {
if (h == height) {
for(int i = 0 ; i < now.m; ++i) {
int ret = k.compareTo((K) now.children[i].key);
if (ret == 0) {
size--;
return (V) now.pop(i).val;
}
else if (ret < 0) return null;
}
return null;
} else {
for(int i = 0; i < now.m; ++i) {
if (i + 1 == now.m || k.compareTo((K) now.children[i + 1].key) < 0) {
V ans = delete(now.children[i].next, k, h + 1);
now.children[i].key = now.children[i].next.children[0].key;
if (now.children[i].next.m < MIN) { // 想办法凑凑, 凑不够就 合并
if(i + 1 != now.m && now.children[i + 1].next.m > MIN) {
// 右侧的失主! 请把第一个给我
Entry key = now.children[i + 1].next.popHead();
now.children[i+1].key = now.children[i+1].next.children[0].key; now.children[i].next.pushBack(key);
} else if (i != 0 && now.children[i - 1].next.m > MIN) {
// 左侧的失主! 请把最后一个给我
Entry key = now.children[i - 1].next.popBack(); now.children[i].next.pushHead(key);
now.children[i].key = now.children[i].next.children[0].key;
} else {
// 找一个老实人 一起过
int start;
if (i + 1 != now.m) {
now.children[i].next.pushBack(now.children[i+1].next);
start = i + 1;
now.pop(start);
} else if (i != 0) {
now.children[i-1].next.pushBack(now.children[i].next);
start = i;
now.pop(start);
}
// 根 的缩水没有枚举
}
}
return ans;
}
}
// BKN
return null;
}
} private Node insert(Node now, K k, V v, int h) {
Entry in = new Entry(k, v, null);
int pos;
if (h == height) { // leaf
for(pos = 0; pos < now.m; ++pos) {
if (k.compareTo((K) now.children[pos].key) <= 0) break;
}
} else {
for(pos = 0 ; pos < now.m; ++pos) {
if (pos+1 == now.m || (k.compareTo((K) now.children[pos+1].key) < 0)) {
Node one = insert(now.children[pos].next, k, v, h + 1);
if (one == null) return null;
in.key = one.children[0].key;
in.next = one;
in.val = null;
++ pos;
break;
}
}
}
for(int j = now.m; j > pos; --j) {
now.children[j] = now.children[j-1];
}
now.children[pos] = in;
++ now.m;
if (now.m < M) return null;
else return split(now);
} private Node split(Node node) {
Node next = new Node(M);
next.m = node.m = MIN;
for (int j = 0; j < MIN; j++) {
next.children[j] = node.children[MIN + j];
node.children[MIN + j] = null;
}
return next;
} private V find(Node now, K key, int h){
Entry[] children = now.children;
if (h == height) {
for (int j = 0; j < now.m; j++) {
if (key.compareTo((K) children[j].key) == 0 ) {
return (V) children[j].val;
}
}
} else {
for (int j = 0; j < now.m; j++) {
if (j+1 == now.m || (key.compareTo((K) children[j+1].key) < 0)) {
return find(children[j].next, key, h + 1);
}
}
}
return null;
} public String toString() {
return toString(root, 1, "") + "\n";
} private String toString(Node h, int ht, String indent) {
StringBuilder s = new StringBuilder();
Entry[] children = h.children;
if (ht == height) {
for (int j = 0; j < h.m; j++) {
s.append(indent).append(children[j].key).append(" ").append((children[j]).val).append("\n");
}
} else {
for (int j = 0; j < h.m; j++) {
s.append(indent).append("(").append(children[j].key).append(")").append("\n");
s.append(toString((children[j]).next, ht + 1, indent + " "));
}
}
return s.toString();
} public static void main(String[] args) {
SimpleBPTree<Integer, String> map = new SimpleBPTree<>(); for(int i = 1 ; i <= 10; ++ i) {
map.put(i, i + "");
}
for(int i = 20; i >= 11; --i) {
map.put(i, i + "");
}
for(int i = 21; i <= 30 ;++i) {
map.put(i, i + "");
}
map.remove(1);
map.remove(3); map.remove(12);
map.remove(11);
map.remove(10); map.remove(2);
map.remove(4);
map.remove(5);
map.remove(6); map.remove(18);
map.remove(13);
map.remove(14);
map.remove(15);
map.remove(16);
map.remove(17);
map.remove(19);
map.remove(7);
map.remove(8);
map.remove(9);
map.remove(20); map.remove(21);
map.remove(22);
map.remove(23);
map.remove(27); map.remove(28);
map.remove(29);
map.remove(30);
map.remove(24);
map.remove(25);
map.remove(26); System.out.println(map.toString());
}
}
java / C++ B+树代码的更多相关文章
- 《阿里巴巴Java开发手册》代码格式部分应用——idea中checkstyle的使用教程
<阿里巴巴Java开发手册>代码格式部分应用--idea中checkstyle的使用教程 1.<阿里巴巴Java开发手册> 这是阿里巴巴工程师送给各位软件工程师的宝典,就像开车 ...
- 设计模式-Java版-全-附代码-超生动实例
阅读推荐:设计模式-简单篇 项目地址:https://gitee.com/zwtgit/gof23 学习网站推荐: https://refactoringguru.cn/design-patterns ...
- (转载)JAVA动态编译--字节代码的操纵
在一般的Java应用开发过程中,开发人员使用Java的方式比较简单.打开惯用的IDE,编写Java源代码,再利用IDE提供的功能直接运行Java 程序就可以了.这种开发模式背后的过程是:开发人员编写的 ...
- 转:java怎么用一行代码初始化ArrayList
java怎么用一行代码初始化ArrayList 您可以创建一个工厂方法: public static ArrayList<String> createArrayList(String .. ...
- java中的静态代码块、构造代码块、构造方法
运行下面这段代码,观察其结果: package com.test; public class HelloB extends HelloA { public HelloB() { } { System. ...
- 在Java中执行js代码
在某些特定场景下,我们需要用Java来执行Js代码(如模拟登录时,密码被JS加密了的情况),操作如下: ScriptEngineManager mgr = new ScriptEngineManage ...
- java中执行js代码
要在java中执行js代码,首先明白,java不支持浏览器本身的方法.支持自定义的js方法,否则会报错 先新建一个js文件:jsss.js 内容如下: function aa(a,b){ return ...
- 如何把java代码转换成smali代码
1.概述 Smali是Android系统中Dalvik虚拟机指令语言,在apk逆向过程中有许多工具可以把smali代码转化成java代码.但是在学习Smali语法的过程中,有时候需要进行java代码和 ...
- JNI_最简单的Java调用C/C++代码
JNI_最简单的Java调用C/C++代码 JNI.是Java Native Interface的简称,中文是"Java本地调用".通过这种技术能够做到下面两点: Java程序中的 ...
随机推荐
- 使用jemdoc制作个人主页
jemdoc官网说明: http://jemdoc.jaboc.net/index.html 作者的个人主页:https://jemnz.com/ 将下载的jemdoc.py文件和需要转化的xxx.j ...
- Map和Collection详解
Collection -----List -----LinkedList 非同步 ----ArrayList 非同 ...
- [Poi] Use Poi to Build an Index.js with Modern JavaScript Features
Poi can easily launch an index.js file simply by running the poi command. This will launch a dev-ser ...
- p2p項目夭折,有種蛋蛋的憂傷。。
在高考完的暑假就在跟杰哥讨论怎样实现的校内p2p文件共享,就在今天.我们无奈的宣布差点儿夭折. 上图是測试图. 那时候的思路已经完好.就是:"学生上传共享文件到咱们工作室的server. ...
- Delphi新语法 For ..In
首先我们要知道哪些类型可以用For In吧,下面就是: for Element in ArrayExpr do Stmt; 数组 for Element in StringExpr do S ...
- quartz-misfire 错失、补偿执行
调度(scheduleJob)或恢复调度(resumeTrigger,resumeJob)后不同的misfire对应的处理规则 misfire产生的条件是:到了该触发执行时上一个执行还未完成,且线程池 ...
- @Html.Raw() 方法输出带有html标签的字符串
@Html.Raw() 方法输出带有html标签的字符串,如:@Html.Raw("<div style='color:red'>输出字符串</div>") ...
- 如何在 Linux 上安装应用程序
如何在 Linux 上安装应用程序 编译自:https://opensource.com/article/18/1/how-install-apps-linux作者: Seth Kenlon原创:LC ...
- codeforces 140E.New Year Garland
传送门: 解题思路: 要求相邻两行小球颜色集合不同,并且限制行内小球相邻不同. 由此可得:每行小球排列都是独立与外界的, 所以答案应该是对于所有行的颜色集合分类,在将行内的答案乘到上面. 先考虑如何分 ...
- GPU-directX的发展历史
GPU发展历史: GPU之前的基础: 1962 麻省理工学院的博士伊凡•苏泽兰发表的论文以及他的画板程序奠定了计算机图形学的基础. 1962-1984 没有专门图形处理硬件,由CPU完成 1984 专 ...