欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 数据结构与算法-B(B-)树的简单实现

数据结构与算法-B(B-)树的简单实现

2025/2/22 20:12:47 来源:https://blog.csdn.net/qq_36115196/article/details/139883569  浏览:    关键词:数据结构与算法-B(B-)树的简单实现

B(B-)树定义

B树(或B-tree)是一个在计算机科学中广泛使用的数据结构,它是一种自平衡的树,能够保持数据有序。

以下是B树的特性

  1. 每个节点最多右m个孩子,二叉树是B-树的特例,其有2个孩子。
  2. 除了叶节点和根节点以外,每个内部节点至少有Math.ceil(m/2)个孩子。
  3. 如果根不是叶节点,则至少有两个孩子。
  4. 所有叶节点都出现在一层,也就是说从根到叶节点点的度(层,高度)一样。
  5. 具有K个孩子的非叶节点包含 K-1个数据,在节点内部的键值是递增的。

B树模型

B-树节点

B树的阶: B树的节点(除根节点外) 最多 有多少个孩子结点(子树),一般用字母 M 表示阶数。

如果所示节点可以连接4个树,此树为四阶B树。

每个节点中存储的数据最多不超过M-1 = 3;

image-20240619142939740

B-树的一般结构

image-20240619114028879

查询

查询在B-树中数据,如果能找到我们需要知道节点对象和节点中哪个位置(每个节点内存储多个数据)

如果没有查询到我们需要记录当前的节点对象及位置为-1(表示不存在)。

NodeBind类

定义一个NodeBind类包装节点和数据的位置。

/*** 绑定数据在节点中位置类*/
public class NodeBind {private Node node;  //当前节点private int index; //当前节点中数据的位置public Node getNode() {return node;}public void setNode(Node node) {this.node = node;}public int getIndex() {return index;}public void setIndex(int index) {this.index = index;}public NodeBind(Node node, int index) {this.node = node;this.index = index;}@Overridepublic String toString() {return "Pair{" +"node=" + node +", index=" + index +'}';}
}

查询实现

从根节点开始遍历,先从根节点开始检查是否存有要查找的数据,如果找到则返回Pair对象,如果没有则记录最后一个扫描到的对象,将其包装为Pair对象返回。

/*** 查询data在B树的节点位置** @param data* @return*/
public Pair search(int data) {Node curr = root;Node parent = null;while (curr != null) {int rowIndex = 0;while (rowIndex < curr.size) {List<Integer> datas = curr.datas;int index = datas.indexOf(data);if (index != -1) {return new Pair(curr,index);} else if (data > datas.get(rowIndex)) {rowIndex++;} else {break;}}parent = curr;curr = curr.children.get(rowIndex);}return new Pair(parent,-1);
}

添加数据

我们采用的策略是先暂时存储数据,当存入后数据超出其长度进行分裂处理。

辅助方法指定节点添加数据

private void insertBySort(Node node, int data) {int index = node.size;for (int i = 0; i < node.datas.size(); i++) {if (data < node.datas.get(i)) {index = i;break;}}node.datas.add(index, data);node.size++;
}

添加新数据

我们向B树中添加数据{12,45,9,78,80,5,3,79,20},跟踪B树的添加过程。

添加第一个元素12

创建新节点即为root节点,在datas中存入数据12。

image-20240622143104011
if (root == null) {//如果根节点不存在root = new Node();root.datas.add(data);root.size++;return;
}

添加数据45

在根节点的第二个位置添加45,找到要添加的节点。 节点的数据安装升序排序存放。

image-20240622143256238
/*
从B-树中查找节点是否存在*/
Pair findNode = search(data);
//数据已经在B树中,只更新不插入
if (findNode.getIndex() != -1) return;/*如果没有找到则插入节点- 当前要插入的节点没有满- 当前要插入的节点已满,需要分裂节点*/
Node curr = findNode.getNode();insertBySort(curr, data);if (curr.size <= M - 1) { //如果当前节点的数据没有满,最大存储M-1数据return;
}

添加数据9

查找位置添加节点,代码和4.2.3一致。

image-20240622143545722

添加数据78

重复添加过程,需要判断当前节点容量是否已满,当前节点的数据容量: size<=M-1

如果超过容量则需要分裂当前节点。

