哈夫曼编码（基于哈夫曼树-最优二叉树，不唯一）、B树(b-树)、B+树

http://blog.csdn.net/shuangde800/article/details/7341289

http://www.cnblogs.com/Jezze/archive/2011/12/23/2299884.html

http://blog.csdn.net/jdhanhua/article/details/6621026

B树介绍：点击打开链接

tire树：点击打开链接    点击打开链接

树集合：点击打开链接

1.定义：

[cpp]
view plain

1. if(score<60)
3. else if(score<70)
4.     cout<<“Pass”<<endl
5. else if(score<80)
6.     cout<<“General”<<endl;
7. else if(score<90)
8.     cout<<“Good”<<endl;
9. else
10.     cout<<“Very good!”<<endl;

JPEG中就应用了哈夫曼编码。 首先介绍什么是哈夫曼树。哈夫曼树又称最优二叉树，

，N个权值Wi(i=1,2,…n)构成一棵有N个叶结点的二叉树，相应的叶结点的路径

2.哈夫曼编码步骤：

1. 将TFile中7个字符都作为叶子结点，每个字符出现次数作为该叶子结点的权值
2.规定哈夫曼树中所有左分支表示字符0，所有右分支表示字符1,将依次从根结点到每个叶子结点所经过的分支的二进制位的序列作为该

3. 由于从根结点到任何一个叶子结点都不可能经过其他叶子，这种编码一定是前缀编码，哈夫曼树的带权路径长度正好是文件TFile编码

3.代码实现：

``<span style="font-family: Arial, Helvetica, sans-serif;">//首先：定义哈夫曼树的节点类，为了方便使用集合类的排序功能，实现了Comparable接口(可以不是实现该接口，此时需要实现排序功能)</span>``
``````
public class Node<T> implements Comparable<Node<T>> {
private T data;
private double weight;
private Node<T> left;
private Node<T> right;

public Node(T data, double weight){
this.data = data;
this.weight = weight;
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}

public double getWeight() {
return weight;
}

public void setWeight(double weight) {
this.weight = weight;
}

public Node<T> getLeft() {
return left;
}

public void setLeft(Node<T> left) {
this.left = left;
}

public Node<T> getRight() {
return right;
}

public void setRight(Node<T> right) {
this.right = right;
}

@Override
public String toString(){
return "data:"+this.data+";weight:"+this.weight;
}

@Override
public int compareTo(Node<T> other) {
if(other.getWeight() > this.getWeight()){
return 1;
}
if(other.getWeight() < this.getWeight()){
return -1;
}

return 0;
}
}

//然后：实现哈夫曼树的主题类，其中包括两个静态的泛型方法，为创建哈夫曼树和广度优先遍历哈夫曼树

package my.huffmanTree;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;

public class HuffmanTree<T> {
public static <T> Node<T> createTree(List<Node<T>> nodes){
while(nodes.size() > 1){
Collections.sort(nodes);
Node<T> left = nodes.get(nodes.size()-1);
Node<T> right = nodes.get(nodes.size()-2);
Node<T> parent = new Node<T>(null, left.getWeight()+right.getWeight());
parent.setLeft(left);
parent.setRight(right);
nodes.remove(left);
nodes.remove(right);
}
return nodes.get(0);
}

public static <T> List<Node<T>> breadth(Node<T> root){
List<Node<T>> list = new ArrayList<Node<T>>();
Queue<Node<T>> queue = new ArrayDeque<Node<T>>();

if(root != null){
queue.offer(root);
}

while(!queue.isEmpty()){
Node<T> node = queue.poll();

if(node.getLeft() != null){
queue.offer(node.getLeft());
}

if(node.getRight() != null){
queue.offer(node.getRight());
}
}
return list;
}
}

[java] view plain copy
package my.huffmanTree;

import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Node<String>> list = new ArrayList<Node<String>>();

Node<String> root = HuffmanTree.createTree(list);
//      System.out.println(list);
}
}

[data:null;weight:18.0, data:a;weight:7.0, data:null;weight:11.0, data:b;weight:5.0, data:null;weight:6.0, data:d;weight:2.0, data:c;weight:4.0]

—————————————-

B树总结

AVL树:最早的平衡二叉树之一。应用相对其他数据结构比较少。windows对进程地址空间的管理用到了AVL树

B/B+树用在磁盘文件组织 数据索引和数据库索引

Trie树 字典树，用在统计和排序大量字符串(场景自己yy = =)

AVL RBtree
B B+
Trie

trie树大都用在word的匹配，但单纯的trie内存消耗很大，建trie树也需要些时间，通常用在带词典的机械分词，jieba分词就是建立在trie上匹配的，trie有其他变体可以压缩空间，像double array trie这类比较老且经典的压缩方法，也有其他比较新的压缩方式，看论文时有看过，没自己实现过所以不断言了，其实面对多模匹配trie没有其变体aho-corasick来得理想，另外aho-corasick也是可以用巧妙的方法来进行压缩空间，这里不再展开，毕竟手机码字，同时想基数树与其也类似，在nginx上有应用，说到aho-corasick其实早期的入侵检测工具snort也有应用实现，但如今改成wu-menber了，具体记不清了，其实trie还是挺有用的，Tengine也用trie实现了了匹配模块。但要是用在大量单词的匹配上确实吃内存。

AVL是一种高度平衡的二叉树，所以通常的结果是，维护这种高度平衡所付出的代价比从中获得的效率收益还大，故而实际的应用不多，更多的地方是用追求局部而不是非常严格整体平衡的红黑树。当然，如果场景中对插入删除不频繁，只是对查找特别有要求，AVL还是优于红黑的。

• 著名的linux进程调度Completely Fair Scheduler,用红黑树管理进程控制块
• epoll在内核中的实现，用红黑树管理事件块
• nginx中，用红黑树管理timer等
• Java的TreeMap实现

B和B+主要用在文件系统以及数据库中做索引等，比如Mysql：B-Tree Index in MySql

trie 树的一个典型应用是前缀匹配，比如下面这个很常见的场景，在我们输入时，搜索引擎会给予提示

B树是为磁盘或其他直接存取辅助存储设置而设计的一种平衡查找树。其能够有效降低磁盘I/O操作次数。许多数据库系统使用B树或B树的变形来储存信息。参考《算法导论》第二版第十八章的思想使用java语言实现了一颗简单的B树，在此跟大家分享下：

