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+树代码的更多相关文章

  1. 《阿里巴巴Java开发手册》代码格式部分应用——idea中checkstyle的使用教程

    <阿里巴巴Java开发手册>代码格式部分应用--idea中checkstyle的使用教程 1.<阿里巴巴Java开发手册> 这是阿里巴巴工程师送给各位软件工程师的宝典,就像开车 ...

  2. 设计模式-Java版-全-附代码-超生动实例

    阅读推荐:设计模式-简单篇 项目地址:https://gitee.com/zwtgit/gof23 学习网站推荐: https://refactoringguru.cn/design-patterns ...

  3. (转载)JAVA动态编译--字节代码的操纵

    在一般的Java应用开发过程中,开发人员使用Java的方式比较简单.打开惯用的IDE,编写Java源代码,再利用IDE提供的功能直接运行Java 程序就可以了.这种开发模式背后的过程是:开发人员编写的 ...

  4. 转:java怎么用一行代码初始化ArrayList

    java怎么用一行代码初始化ArrayList 您可以创建一个工厂方法: public static ArrayList<String> createArrayList(String .. ...

  5. java中的静态代码块、构造代码块、构造方法

    运行下面这段代码,观察其结果: package com.test; public class HelloB extends HelloA { public HelloB() { } { System. ...

  6. 在Java中执行js代码

    在某些特定场景下,我们需要用Java来执行Js代码(如模拟登录时,密码被JS加密了的情况),操作如下: ScriptEngineManager mgr = new ScriptEngineManage ...

  7. java中执行js代码

    要在java中执行js代码,首先明白,java不支持浏览器本身的方法.支持自定义的js方法,否则会报错 先新建一个js文件:jsss.js 内容如下: function aa(a,b){ return ...

  8. 如何把java代码转换成smali代码

    1.概述 Smali是Android系统中Dalvik虚拟机指令语言,在apk逆向过程中有许多工具可以把smali代码转化成java代码.但是在学习Smali语法的过程中,有时候需要进行java代码和 ...

  9. JNI_最简单的Java调用C/C++代码

    JNI_最简单的Java调用C/C++代码 JNI.是Java Native Interface的简称,中文是"Java本地调用".通过这种技术能够做到下面两点: Java程序中的 ...

随机推荐

  1. Spring Cloud学习笔记【十】配置中心(消息驱动刷新配置)

    上一篇中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用 Webhook 的机制每次提交代码发送请求来刷新客户端,当客户端越来越多的时候,需要每个客户端都执行一遍,这种方案 ...

  2. 《机器学习系统设计》之应用scikit-learn做文本分类(上)

    前言: 本系列是在作者学习<机器学习系统设计>([美] WilliRichert)过程中的思考与实践,全书通过Python从数据处理.到特征project,再到模型选择,把机器学习解决这个 ...

  3. Android LruCache 压缩图片 有效避免程序OOM

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9316683 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工, ...

  4. 插入css样式表的三种方法

    http://www.w3school.com.cn/css/css_howto.asp http://www.runoob.com/css/css-howto.html 当读到一个样式表时,浏览器会 ...

  5. ADO.Net数据库帮助类

    public interface IDBHelper { /// <summary> /// 执行sql语句 /// </summary> /// <param name ...

  6. Spring-MVC:应用上下文webApplicationContext

    一.先说ServletContext javaee标准规定了,servlet容器需要在应用项目启动时,给应用项目初始化一个ServletContext作为公共环境容器存放公共信息.ServletCon ...

  7. 为什么选择Sqoop1

    1)大部分企业还在使用sqoop1版本 2)sqoop1能满足公司的基本需求 3)sqoop2 功能还不是很成熟和完善 4)sqoop只是一个工具而已,相对比较简单

  8. Sqoop 数据导入导出实践

    Sqoop是一个用来将hadoop和关系型数据库中的数据相互转移的工具,可以将一个关系型数据库(例如:mysql,oracle,等)中的数据导入到hadoop的HDFS中,也可以将HDFS的数据导入到 ...

  9. OpenGL编程逐步深入(四)Shaders

    OpenGl 中的 Shader在一些中文书籍或资料中都被翻译为"着色器", 单从字面意思也看不出Shader到底是什么,Shader实际上就是一段代码,用于完成特定功能的一个模块 ...

  10. sublime text 2 licence

    ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 6C0EEB94 BC ...