使用Java写一个B树

时间: 2024-05-13 18:15:30 浏览: 16
B树是一种自平衡的数据结构,常用于数据库和文件系统中。以下是一个基于Java语言实现的B树的示例代码: ```java public class BTree { private int order; // B树的阶数 private Node root; public BTree(int order) { this.order = order; root = new Node(order, true); } // 节点类 private class Node { private int count; // 节点中元素的数量 private int[] keys; // 节点中的元素 private Node[] children; // 子节点 private boolean isLeaf; // 是否为叶子节点 public Node(int order, boolean isLeaf) { this.count = 0; this.keys = new int[2 * order - 1]; this.children = new Node[2 * order]; this.isLeaf = isLeaf; } // 查找元素在节点中的索引 public int find(int key) { for (int i = 0; i < count; i++) { if (keys[i] == key) { return i; } } return -1; } // 插入元素到节点 public void insert(int key) { int i = count - 1; while (i >= 0 && keys[i] > key) { keys[i + 1] = keys[i]; i--; } keys[i + 1] = key; count++; } // 删除节点中的元素 public void remove(int key) { int i = find(key); if (i >= 0) { for (int j = i; j < count - 1; j++) { keys[j] = keys[j + 1]; } count--; } } } // 插入元素到B树 public void insert(int key) { Node node = root; if (node.count == 2 * order - 1) { Node newRoot = new Node(order, false); newRoot.children[0] = root; splitChild(newRoot, 0, root); insertNonFull(newRoot, key); root = newRoot; } else { insertNonFull(node, key); } } // 插入元素到非满节点 private void insertNonFull(Node node, int key) { int i = node.count - 1; if (node.isLeaf) { while (i >= 0 && node.keys[i] > key) { node.keys[i + 1] = node.keys[i]; i--; } node.keys[i + 1] = key; node.count++; } else { while (i >= 0 && node.keys[i] > key) { i--; } i++; if (node.children[i].count == 2 * order - 1) { splitChild(node, i, node.children[i]); if (node.keys[i] < key) { i++; } } insertNonFull(node.children[i], key); } } // 分裂子节点 private void splitChild(Node parent, int index, Node child) { Node newNode = new Node(order, child.isLeaf); newNode.count = order - 1; for (int i = 0; i < order - 1; i++) { newNode.keys[i] = child.keys[i + order]; } if (!child.isLeaf) { for (int i = 0; i < order; i++) { newNode.children[i] = child.children[i + order]; } } child.count = order - 1; for (int i = parent.count; i > index; i--) { parent.children[i + 1] = parent.children[i]; } parent.children[index + 1] = newNode; for (int i = parent.count - 1; i >= index; i--) { parent.keys[i + 1] = parent.keys[i]; } parent.keys[index] = child.keys[order - 1]; parent.count++; } // 删除元素从B树中 public void remove(int key) { remove(root, key); if (root.count == 0) { root = root.children[0]; } } // 删除元素从节点中 private void remove(Node node, int key) { int i = node.find(key); if (i >= 0) { if (node.isLeaf) { node.remove(key); } else { Node leftChild = node.children[i]; Node rightChild = node.children[i + 1]; if (leftChild.count >= order) { int predecessor = getPredecessor(leftChild); node.keys[i] = predecessor; remove(leftChild, predecessor); } else if (rightChild.count >= order) { int successor = getSuccessor(rightChild); node.keys[i] = successor; remove(rightChild, successor); } else { merge(node, i, leftChild, rightChild); remove(leftChild, key); } } } else { if (node.isLeaf) { return; } boolean flag = (i == -1) ? true : false; Node child = node.children[flag ? 0 : i + 1]; if (child.count < order) { fill(node, i, child, flag); } remove(child, key); } } // 获取节点的前继元素 private int getPredecessor(Node node) { while (!node.isLeaf) { node = node.children[node.count]; } return node.keys[node.count - 1]; } // 获取节点的后继元素 private int getSuccessor(Node node) { while (!node.isLeaf) { node = node.children[0]; } return node.keys[0]; } // 合并节点 private void merge(Node parent, int index, Node leftChild, Node rightChild) { leftChild.keys[leftChild.count] = parent.keys[index]; for (int i = 0; i < rightChild.count; i++) { leftChild.keys[leftChild.count + 1 + i] = rightChild.keys[i]; } if (!leftChild.isLeaf) { for (int i = 0; i <= rightChild.count; i++) { leftChild.children[leftChild.count + 1 + i] = rightChild.children[i]; } } leftChild.count += rightChild.count + 1; for (int i = index; i < parent.count - 1; i++) { parent.keys[i] = parent.keys[i + 1]; parent.children[i + 1] = parent.children[i + 2]; } parent.children[parent.count] = null; parent.count--; } // 填充节点 private void fill(Node parent, int index, Node child, boolean flag) { if (index != 0 && parent.children[index - 1].count >= order) { borrowFromLeft(parent, index, child, flag); } else if (index != parent.count && parent.children[index + 1].count >= order) { borrowFromRight(parent, index, child, flag); } else { if (index == parent.count) { index--; } merge(parent, index, child, parent.children[index + 1]); } } // 从左侧节点借元素 private void borrowFromLeft(Node parent, int index, Node child, boolean flag) { Node leftChild = parent.children[index - 1]; for (int i = child.count; i > 0; i--) { child.keys[i] = child.keys[i - 1]; } if (!child.isLeaf) { for (int i = child.count + 1; i > 0; i--) { child.children[i] = child.children[i - 1]; } child.children[0] = leftChild.children[leftChild.count]; } child.keys[0] = parent.keys[index - 1]; parent.keys[index - 1] = leftChild.keys[leftChild.count - 1]; child.count++; leftChild.count--; } // 从右侧节点借元素 private void borrowFromRight(Node parent, int index, Node child, boolean flag) { Node rightChild = parent.children[index + 1]; child.keys[child.count] = parent.keys[index]; if (!child.isLeaf) { child.children[child.count + 1] = rightChild.children[0]; } parent.keys[index] = rightChild.keys[0]; for (int i = 0; i < rightChild.count - 1; i++) { rightChild.keys[i] = rightChild.keys[i + 1]; } if (!rightChild.isLeaf) { for (int i = 0; i < rightChild.count; i++) { rightChild.children[i] = rightChild.children[i + 1]; } rightChild.children[rightChild.count] = null; } rightChild.count--; child.count++; } // 查找元素是否存在于B树中 public boolean contains(int key) { return search(root, key) != null; } // 在B树中查找元素 private Node search(Node node, int key) { int i = 0; while (i < node.count && key > node.keys[i]) { i++; } if (i < node.count && key == node.keys[i]) { return node; } if (node.isLeaf) { return null; } else { return search(node.children[i], key); } } // 打印B树 public void print() { print(root, ""); } // 打印节点 private void print(Node node, String indent) { if (node == null) { return; } System.out.print(indent); for (int i = 0; i < node.count; i++) { System.out.print(node.keys[i] + " "); } System.out.println(); if (!node.isLeaf) { for (int i = 0; i <= node.count; i++) { print(node.children[i], indent + " "); } } } } ``` 这个B树实现支持插入、删除和查询操作,并提供了打印B树的功能。在实际使用时,可以根据需求进行修改和扩展。