``````package com.discover;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* 一颗B树的简单实现。
* <p/>
* 其实现原理参考《算法导论》第二版第十八章。
* <p/>
* 如果大家想读懂这些源代码，不妨先看看上述章节。
*
* @author WangPing
*
* @param <K> - 键类型
* @param <V> - 值类型
*/
public class BTree<K, V>
{
private static Log logger = LogFactory.getLog(BTree.class);

/**
* 在B树节点中搜索给定键值的返回结果。
* <p/>
* 该结果有两部分组成。第一部分表示此次查找是否成功，
* 如果查找成功，第二部分表示给定键值在B树节点中的位置，
* 如果查找失败，第二部分表示给定键值应该插入的位置。
*/
private static class SearchResult
{
private boolean result;
private int index;

public SearchResult(boolean result, int index)
{
this.result = result;
this.index = index;
}

public boolean getResult()
{
return result;
}

public int getIndex()
{
return index;
}
}

/**
* 为了简单起见，暂时只支持整型的key，
* 等到工作完成后，支持泛型。
* <p/>
*
* TODO 需要考虑并发情况下的存取。
*/
private static class BTreeNode
{
/** 节点的关键字，以非降序存放 */
private List<Integer> keys;
/** 内节点的子节点 */
private List<BTreeNode> children;
/** 是否为叶子节点 */
private boolean leaf;

public BTreeNode()
{
keys = new ArrayList<Integer>();
children = new ArrayList<BTreeNode>();
leaf = false;
}

public boolean isLeaf()
{
return leaf;
}

public void setLeaf(boolean leaf)
{
this.leaf = leaf;
}

/**
* 返回关键字的个数。如果是非叶子节点，该节点的
*
* @return 关键字的个数
*/
public int size()
{
return keys.size();
}

/**
* 在节点中查找给定的<code>key</code>，如果节点中存在给定的
* <code>key</code>，则返回一个<code>SearchResult</code>，
* 标识此次查找成功，给定<code>key</code>在节点中的索引和给定
* <code>key</code>对应的值。如果不存在，则返回<code>SearchResult</code>
* 标识此次查找失败，给定<code>key</code>应该插入的位置，该<code>key</code>
* 对应的值为null。
* <p/>
* 如果查找成功，返回结果中的索引域为[0, {@link #size()} - 1]
* <p/>
* 这是一个二分查找算法，可以保证时间复杂度为O(log(t))。
*
* @param key - 给定的键值
* @return - 查找结果
*/
public SearchResult searchKey(Integer key)
{
int l = 0;
int h = keys.size() - 1;
int mid = 0;
while(l <= h)
{
mid = (l + h) / 2; // 先这么写吧，BTree实现中，l+h不可能溢出
if(keys.get(mid) == key)
break;
else if(keys.get(mid) > key)
h = mid - 1;
else // if(keys.get(mid) < key)
l = mid + 1;
}
boolean result = false;
int index = 0;
if(l <= h) // 说明查找成功
{
result = true;
index = mid; // index表示元素所在的位置
}
else
{
result = false;
index = l; // index表示元素应该插入的位置
}
return new SearchResult(result, index);
}

/**
* 将给定的<code>key</code>追加到节点的末尾，
* 一定要确保调用该方法之后，节点中的关键字还是
* 以非降序存放。
*
* @param key - 给定的键值
*/
{
}

/**
* 删除给定索引的键值。
* <p/>
* 你需要自己保证给定的索引是合法的。
*
* @param index - 给定的索引
*/
public void removeKey(int index)
{
keys.remove(index);
}

/**
* 得到节点中给定索引的键值。
* <p/>
* 你需要自己保证给定的索引是合法的。
*
* @param index - 给定的索引
* @return 节点中给定索引的键值
*/
public Integer keyAt(int index)
{
return keys.get(index);
}

/**
* 在该节点中插入给定的<code>key</code>，
* 该方法保证插入之后，其键值还是以非降序存放。
* <p/>
* 不过该方法的时间复杂度为O(t)。
* <p/>
* TODO 需要考虑键值是否可以重复。
*
* @param key - 给定的键值
*/
public void insertKey(Integer key)
{
SearchResult result = searchKey(key);
insertKey(key, result.getIndex());
}

/**
* 在该节点中给定索引的位置插入给定的<code>key</code>，
* 你需要自己保证<code>key</code>插入了正确的位置。
*
* @param key - 给定的键值
* @param index - 给定的索引
*/
public void insertKey(Integer key, int index)
{
/* TODO
* 通过新建一个ArrayList来实现插入真的很恶心，先这样吧
* 要是有类似C中的reallocate就好了。
*/
List<Integer> newKeys = new ArrayList<Integer>();
int i = 0;

// index = 0或者index = keys.size()都没有问题
for(; i < index; ++ i)
for(; i < keys.size(); ++ i)
keys = newKeys;
}

/**
* 返回节点中给定索引的子节点。
* <p/>
* 你需要自己保证给定的索引是合法的。
*
* @param index - 给定的索引
* @return 给定索引对应的子节点
*/
public BTreeNode childAt(int index)
{
if(isLeaf())
throw new UnsupportedOperationException("Leaf node doesn't have children.");
return children.get(index);
}

/**
* 将给定的子节点追加到该节点的末尾。
*
* @param child - 给定的子节点
*/
{
}

/**
* 删除该节点中给定索引位置的子节点。
* </p>
* 你需要自己保证给定的索引是合法的。
*
* @param index - 给定的索引
*/
public void removeChild(int index)
{
children.remove(index);
}

/**
* 将给定的子节点插入到该节点中给定索引
* 的位置。
*
* @param child - 给定的子节点
* @param index - 子节点带插入的位置
*/
public void insertChild(BTreeNode child, int index)
{
List<BTreeNode> newChildren = new ArrayList<BTreeNode>();
int i = 0;
for(; i < index; ++ i)
for(; i < children.size(); ++ i)
children = newChildren;
}
}

private static final int DEFAULT_T = 2;

/** B树的根节点 */
private BTreeNode root;
/** 根据B树的定义，B树的每个非根节点的关键字数n满足(t - 1) <= n <= (2t - 1) */
private int t = DEFAULT_T;
/** 非根节点中最小的键值数 */
private int minKeySize = t - 1;
/** 非根节点中最大的键值数 */
private int maxKeySize = 2*t - 1;

public BTree()
{
root = new BTreeNode();
root.setLeaf(true);
}

public BTree(int t)
{
this();
this.t = t;
minKeySize = t - 1;
maxKeySize = 2*t - 1;
}

/**
* 搜索给定的<code>key</code>。
* <p/>
* TODO 需要重新定义返回结果，应该返回
* <code>key</code>对应的值。
*
* @param key - 给定的键值
*/
public int search(Integer key)
{
return search(root, key);
}

/**
* 在以给定节点为根的子树中，递归搜索
* 给定的<code>key</code>
*
* @param node - 子树的根节点
* @param key - 给定的键值
*/
private static int search(BTreeNode node, Integer key)
{
SearchResult result = node.searchKey(key);
if(result.getResult())
return result.getIndex();
else
{
if(node.isLeaf())
return -1;
else
search(node.childAt(result.getIndex()), key);

}
return -1;
}

/**
* 分裂一个满子节点<code>childNode</code>。
* <p/>
* 你需要自己保证给定的子节点是满节点。
*
* @param parentNode - 父节点
* @param childNode - 满子节点
* @param index - 满子节点在父节点中的索引
*/
private void splitNode(BTreeNode parentNode, BTreeNode childNode, int index)
{
assert childNode.size() == maxKeySize;

BTreeNode siblingNode = new BTreeNode();
siblingNode.setLeaf(childNode.isLeaf());
// 将满子节点中索引为[t, 2t - 2]的(t - 1)个关键字插入新的节点中
for(int i = 0; i < minKeySize; ++ i)
// 提取满子节点中的中间关键字，其索引为(t - 1)
Integer key = childNode.keyAt(t - 1);
// 删除满子节点中索引为[t - 1, 2t - 2]的t个关键字
for(int i = maxKeySize - 1; i >= t - 1; -- i)
childNode.removeKey(i);
if(!childNode.isLeaf()) // 如果满子节点不是叶节点，则还需要处理其子节点
{
// 将满子节点中索引为[t, 2t - 1]的t个子节点插入新的节点中
for(int i = 0; i < minKeySize + 1; ++ i)
// 删除满子节点中索引为[t, 2t - 1]的t个子节点
for(int i = maxKeySize; i >= t; -- i)
childNode.removeChild(i);
}
// 将key插入父节点
parentNode.insertKey(key, index);
// 将新节点插入父节点
parentNode.insertChild(siblingNode, index + 1);
}

/**
* 在一个非满节点中插入给定的<code>key</code>。
*
* @param node - 非满节点
* @param key - 给定的键值
*/
private void insertNotFull(BTreeNode node, Integer key)
{
assert node.size() < maxKeySize;

if(node.isLeaf()) // 如果是叶子节点，直接插入
node.insertKey(key);
else
{
/* 找到key在给定节点应该插入的位置，那么key应该插入
* 该位置对应的子树中
*/
SearchResult result = node.searchKey(key);
BTreeNode childNode = node.childAt(result.getIndex());
if(childNode.size() == 2*t - 1) // 如果子节点是满节点
{
// 则先分裂
splitNode(node, childNode, result.getIndex());
/* 如果给定的key大于分裂之后新生成的键值，则需要插入该新键值的右边，
* 否则左边。
*/
if(key > node.keyAt(result.getIndex()))
childNode = node.childAt(result.getIndex() + 1);
}
insertNotFull(childNode, key);
}
}

/**
* 在B树中插入给定的<code>key</code>。
*
* @param key - 给定的键值
*/
public void insert(Integer key)
{
if(root.size() == maxKeySize) // 如果根节点满了，则B树长高
{
BTreeNode newRoot = new BTreeNode();
newRoot.setLeaf(false);
splitNode(newRoot, root, 0);
root = newRoot;
}
insertNotFull(root, key);
}

/**
* 从B树中删除一个给定的<code>key</code>。
*
* @param key - 给定的键值
*/
public void delete(Integer key)
{
// root的情况还需要做一些特殊处理
delete(root, key);
}

/**
* 从以给定<code>node</code>为根的子树中删除指定的<code>key</code>。
* <p/>
* 删除的实现思想请参考《算法导论》第二版的第18章。
* <p/>
* TODO 需要重构，代码太长了
*
* @param node - 给定的节点
* @param key - 给定的键值
*/
public void delete(BTreeNode node, Integer key)
{
// 该过程需要保证，对非根节点执行删除操作时，其关键字个数至少为t。
assert node.size() >= t || node == root;

SearchResult result = node.searchKey(key);
/*
* 因为这是查找成功的情况，0 <= result.getIndex() <= (node.size() - 1)，
* 因此(result.getIndex() + 1)不会溢出。
*/
if(result.getResult())
{
// 1.如果关键字在节点node中，并且是叶节点，则直接删除。
if(node.isLeaf())
node.removeKey(result.getIndex());
else
{
// 2.a 如果节点node中前于key的子节点包含至少t个关键字
BTreeNode leftChildNode = node.childAt(result.getIndex());
if(leftChildNode.size() >= t)
{
// 使用leftChildNode中的最后一个键值代替node中的key
node.removeKey(result.getIndex());
node.insertKey(leftChildNode.keyAt(leftChildNode.size() - 1), result.getIndex());
delete(leftChildNode, leftChildNode.keyAt(leftChildNode.size() - 1));
// node.
}
else
{
// 2.b 如果节点node中后于key的子节点包含至少t个关键字
BTreeNode rightChildNode = node.childAt(result.getIndex() + 1);
if(rightChildNode.size() >= t)
{
// 使用rightChildNode中的第一个键值代替node中的key
node.removeKey(result.getIndex());
node.insertKey(rightChildNode.keyAt(0), result.getIndex());
delete(rightChildNode, rightChildNode.keyAt(0));
}
else // 2.c 前于key和后于key的子节点都只包含t-1个关键字
{
node.removeKey(result.getIndex());
node.removeChild(result.getIndex() + 1);
// 将key和rightChildNode中的键值合并进leftChildNode
for(int i = 0; i < rightChildNode.size(); ++ i)
// 将rightChildNode中的子节点合并进leftChildNode，如果有的话
if(!rightChildNode.isLeaf())
{
for(int i = 0; i <= rightChildNode.size(); ++ i)
}
delete(leftChildNode, key);
}
}
}
}
else
{
/*
* 因为这是查找失败的情况，0 <= result.getIndex() <= node.size()，
* 因此(result.getIndex() + 1)会溢出。
*/
if(node.isLeaf()) // 如果关键字不在节点node中，并且是叶节点，则什么都不做，因为该关键字不在该B树中
{
logger.info("The key: " + key + " isn't in this BTree.");
return;
}
BTreeNode childNode = node.childAt(result.getIndex());
if(childNode.size() >= t)
delete(childNode, key); // 递归删除
else // 3
{
// 先查找右边的兄弟节点
BTreeNode siblingNode = null;
int siblingIndex = -1;
if(result.getIndex() < node.size()) // 存在右兄弟节点
{
if(node.childAt(result.getIndex() + 1).size() >= t)
{
siblingNode = node.childAt(result.getIndex() + 1);
siblingIndex = result.getIndex() + 1;
}
}
// 如果右边的兄弟节点不符合条件，则试试左边的兄弟节点
if(siblingNode == null)
{
if(result.getIndex() > 0) // 存在左兄弟节点
{
if(node.childAt(result.getIndex() - 1).size() >= t)
{
siblingNode = node.childAt(result.getIndex() - 1);
siblingIndex = result.getIndex() - 1;
}
}
}
// 3.a 有一个相邻兄弟节点至少包含t个关键字
if(siblingNode != null)
{
if(siblingIndex < result.getIndex()) // 左兄弟节点满足条件
{
childNode.insertKey(node.keyAt(siblingIndex), 0);
node.removeKey(siblingIndex);
node.insertKey(siblingNode.keyAt(siblingNode.size() - 1), siblingIndex);
siblingNode.removeKey(siblingNode.size() - 1);
// 将左兄弟节点的最后一个孩子移到childNode
if(!siblingNode.isLeaf())
{
childNode.insertChild(siblingNode.childAt(siblingNode.size()), 0);
siblingNode.removeChild(siblingNode.size());
}
}
else // 右兄弟节点满足条件
{
childNode.insertKey(node.keyAt(result.getIndex()), childNode.size() - 1);
node.removeKey(result.getIndex());
node.insertKey(siblingNode.keyAt(0), result.getIndex());
siblingNode.removeKey(0);
// 将右兄弟节点的第一个孩子移到childNode
// childNode.insertChild(siblingNode.childAt(0), childNode.size() + 1);
if(!siblingNode.isLeaf())
{
siblingNode.removeChild(0);
}
}
delete(childNode, key);
}
else // 3.b 如果其相邻左右节点都包含t-1个关键字
{
if(result.getIndex() < node.size()) // 存在右兄弟
{
BTreeNode rightSiblingNode = node.childAt(result.getIndex() + 1);
node.removeKey(result.getIndex());
node.removeChild(result.getIndex() + 1);
for(int i = 0; i < rightSiblingNode.size(); ++ i)
if(!rightSiblingNode.isLeaf())
{
for(int i = 0; i <= rightSiblingNode.size(); ++ i)
}
}
else // 存在左节点
{
BTreeNode leftSiblingNode = node.childAt(result.getIndex() - 1);
node.removeKey(result.getIndex() - 1);
node.removeChild(result.getIndex() - 1);
for(int i = leftSiblingNode.size() - 1; i >= 0; -- i)
childNode.insertKey(leftSiblingNode.keyAt(i), 0);
if(!leftSiblingNode.isLeaf())
{
for(int i = leftSiblingNode.size(); i >= 0; -- i)
childNode.insertChild(leftSiblingNode.childAt(i), 0);
}
}
// 如果node是root并且node不包含任何关键字了
if(node == root && node.size() == 0)
root = childNode;
delete(childNode, key);
}
}
}
}

/**
* 一个简单的层次遍历B树实现，用于输出B树。
* <p/>
* TODO 待改进，使显示更加形象化。
*/
public void output()
{
queue.offer(root);
while(!queue.isEmpty())
{
BTreeNode node = queue.poll();
for(int i = 0; i < node.size(); ++ i)
System.out.print(node.keyAt(i) + " ");
System.out.println();
if(!node.isLeaf())
{
for(int i = 0; i <= node.size(); ++ i)
queue.offer(node.childAt(i));
}
}
}

public static void main(String[] args)
{
Random random = new Random();
BTree<Integer, Byte[]> btree = new BTree<Integer, Byte[]>();
for(int i = 0; i < 10; ++ i)
{
int r = random.nextInt(100);
System.out.println(r);
btree.insert(r);
}
System.out.println("----------------------");
btree.output();
}
}``````