image-20240622143845811
....添加节点代码
if (curr.size <= M - 1) { //如果当前节点的数据没有满,最大存储M-1数据return;
} else { //当前节点已满System.out.println("当前节点已满,准备分裂...");split(curr);
}

分裂节点

以当前节点的中间节点为中心将左右分裂为两个节点

  • 原节点保留左侧数据
  • 新建节点保留右侧数据
  • 如果当前节点为根节点,需要新创建节点为根节点,存储中间节点数据。

image-20240622144624956

public void split(Node curr) {int mid = curr.size / 2; //找到中间节点int midVal = curr.datas.get(mid); //记录中间节点的值Node parent = curr.parent; //记录当前节点的父节点/**  创建新节点,用来存储分裂的右侧节点*/Node newNode = new Node();for (int i = mid + 1; i < curr.size; i++) {newNode.datas.add(curr.datas.get(i)); //新节点循环添加mid+1开始的节点newNode.size++; //新节点数据大小更新curr.datas.remove(i); //删除右侧数据(右侧节点放中间朝右的数据)i--; //防止引起集合索引的移动(由于删除了数据,集合大小会改变,当前位置也会调整,回退到删除为止)curr.size--; //当前节点的数据-1处理}:
}

如果当前节点是根节点,分裂两个子树,实际上当前节点和分裂节点的每一个parent要连接新的parent,parent的每一个孩子节点的孩子节点要引用这两个节点(可以自行完成,繁琐但不难)。

/**  判断当前节点是否为根节点*/
if (parent == null) {//父节点为空,当前节点为根节点root = new Node();//产生一个新的父节点root.datas.add(midVal); //拷贝当前节点的中间值到父节点中curr.datas.remove(Integer.valueOf(midVal)); //从当前对象中移除已经添加到root上的数据curr.size--;root.size++; //父节点长度更新root.children.add(curr); //父节点的孩子节点连接子节点curr.parent = root; //当前节点的父节点变成新的父节点root.children.add(newNode); //添加右侧的子节点到父节点的孩子节点中newNode.parent = root; //右接点的父节点连接新父节点return;
}

添加80

image-20240622145021014

添加5

image-20240622145120905

添加3

image-20240622145150175

再次分裂

  • 左侧分裂时检查父节点是否有空位,如果有把中间节点(9)放到父节点去
  • 分裂出一下新节点存储中间节点的右侧数据 12
  • 递归向上操作(如果父节点已满,继续向上操作直到父节点为根节点结束)

image-20240622151412419

else {//如果parent不是根节点,且当前节点已满//将中间值拷贝到parent节点中insertBySort(parent, midVal);curr.datas.remove(Integer.valueOf(midVal));curr.size--;parent.children.add(newNode); //将新parent.children.sort((o1, o2) -> o1.datas.get(o1.size - 1) - o2.datas.get(0)); //对孩子节点安装正序排序if (parent.size >= M) {split(parent);   //低估操作父节点}
}

添加数据20

image-20240622151756555

完整代码实现

B(B-)树节点

public class Node {public List<Integer> datas; //记录数据public List<Node> children; //子节点public Node parent; //父节点public int size; //节点内存储的值数量public Node() {datas = new ArrayList<>();children = new ArrayList<>();}@Overridepublic String toString() {return "Node{" +"datas=" + datas +", size=" + size +'}';}
}

B(B-)树的实现

辅助类NodeBind

辅助类NodeBind用来绑定在查询节点时,将节点和查询到B-树位置绑定。

如果节点没有查到,绑定当前节点,-1;如果查找到节点绑定当前节点,数据在节点中的位置。

/*** 绑定数据在节点中位置类*/
public class NodeBind {private Node node;  //当前节点private int index; //当前节点中数据的位置public Node getNode() {return node;}public void setNode(Node node) {this.node = node;}public int getIndex() {return index;}public void setIndex(int index) {this.index = index;}public NodeBind(Node node, int index) {this.node = node;this.index = index;}@Overridepublic String toString() {return "Pair{" +"node=" + node +", index=" + index +'}';}
}

添加节点和分裂节点的实现