相关推荐

最新推荐

recommend-type

完整B树算法Java实现代码

主要为大家详细介绍了完整的B树算法Java实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Java后端面试问题整理.docx

• 熟悉常用集合数据结构(数组、Hashmap、ConcurrentHashMap、HashTable、ArrayList、Vetor、LinkedList、HashSet、TreeSet、LinkedHashSet),了解AVL、RBtree、B/B+树、跳表 • 熟悉常见异常分类以及处理,熟悉反射...
recommend-type

安全隐患台账(模版).xls

安全隐患台账(模版).xls
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

优化MATLAB分段函数绘制:提升效率,绘制更快速

![优化MATLAB分段函数绘制:提升效率,绘制更快速](https://ucc.alicdn.com/pic/developer-ecology/666d2a4198c6409c9694db36397539c1.png?x-oss-process=image/resize,s_500,m_lfit) # 1. MATLAB分段函数绘制概述** 分段函数绘制是一种常用的技术,用于可视化不同区间内具有不同数学表达式的函数。在MATLAB中,分段函数可以通过使用if-else语句或switch-case语句来实现。 **绘制过程** MATLAB分段函数绘制的过程通常包括以下步骤: 1.
recommend-type

SDN如何实现简易防火墙

SDN可以通过控制器来实现简易防火墙。具体步骤如下: 1. 定义防火墙规则:在控制器上定义防火墙规则,例如禁止某些IP地址或端口访问,或者只允许来自特定IP地址或端口的流量通过。 2. 获取流量信息:SDN交换机会将流量信息发送给控制器。控制器可以根据防火墙规则对流量进行过滤。 3. 过滤流量:控制器根据防火墙规则对流量进行过滤,满足规则的流量可以通过,不满足规则的流量则被阻止。 4. 配置交换机:控制器根据防火墙规则配置交换机,只允许通过满足规则的流量,不满足规则的流量则被阻止。 需要注意的是,这种简易防火墙并不能完全保护网络安全,只能起到一定的防护作用,对于更严格的安全要求,需要
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

揭秘MATLAB分段函数绘制技巧:掌握绘制分段函数图的精髓

![揭秘MATLAB分段函数绘制技巧:掌握绘制分段函数图的精髓](https://img-blog.csdnimg.cn/direct/3821ea2a63d44e65925d8251196d5ca9.png) # 1. MATLAB分段函数的概念和基本语法** 分段函数是一种将函数域划分为多个子域,并在每个子域上定义不同函数表达式的函数。在MATLAB中,可以使用`piecewise`函数来定义分段函数。其语法为: ``` y = piecewise(x, x1, y1, ..., xn, yn) ``` 其中: * `x`:自变量。 * `x1`, `y1`, ..., `xn`,