B+树非叶子结点只存储键值，单个节点占空间小，索引块能够存储更多的节点，从磁盘读索引时所需的索引块更少，所以索引查找时I/O次数较B-Tree索引少，效率更高。而且B+Tree在叶子节点存放的记录以链表的形式链接，范围查找或遍历效率更高。Mysql InnoDB用的就是B+Tree索引。

``````package debuggees;

import java.io.IOException;
import java.util.NoSuchElementException;

/**
* jBixbe debuggee: test insert and delete operation of a balanced tree data
* structure. Using integer values read from keyboard as tree elements.
*
* @author ds-emedia
*/
public class BTree<T extends Comparable<T>> {

private static BTree<Integer> tree = new BTree<Integer>();

public static void main(String args[]) throws IOException {

System.out.println("test balanced tree operations");
System.out.println("*****************************");

String input;
Integer value;

do {
input = stringInput("please select: [i]nsert, [d]elete, [e]xit");
switch (input.charAt(0)) {
case 'i':
value = Integer.parseInt(stringInput("insert: "), 10);
if (tree.isMember(value)) {
System.out.println("value " + value + " already in tree");
} else {
tree.insert(value);
}
break;
case 'd':
value = Integer.parseInt(stringInput("delete: "), 10);
if (tree.isMember(value)) {
tree.delete(value);
} else {
}
break;
}
} while ((input.charAt(0) != 'e'));
}

private static String stringInput(String inputRequest) throws IOException {
System.out.println(inputRequest);
}

/* +++++++++++ instance declarations +++++++++++ */

private Node root;

/**
* Creates an empty balanced tree.
*/
public BTree() {
root = null;
}

/**
* Creates a balances tree using the given node as tree root.
*/
public BTree(Node root) {
this.root = root;
}

/**
* Inserts an element into the tree.
*/
public void insert(T info) {
insert(info, root, null, false);
}

/**
* Checks whether the given element is already in the tree.
*/
public boolean isMember(T info) {
return isMember(info, root);
}

/**
* Removes an elememt from the tree.
*/
public void delete(T info) {
delete(info, root);
}

/**
* Returns a text representation of the tree.
*/
public String toString() {
return inOrder();
}

/**
* Returns all elements of the tree in in-order traversing.
*/
public String inOrder() {
return inOrder(root);
}

/**
* Returns all elements of the tree in pre-order traversing.
*/
public String preOrder() {
return preOrder(root);
}

/**
* Returns all elements of the tree in post-order traversing.
*/
public String postOrder() {
return postOrder(root);
}

/**
* Returns the height of the tree.
*/
public int getHeight() {
return getHeight(root);
}

private void insert(T info, Node node, Node parent, boolean right) {

if (node == null) {
if (parent == null) {
root = node = new Node(info, parent);
} else if (right) {
parent.right = node = new Node(info, parent);
} else {
parent.left = node = new Node(info, parent);
}
restructInsert(node, false);
} else if (info.compareTo(node.information) == 0) {
node.information = info;
} else if (info.compareTo(node.information) > 0) {
insert(info, node.right, node, true);
} else {
insert(info, node.left, node, false);
}
}

private boolean isMember(T info, Node node) {

boolean member = false;

if (node == null) {
member = false;
} else if (info.compareTo(node.information) == 0) {
member = true;
} else if (info.compareTo(node.information) > 0) {
member = isMember(info, node.right);
} else {
member = isMember(info, node.left);
}

return member;
}

private void delete(T info, Node node) throws NoSuchElementException {

if (node == null) {
throw new NoSuchElementException();
} else if (info.compareTo(node.information) == 0) {
deleteNode(node);
} else if (info.compareTo(node.information) > 0) {
delete(info, node.right);
} else {
delete(info, node.left);
}
}

private void deleteNode(Node node) {

Node eNode, minMaxNode, delNode = null;
boolean rightNode = false;

if (node.isLeaf()) {
if (node.parent == null) {
root = null;
} else if (node.isRightNode()) {
node.parent.right = null;
rightNode = true;
} else if (node.isLeftNode()) {
node.parent.left = null;
}
delNode = node;
} else if (node.hasLeftNode()) {
minMaxNode = node.left;
for (eNode = node.left; eNode != null; eNode = eNode.right) {
minMaxNode = eNode;
}
delNode = minMaxNode;
node.information = minMaxNode.information;

if (node.left.right != null) {
minMaxNode.parent.right = minMaxNode.left;
rightNode = true;
} else {
minMaxNode.parent.left = minMaxNode.left;
}

if (minMaxNode.left != null) {
minMaxNode.left.parent = minMaxNode.parent;
}
} else if (node.hasRightNode()) {
minMaxNode = node.right;
delNode = minMaxNode;
rightNode = true;

node.information = minMaxNode.information;

node.right = minMaxNode.right;
if (node.right != null) {
node.right.parent = node;
}
node.left = minMaxNode.left;
if (node.left != null) {
node.left.parent = node;
}
}
restructDelete(delNode.parent, rightNode);
}

private int getHeight(Node node) {
int height = 0;

if (node == null) {
height = -1;
} else {
height = 1 + Math.max(getHeight(node.left), getHeight(node.right));
}
return height;
}

private String inOrder(Node node) {

String result = "";
if (node != null) {
result = result + inOrder(node.left) + " ";
result = result + node.information.toString();
result = result + inOrder(node.right);
}
return result;
}

private String preOrder(Node node) {

String result = "";
if (node != null) {
result = result + node.information.toString() + " ";
result = result + preOrder(node.left);
result = result + preOrder(node.right);
}
return result;
}

private String postOrder(Node node) {

String result = "";
if (node != null) {
result = result + postOrder(node.left);
result = result + postOrder(node.right);
result = result + node.information.toString() + " ";
}
return result;
}

private void restructInsert(Node node, boolean wasRight) {

if (node != root) {
if (node.parent.balance == '_') {
if (node.isLeftNode()) {
node.parent.balance = '/';
restructInsert(node.parent, false);
} else {
node.parent.balance = '\\';
restructInsert(node.parent, true);
}
} else if (node.parent.balance == '/') {
if (node.isRightNode()) {
node.parent.balance = '_';
} else {
if (!wasRight) {
rotateRight(node.parent);
} else {
doubleRotateRight(node.parent);
}
}
} else if (node.parent.balance == '\\') {
if (node.isLeftNode()) {
node.parent.balance = '_';
} else {
if (wasRight) {
rotateLeft(node.parent);
} else {
doubleRotateLeft(node.parent);
}
}
}
}
}

private void restructDelete(Node z, boolean wasRight) {

Node parent;
boolean isRight = false;
boolean climb = false;
boolean canClimb;

if (z == null) {
return;
}

parent = z.parent;
canClimb = (parent != null);

if (canClimb) {
isRight = z.isRightNode();
}

if (z.balance == '_') {
if (wasRight) {
z.balance = '/';
} else {
z.balance = '\\';
}
} else if (z.balance == '/') {
if (wasRight) {
if (z.left.balance == '\\') {
doubleRotateRight(z);
climb = true;
} else {
rotateRight(z);
if (z.balance == '_') {
climb = true;
}
}
} else {
z.balance = '_';
climb = true;
}
} else {
if (wasRight) {
z.balance = '_';
climb = true;
} else {
if (z.right.balance == '/') {
doubleRotateLeft(z);
climb = true;
} else {
rotateLeft(z);
if (z.balance == '_') {
climb = true;
}
}
}
}

if (canClimb && climb) {
restructDelete(parent, isRight);
}
}

private void rotateLeft(Node a) {

Node b = a.right;

if (a.parent == null) {
root = b;
} else {
if (a.isLeftNode()) {
a.parent.left = b;
} else {
a.parent.right = b;
}
}

a.right = b.left;
if (a.right != null) {
a.right.parent = a;
}

b.parent = a.parent;
a.parent = b;
b.left = a;

if (b.balance == '_') {
a.balance = '\\';
b.balance = '/';
} else {
a.balance = '_';
b.balance = '_';
}
}

private void rotateRight(Node a) {

Node b = a.left;

if (a.parent == null) {
root = b;
} else {
if (a.isLeftNode()) {
a.parent.left = b;
} else {
a.parent.right = b;
}
}

a.left = b.right;
if (a.left != null) {
a.left.parent = a;
}

b.parent = a.parent;
a.parent = b;
b.right = a;

if (b.balance == '_') {
a.balance = '/';
b.balance = '\\';
} else {
a.balance = '_';
b.balance = '_';
}
}

private void doubleRotateLeft(Node a) {

Node b = a.right;
Node c = b.left;

if (a.parent == null) {
root = c;
} else {
if (a.isLeftNode()) {
a.parent.left = c;
} else {
a.parent.right = c;
}
}

c.parent = a.parent;

a.right = c.left;
if (a.right != null) {
a.right.parent = a;
}
b.left = c.right;
if (b.left != null) {
b.left.parent = b;
}

c.left = a;
c.right = b;

a.parent = c;
b.parent = c;

if (c.balance == '/') {
a.balance = '_';
b.balance = '\\';
} else if (c.balance == '\\') {
a.balance = '/';
b.balance = '_';
} else {
a.balance = '_';
b.balance = '_';
}

c.balance = '_';
}

private void doubleRotateRight(Node a) {

Node b = a.left;
Node c = b.right;

if (a.parent == null) {
root = c;
} else {
if (a.isLeftNode()) {
a.parent.left = c;
} else {
a.parent.right = c;
}
}

c.parent = a.parent;

a.left = c.right;
if (a.left != null) {
a.left.parent = a;
}
b.right = c.left;
if (b.right != null) {
b.right.parent = b;
}

c.right = a;
c.left = b;

a.parent = c;
b.parent = c;

if (c.balance == '/') {
b.balance = '_';
a.balance = '\\';
} else if (c.balance == '\\') {
b.balance = '/';
a.balance = '_';
} else {
b.balance = '_';
a.balance = '_';
}
c.balance = '_';
}

class Node {

T information;

Node parent;

Node left;

Node right;

char balance;

public Node(T information, Node parent) {
this.information = information;
this.parent = parent;
this.left = null;
this.right = null;
this.balance = '_';
}

boolean isLeaf() {
return ((left == null) && (right == null));
}

boolean isNode() {
return !isLeaf();
}

boolean hasLeftNode() {
return (null != left);
}

boolean hasRightNode() {
return (right != null);
}

boolean isLeftNode() {
return (parent.left == this);
}

boolean isRightNode() {
return (parent.right == this);
}
}
}``````