package com.ffyc.tree;import com.ffyc.tree.node.Node;
import com.ffyc.tree.node.NodeBind;import java.util.*;public class BTree {private Node root;// M表示B-树的阶数// 根节点的数据数: [1,M]之间private final int M = 4; //4阶B-树/*** 添加节点数据* @param data*/public void add(int data) {if (root == null) {//如果根节点不存在root = new Node();root.datas.add(data);root.size++;return;}/*从B-树中查找节点是否存在*/NodeBind findNode = search(data);//数据已经在B树中,只更新不插入if (findNode.getIndex() != -1) return;/*如果没有找到则插入节点- 当前要插入的节点没有满- 当前要插入的节点已满,需要分裂节点*/Node curr = findNode.getNode();insertBySort(curr, data);if (curr.size <= M - 1) { //如果当前节点的数据没有满,最大存储M-1数据return;} else { //当前节点已满System.out.println("当前节点已满,准备分裂...");split(curr);}}/*** B树的节点进行分裂* @param curr 当前要分裂的节点*/public void split(Node curr) {int mid = curr.size / 2; //找到中间节点int midVal = curr.datas.get(mid);Node parent = curr.parent;/**  创建新节点,用来存储分裂的右侧节点*/Node newNode = new Node();//newNode.datas = curr.datas.subList(mid + 1, curr.size);for (int i = mid + 1; i < curr.size; i++) {newNode.datas.add(curr.datas.get(i));newNode.size++;curr.datas.remove(i);i--;curr.size--;}/**  判断当前节点是否为根节点*/if (parent == null) {//父节点为空,当前节点为根节点root = new Node();//产生一个新的父节点root.datas.add(midVal); //拷贝当前节点的中间值到父节点中curr.datas.remove(Integer.valueOf(midVal)); //从当前对象中移除已经添加到root上的数据curr.size--;root.size++; //父节点长度更新root.children.add(curr); //父节点的孩子节点连接子节点curr.parent = root; //当前节点的父节点变成新的父节点root.children.add(newNode); //添加右侧的子节点到父节点的孩子节点中newNode.parent = root; //右接点的父节点连接新父节点return;} else {//如果parent不是根节点,且当前节点已满//将中间值拷贝到parent节点中insertBySort(parent, midVal);curr.datas.remove(Integer.valueOf(midVal));curr.size--;parent.children.add(newNode); //将新parent.children.sort((o1, o2) -> o1.datas.get(o1.size - 1) - o2.datas.get(0));if (parent.size >= M) {split(parent);}}}/*** 在节点中按顺序插入数据* @param node* @param data*/private void insertBySort(Node node, int data) {int index = node.size;for (int i = 0; i < node.datas.size(); i++) {if (data < node.datas.get(i)) {index = i;break;}}node.datas.add(index, data);node.size++;}/*** 查询data在B树的节点位置** @param data* @return*/public NodeBind search(int data) {Node curr = root;  //记录根节点,从根节点开始遍历Node parent = curr.parent;  //记录父节点while (curr != null) {   //从root遍历所有节点int rowIndex = 0;  // 每个节点中数据的存储位置索引while (rowIndex < curr.size) { //扫描节点中所有数据List<Integer> datas = curr.datas;int index = datas.indexOf(data); //判断节点中的数据汇总是否包含要查找的数据if (index != -1) { //数据存在,返回终止查找return new NodeBind(curr, index);} else if (data > datas.get(rowIndex)) { //数据大于节点中的数据,继续查找下一个数据rowIndex++;} else {//找到比节点中数据小的位置跳出循环,表示查找完毕没有找到break;}}parent = curr;try {curr = curr.children.get(rowIndex);} catch (IndexOutOfBoundsException e) {curr = null;}}return new NodeBind(parent, -1);}public void print() {Queue<Node> queue = new LinkedList<>();queue.add(root);while (!queue.isEmpty()) {   //从root遍历所有节点Node curr = queue.poll();for (int i = 0; i < curr.size; i++) {System.out.print(curr.datas.get(i) + "\t");}List<Node> children = curr.children;for (Node node : children) {queue.offer(node);}}}public Node getRoot() {return this.root;}/*** 测试代码*/public static void main(String[] args) {BTree bTree = new BTree();bTree.add(12);bTree.add(45);bTree.add(9);bTree.add(78);bTree.add(80);bTree.add(5);bTree.add(3);bTree.add(79);bTree.add(20);bTree.add(1);bTree.print();}
}

运行结果

image-20240622154318947

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词