B树和B+树

1.B树中同一键值不会出现多次,并且它有可能出现在叶结点,也有可能出现在非叶结点中.而B+树的键一定会出现在叶结点中,并且有可能在非叶结点中也有可能重复出现,以维持B+树的平衡.
2.因为B树键位置不定,且在整个树结构中只出现一次,虽然可以节省存储空间,但使得在插入、删除操作复杂度明显增加.B+树相比来说是一种较好的折中.
3.B树的查询效率与键在树中的位置有关,最大时间复杂度与B+树相同(在叶结点的时候),最小时间复杂度为1(在根结点的时候).而B+树的时候复杂度对某建成的树是固定的.

B+树的定义：

1.任意非叶子结点最多有M个子节点；且M>2；

2.除根结点以外的非叶子结点至少有 M/2个子节点；

3.根结点至少有2个子节点；

4.除根节点外每个结点存放至少M/2和至多M个关键字；（至少2个关键字）

5.非叶子结点的子树指针与关键字个数相同；

6.所有结点的关键字：K[1], K[2], …, K[M]；且K[i] < K[i+1]；

7.非叶子结点的子树指针P[i]，指向关键字值属于[K[i], K[i+1])的子树；

8.所有叶子结点位于同一层；

5.为所有叶子结点增加一个链指针；

6.所有关键字都在叶子结点出现；

Java实现：

Java代码

1. package com.meidusa.test;
2.
3. public interface B {
4.        public Object get(Comparable key);   //查询
5.        public void remove(Comparable key);    //移除
6.        public void insertOrUpdate(Comparable key, Object obj); //插入或者更新，如果已经存在，就更新，否则插入

[java]
view plain

1. package com.meidusa.test;
2.
3. public interface B {
4.        public Object get(Comparable key);   //查询
5.        public void remove(Comparable key);    //移除
6.        public void insertOrUpdate(Comparable key, Object obj); //插入或者更新，如果已经存在，就更新，否则插入
7. }

B+树： Java代码

1. package com.meidusa.test;
2.
3. import java.util.Random;
4.
5. public class BplusTree implements B {
6.
7.     /** 根节点 */
8.     protected Node root;
9.
10.     /** 阶数，M值 */
11.     protected int order;
12.
13.     /** 叶子节点的链表头*/
15.
18.     }
19.
22.     }
23.
24.     public Node getRoot() {
25.         return root;
26.     }
27.
28.     public void setRoot(Node root) {
29.         this.root = root;
30.     }
31.
32.     public int getOrder() {
33.         return order;
34.     }
35.
36.     public void setOrder(int order) {
37.         this.order = order;
38.     }
39.
40.     @Override
41.     public Object get(Comparable key) {
42.         return root.get(key);
43.     }
44.
45.     @Override
46.     public void remove(Comparable key) {
47.         root.remove(key, this);
48.
49.     }
50.
51.     @Override
52.     public void insertOrUpdate(Comparable key, Object obj) {
53.         root.insertOrUpdate(key, obj, this);
54.
55.     }
56.
57.     public BplusTree(int order){
58.         if (order < 3) {
59.             System.out.print(“order must be greater than 2”);
60.             System.exit(0);
61.         }
62.         this.order = order;
63.         root = new Node(truetrue);
65.     }
66.
67.     //测试
68.     public static void main(String[] args) {
69.         BplusTree tree = new BplusTree(6);
70.         Random random = new Random();
71.         long current = System.currentTimeMillis();
72.         for (int j = 0; j < 100000; j++) {
73.             for (int i = 0; i < 100; i++) {
74.                 int randomNumber = random.nextInt(1000);
75.                 tree.insertOrUpdate(randomNumber, randomNumber);
76.             }
77.
78.             for (int i = 0; i < 100; i++) {
79.                 int randomNumber = random.nextInt(1000);
80.                 tree.remove(randomNumber);
81.             }
82.         }
83.
84.         long duration = System.currentTimeMillis() – current;
85.         System.out.println(“time elpsed for duration: “ + duration);
86.         int search = 80
87.         System.out.print(tree.get(search));
88.     }
89.

[java]
view plain

1. package com.meidusa.test;
2.
3. import java.util.Random;
4.
5. public class BplusTree implements B {
6.
7.     /** 根节点 */
8.     protected Node root;
9.
10.     /** 阶数，M值 */
11.     protected int order;
12.
13.     /** 叶子节点的链表头*/
15.
18.     }
19.
22.     }
23.
24.     public Node getRoot() {
25.         return root;
26.     }
27.
28.     public void setRoot(Node root) {
29.         this.root = root;
30.     }
31.
32.     public int getOrder() {
33.         return order;
34.     }
35.
36.     public void setOrder(int order) {
37.         this.order = order;
38.     }
39.
40.     @Override
41.     public Object get(Comparable key) {
42.         return root.get(key);
43.     }
44.
45.     @Override
46.     public void remove(Comparable key) {
47.         root.remove(key, this);
48.
49.     }
50.
51.     @Override
52.     public void insertOrUpdate(Comparable key, Object obj) {
53.         root.insertOrUpdate(key, obj, this);
54.
55.     }
56.
57.     public BplusTree(int order){
58.         if (order < 3) {
59.             System.out.print(“order must be greater than 2”);
60.             System.exit(0);
61.         }
62.         this.order = order;
63.         root = new Node(truetrue);
65.     }
66.
67.     //测试
68.     public static void main(String[] args) {
69.         BplusTree tree = new BplusTree(6);
70.         Random random = new Random();
71.         long current = System.currentTimeMillis();
72.         for (int j = 0; j < 100000; j++) {
73.             for (int i = 0; i < 100; i++) {
74.                 int randomNumber = random.nextInt(1000);
75.                 tree.insertOrUpdate(randomNumber, randomNumber);
76.             }
77.
78.             for (int i = 0; i < 100; i++) {
79.                 int randomNumber = random.nextInt(1000);
80.                 tree.remove(randomNumber);
81.             }
82.         }
83.
84.         long duration = System.currentTimeMillis() – current;
85.         System.out.println(“time elpsed for duration: “ + duration);
86.         int search = 80;
87.         System.out.print(tree.get(search));
88.     }
89.
90. }

1. package com.meidusa.test;
2.
3. import java.util.AbstractMap.SimpleEntry;
4. import java.util.ArrayList;
5. import java.util.List;
6. import java.util.Map.Entry;
7.
8. public class Node {
9.
10.     /** 是否为叶子节点 */
11.     protected boolean isLeaf;
12.
13.     /** 是否为根节点*/
14.     protected boolean isRoot;
15.
16.     /** 父节点 */
17.     protected Node parent;
18.
19.     /** 叶节点的前节点*/
20.     protected Node previous;
21.
22.     /** 叶节点的后节点*/
23.     protected Node next;
24.
25.     /** 节点的关键字 */
26.     protected List<Entry<Comparable, Object>> entries;
27.
28.     /** 子节点 */
29.     protected List<Node> children;
30.
31.     public Node(boolean isLeaf) {
32.         this.isLeaf = isLeaf;
33.         entries = new ArrayList<Entry<Comparable, Object>>();
34.
35.         if (!isLeaf) {
36.             children = new ArrayList<Node>();
37.         }
38.     }
39.
40.     public Node(boolean isLeaf, boolean isRoot) {
41.         this(isLeaf);
42.         this.isRoot = isRoot;
43.     }
44.
45.     public Object get(Comparable key) {
46.
47.         //如果是叶子节点
48.         if (isLeaf) {
49.             for (Entry<Comparable, Object> entry : entries) {
50.                 if (entry.getKey().compareTo(key) == 0) {
51.                     //返回找到的对象
52.                     return entry.getValue();
53.                 }
54.             }
55.             //未找到所要查询的对象
56.             return null
57.
58.         //如果不是叶子节点
59.         }else {
60.             //如果key小于等于节点最左边的key，沿第一个子节点继续搜索
61.             if (key.compareTo(entries.get(0).getKey()) <= 0) {
62.                 return children.get(0).get(key);
63.             //如果key大于节点最右边的key，沿最后一个子节点继续搜索
64.             }else if (key.compareTo(entries.get(entries.size()-1).getKey()) >= 0) {
65.                 return children.get(children.size()-1).get(key);
66.             //否则沿比key大的前一个子节点继续搜索
67.             }else {
68.                 for (int i = 0; i < entries.size(); i++) {
69.                     if (entries.get(i).getKey().compareTo(key) <= 0 && entries.get(i+1).getKey().compareTo(key) > 0) {
70.                         return children.get(i).get(key);
71.                     }
72.                 }
73.             }
74.         }
75.
76.         return null
77.     }
78.
79.     public void insertOrUpdate(Comparable key, Object obj, BplusTree tree){
80.         //如果是叶子节点
81.         if (isLeaf){
82.             //不需要分裂，直接插入或更新
83.             if (contains(key) || entries.size() < tree.getOrder()){
84.                 insertOrUpdate(key, obj);
85.                 if (parent != null) {
86.                     //更新父节点
87.                     parent.updateInsert(tree);
88.                 }
89.
90.             //需要分裂
91.             }else {
92.                 //分裂成左右两个节点
93.                 Node left = new Node(true);
94.                 Node right = new Node(true);
95.                 //设置链接
96.                 if (previous != null){
97.                     previous.setNext(left);
98.                     left.setPrevious(previous);
99.                 }
100.                 if (next != null) {
101.                     next.setPrevious(right);
102.                     right.setNext(next);
103.                 }
104.                 if (previous == null){
106.                 }
107.
108.                 left.setNext(right);
109.                 right.setPrevious(left);
110.                 previous = null
111.                 next = null
112.
113.                 //左右两个节点关键字长度
114.                 int leftSize = (tree.getOrder() + 1) / 2 + (tree.getOrder() + 1) % 2;
115.                 int rightSize = (tree.getOrder() + 1) / 2
116.                 //复制原节点关键字到分裂出来的新节点
117.                 insertOrUpdate(key, obj);
118.                 for (int i = 0; i < leftSize; i++){
120.                 }
121.                 for (int i = 0; i < rightSize; i++){
123.                 }
124.
125.                 //如果不是根节点
126.                 if (parent != null) {
127.                     //调整父子节点关系
128.                     int index = parent.getChildren().indexOf(this);
129.                     parent.getChildren().remove(this);
130.                     left.setParent(parent);
131.                     right.setParent(parent);
134.                     setEntries(null);
135.                     setChildren(null);
136.
137.                     //父节点插入或更新关键字
138.                     parent.updateInsert(tree);
139.                     setParent(null);
140.                 //如果是根节点
141.                 }else {
142.                     isRoot = false
143.                     Node parent = new Node(falsetrue);
144.                     tree.setRoot(parent);
145.                     left.setParent(parent);
146.                     right.setParent(parent);
149.                     setEntries(null);
150.                     setChildren(null);
151.
152.                     //更新根节点
153.                     parent.updateInsert(tree);
154.                 }
155.
156.
157.             }
158.
159.         //如果不是叶子节点
160.         }else {
161.             //如果key小于等于节点最左边的key，沿第一个子节点继续搜索
162.             if (key.compareTo(entries.get(0).getKey()) <= 0) {
163.                 children.get(0).insertOrUpdate(key, obj, tree);
164.             //如果key大于节点最右边的key，沿最后一个子节点继续搜索
165.             }else if (key.compareTo(entries.get(entries.size()-1).getKey()) >= 0) {
166.                 children.get(children.size()-1).insertOrUpdate(key, obj, tree);
167.             //否则沿比key大的前一个子节点继续搜索
168.             }else {
169.                 for (int i = 0; i < entries.size(); i++) {
170.                     if (entries.get(i).getKey().compareTo(key) <= 0 && entries.get(i+1).getKey().compareTo(key) > 0) {
171.                         children.get(i).insertOrUpdate(key, obj, tree);
172.                         break
173.                     }
174.                 }
175.             }
176.         }
177.     }
178.
179.     /** 插入节点后中间节点的更新 */
180.     protected void updateInsert(BplusTree tree){
181.
182.         validate(this, tree);
183.
184.         //如果子节点数超出阶数，则需要分裂该节点
185.         if (children.size() > tree.getOrder()) {
186.             //分裂成左右两个节点
187.             Node left = new Node(false);
188.             Node right = new Node(false);
189.             //左右两个节点关键字长度
190.             int leftSize = (tree.getOrder() + 1) / 2 + (tree.getOrder() + 1) % 2
191.             int rightSize = (tree.getOrder() + 1) / 2
192.             //复制子节点到分裂出来的新节点，并更新关键字
193.             for (int i = 0; i < leftSize; i++){
196.                 children.get(i).setParent(left);
197.             }
198.             for (int i = 0; i < rightSize; i++){
200.                 right.getEntries().add(new SimpleEntry(children.get(leftSize + i).getEntries().get(0).getKey(), null));
201.                 children.get(leftSize + i).setParent(right);
202.             }
203.
204.             //如果不是根节点
205.             if (parent != null) {
206.                 //调整父子节点关系
207.                 int index = parent.getChildren().indexOf(this);
208.                 parent.getChildren().remove(this);
209.                 left.setParent(parent);
210.                 right.setParent(parent);
213.                 setEntries(null);
214.                 setChildren(null);
215.
216.                 //父节点更新关键字
217.                 parent.updateInsert(tree);
218.                 setParent(null);
219.             //如果是根节点
220.             }else {
221.                 isRoot = false
222.                 Node parent = new Node(falsetrue);
223.                 tree.setRoot(parent);
224.                 left.setParent(parent);
225.                 right.setParent(parent);
228.                 setEntries(null);
229.                 setChildren(null);
230.
231.                 //更新根节点
232.                 parent.updateInsert(tree);
233.             }
234.         }
235.     }
236.
237.     /** 调整节点关键字*/
238.     protected static void validate(Node node, BplusTree tree) {
239.
240.         // 如果关键字个数与子节点个数相同
241.         if (node.getEntries().size() == node.getChildren().size()) {
242.             for (int i = 0; i < node.getEntries().size(); i++) {
243.                 Comparable key = node.getChildren().get(i).getEntries().get(0).getKey();
244.                 if (node.getEntries().get(i).getKey().compareTo(key) != 0) {
245.                     node.getEntries().remove(i);
247.                     if(!node.isRoot()){
248.                         validate(node.getParent(), tree);
249.                     }
250.                 }
251.             }
252.             // 如果子节点数不等于关键字个数但仍大于M / 2并且小于M，并且大于2
253.         } else if (node.isRoot() && node.getChildren().size() >= 2
254.                 ||node.getChildren().size() >= tree.getOrder() / 2
255.                 && node.getChildren().size() <= tree.getOrder()
256.                 && node.getChildren().size() >= 2) {
257.             node.getEntries().clear();
258.             for (int i = 0; i < node.getChildren().size(); i++) {
259.                 Comparable key = node.getChildren().get(i).getEntries().get(0).getKey();
261.                 if (!node.isRoot()) {
262.                     validate(node.getParent(), tree);
263.                 }
264.             }
265.         }
266.     }
267.
268.     /** 删除节点后中间节点的更新*/
269.     protected void updateRemove(BplusTree tree) {
270.
271.         validate(this, tree);
272.
273.         // 如果子节点数小于M / 2或者小于2，则需要合并节点
274.         if (children.size() < tree.getOrder() / 2 || children.size() < 2) {
275.             if (isRoot) {
276.                 // 如果是根节点并且子节点数大于等于2，OK
277.                 if (children.size() >= 2) {
278.                     return
279.                 // 否则与子节点合并
280.                 } else {
281.                     Node root = children.get(0);
282.                     tree.setRoot(root);
283.                     root.setParent(null);
284.                     root.setRoot(true);
285.                     setEntries(null);
286.                     setChildren(null);
287.                 }
288.             } else {
289.                 //计算前后节点
290.                 int currIdx = parent.getChildren().indexOf(this);
291.                 int prevIdx = currIdx – 1
292.                 int nextIdx = currIdx + 1
293.                 Node previous = null, next = null
294.                 if (prevIdx >= 0) {
295.                     previous = parent.getChildren().get(prevIdx);
296.                 }
297.                 if (nextIdx < parent.getChildren().size()) {
298.                     next = parent.getChildren().get(nextIdx);
299.                 }
300.
301.                 // 如果前节点子节点数大于M / 2并且大于2，则从其处借补
302.                 if (previous != null
303.                         && previous.getChildren().size() > tree.getOrder() / 2
304.                         && previous.getChildren().size() > 2) {
305.                     //前叶子节点末尾节点添加到首位
306.                     int idx = previous.getChildren().size() – 1
307.                     Node borrow = previous.getChildren().get(idx);
308.                     previous.getChildren().remove(idx);
309.                     borrow.setParent(this);
311.                     validate(previous, tree);
312.                     validate(this, tree);
313.                     parent.updateRemove(tree);
314.
315.                 // 如果后节点子节点数大于M / 2并且大于2，则从其处借补
316.                 } else if (next != null
317.                         && next.getChildren().size() > tree.getOrder() / 2
318.                         && next.getChildren().size() > 2) {
319.                     //后叶子节点首位添加到末尾
320.                     Node borrow = next.getChildren().get(0);
321.                     next.getChildren().remove(0);
322.                     borrow.setParent(this);
324.                     validate(next, tree);
325.                     validate(this, tree);
326.                     parent.updateRemove(tree);
327.
328.                 // 否则需要合并节点
329.                 } else {
330.                     // 同前面节点合并
331.                     if (previous != null
332.                             && (previous.getChildren().size() <= tree.getOrder() / 2 || previous.getChildren().size() <= 2)) {
333.
334.                         for (int i = previous.getChildren().size() – 1; i >= 0; i–) {
335.                             Node child = previous.getChildren().get(i);
337.                             child.setParent(this);
338.                         }
339.                         previous.setChildren(null);
340.                         previous.setEntries(null);
341.                         previous.setParent(null);
342.                         parent.getChildren().remove(previous);
343.                         validate(this, tree);
344.                         parent.updateRemove(tree);
345.
346.                     // 同后面节点合并
347.                     } else if (next != null
348.                             && (next.getChildren().size() <= tree.getOrder() / 2 || next.getChildren().size() <= 2)) {
349.
350.                         for (int i = 0; i < next.getChildren().size(); i++) {
351.                             Node child = next.getChildren().get(i);
353.                             child.setParent(this);
354.                         }
355.                         next.setChildren(null);
356.                         next.setEntries(null);
357.                         next.setParent(null);
358.                         parent.getChildren().remove(next);
359.                         validate(this, tree);
360.                         parent.updateRemove(tree);
361.                     }
362.                 }
363.             }
364.         }
365.     }
366.
367.     public void remove(Comparable key, BplusTree tree){
368.         //如果是叶子节点
369.         if (isLeaf){
370.
371.             //如果不包含该关键字，则直接返回
372.             if (!contains(key)){
373.                 return
374.             }
375.
376.             //如果既是叶子节点又是跟节点，直接删除
377.             if (isRoot) {
378.                 remove(key);
379.             }else {
380.                 //如果关键字数大于M / 2，直接删除
381.                 if (entries.size() > tree.getOrder() / 2 && entries.size() > 2) {
382.                     remove(key);
383.                 }else {
384.                     //如果自身关键字数小于M / 2，并且前节点关键字数大于M / 2，则从其处借补
385.                     if (previous != null
386.                             && previous.getEntries().size() > tree.getOrder() / 2
387.                             && previous.getEntries().size() > 2
388.                             && previous.getParent() == parent) {
389.                         int size = previous.getEntries().size();
390.                         Entry<Comparable, Object> entry = previous.getEntries().get(size – 1);
391.                         previous.getEntries().remove(entry);
392.                         //添加到首位
394.                         remove(key);
395.                     //如果自身关键字数小于M / 2，并且后节点关键字数大于M / 2，则从其处借补
396.                     }else if (next != null
397.                             && next.getEntries().size() > tree.getOrder() / 2
398.                             && next.getEntries().size() > 2
399.                             && next.getParent() == parent) {
400.                         Entry<Comparable, Object> entry = next.getEntries().get(0);
401.                         next.getEntries().remove(entry);
402.                         //添加到末尾
404.                         remove(key);
405.                     //否则需要合并叶子节点
406.                     }else {
407.                         //同前面节点合并
408.                         if (previous != null
409.                                 && (previous.getEntries().size() <= tree.getOrder() / 2 || previous.getEntries().size() <= 2
410.                                 && previous.getParent() == parent) {
411.                             for (int i = previous.getEntries().size() – 1; i >=0; i–) {
412.                                 //从末尾开始添加到首位
414.                             }
415.                             remove(key);
416.                             previous.setParent(null);
417.                             previous.setEntries(null);
418.                             parent.getChildren().remove(previous);
419.                             //更新链表
420.                             if (previous.getPrevious() != null) {
421.                                 Node temp = previous;
422.                                 temp.getPrevious().setNext(this);
423.                                 previous = temp.getPrevious();
424.                                 temp.setPrevious(null);
425.                                 temp.setNext(null);
426.                             }else {
428.                                 previous.setNext(null);
429.                                 previous = null
430.                             }
431.                         //同后面节点合并
432.                         }else if(next != null
433.                                 && (next.getEntries().size() <= tree.getOrder() / 2 || next.getEntries().size() <= 2
434.                                 && next.getParent() == parent){
435.                             for (int i = 0; i < next.getEntries().size(); i++) {
436.                                 //从首位开始添加到末尾
438.                             }
439.                             remove(key);
440.                             next.setParent(null);
441.                             next.setEntries(null);
442.                             parent.getChildren().remove(next);
443.                             //更新链表
444.                             if (next.getNext() != null) {
445.                                 Node temp = next;
446.                                 temp.getNext().setPrevious(this);
447.                                 next = temp.getNext();
448.                                 temp.setPrevious(null);
449.                                 temp.setNext(null);
450.                             }else {
451.                                 next.setPrevious(null);
452.                                 next = null
453.                             }
454.                         }
455.                     }
456.                 }
457.                 parent.updateRemove(tree);
458.             }
459.         //如果不是叶子节点
460.         }else {
461.             //如果key小于等于节点最左边的key，沿第一个子节点继续搜索
462.             if (key.compareTo(entries.get(0).getKey()) <= 0) {
463.                 children.get(0).remove(key, tree);
464.             //如果key大于节点最右边的key，沿最后一个子节点继续搜索
465.             }else if (key.compareTo(entries.get(entries.size()-1).getKey()) >= 0) {
466.                 children.get(children.size()-1).remove(key, tree);
467.             //否则沿比key大的前一个子节点继续搜索
468.             }else {
469.                 for (int i = 0; i < entries.size(); i++) {
470.                     if (entries.get(i).getKey().compareTo(key) <= 0 && entries.get(i+1).getKey().compareTo(key) > 0) {
471.                         children.get(i).remove(key, tree);
472.                         break
473.                     }
474.                 }
475.             }
476.         }
477.     }
478.
479.     /** 判断当前节点是否包含该关键字*/
480.     protected boolean contains(Comparable key) {
481.         for (Entry<Comparable, Object> entry : entries) {
482.             if (entry.getKey().compareTo(key) == 0) {
483.                 return true
484.             }
485.         }
486.         return false
487.     }
488.
489.     /** 插入到当前节点的关键字中*/
490.     protected void insertOrUpdate(Comparable key, Object obj){
491.         Entry<Comparable, Object> entry = new SimpleEntry<Comparable, Object>(key, obj);
492.         //如果关键字列表长度为0，则直接插入
493.         if (entries.size() == 0) {
495.             return
496.         }
497.         //否则遍历列表
498.         for (int i = 0; i < entries.size(); i++) {
499.             //如果该关键字键值已存在，则更新
500.             if (entries.get(i).getKey().compareTo(key) == 0) {
501.                 entries.get(i).setValue(obj);
502.                 return
503.             //否则插入
504.             }else if (entries.get(i).getKey().compareTo(key) > 0){
505.                 //插入到链首
506.                 if (i == 0) {
508.                     return
509.                 //插入到中间
510.                 }else {
512.                     return
513.                 }
514.             }
515.         }
516.         //插入到末尾
518.     }
519.
520.     /** 删除节点*/
521.     protected void remove(Comparable key){
522.         int index = –1
523.         for (int i = 0; i < entries.size(); i++) {
524.             if (entries.get(i).getKey().compareTo(key) == 0) {
525.                 index = i;
526.                 break
527.             }
528.         }
529.         if (index != –1) {
530.             entries.remove(index);
531.         }
532.     }
533.
534.     public Node getPrevious() {
535.         return previous;
536.     }
537.
538.     public void setPrevious(Node previous) {
539.         this.previous = previous;
540.     }
541.
542.     public Node getNext() {
543.         return next;
544.     }
545.
546.     public void setNext(Node next) {
547.         this.next = next;
548.     }
549.
550.     public boolean isLeaf() {
551.         return isLeaf;
552.     }
553.
554.     public void setLeaf(boolean isLeaf) {
555.         this.isLeaf = isLeaf;
556.     }
557.
558.     public Node getParent() {
559.         return parent;
560.     }
561.
562.     public void setParent(Node parent) {
563.         this.parent = parent;
564.     }
565.
566.     public List<Entry<Comparable, Object>> getEntries() {
567.         return entries;
568.     }
569.
570.     public void setEntries(List<Entry<Comparable, Object>> entries) {
571.         this.entries = entries;
572.     }
573.
574.     public List<Node> getChildren() {
575.         return children;
576.     }
577.
578.     public void setChildren(List<Node> children) {
579.         this.children = children;
580.     }
581.
582.     public boolean isRoot() {
583.         return isRoot;
584.     }
585.
586.     public void setRoot(boolean isRoot) {
587.         this.isRoot = isRoot;
588.     }
589.
590.     public String toString(){
591.         StringBuilder sb = new StringBuilder();
592.         sb.append(“isRoot: “);
593.         sb.append(isRoot);
594.         sb.append(“, “);
595.         sb.append(“isLeaf: “);
596.         sb.append(isLeaf);
597.         sb.append(“, “);
598.         sb.append(“keys: “);
599.         for (Entry entry : entries){
600.             sb.append(entry.getKey());
601.             sb.append(“, “);
602.         }
603.         sb.append(“, “);
604.         return sb.toString();
605.
606.     }
607.

原文作者：B树
原文地址: https://blog.csdn.net/basycia/article/details/52015780
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。