模拟oracle 索引平衡树叶子节点

import java.util.ArrayList;

import java.util.List;

// 模拟Oracle中的ROWID,唯一标识表中的一行数据

class RowId {

private String dataFile; // 数据文件号

private int blockNumber; // 块号

private int rowNumber; // 行号

public RowId(String dataFile, int blockNumber, int rowNumber) {

this.dataFile = dataFile;

this.blockNumber = blockNumber;

this.rowNumber = rowNumber;

}

@Override

public String toString() {

return dataFile + ":" + blockNumber + ":" + rowNumber;

}

}

// B树索引的叶子节点

class LeafNode {

private int key; // 索引键值

private RowId rowId; // 对应的行ID

private LeafNode prev; // 左指针,指向前一个叶子节点

private LeafNode next; // 右指针,指向后一个叶子节点

public LeafNode(int key, RowId rowId) {

this.key = key;

this.rowId = rowId;

}

// getter和setter方法

public int getKey() {

return key;

}

public RowId getRowId() {

return rowId;

}

public LeafNode getPrev() {

return prev;

}

public void setPrev(LeafNode prev) {

this.prev = prev;

}

public LeafNode getNext() {

return next;

}

public void setNext(LeafNode next) {

this.next = next;

}

@Override

public String toString() {

return "Key: " + key + ", ROWID: " + rowId;

}

}

// 模拟B树索引的叶子节点双向链表

public class BTreeLeafNodes {

public static void main(String[] args) {

// 创建几个叶子节点

LeafNode node1 = new LeafNode(10, new RowId("f1", 100, 5));

LeafNode node2 = new LeafNode(20, new RowId("f1", 102, 3));

LeafNode node3 = new LeafNode(30, new RowId("f2", 50, 8));

LeafNode node4 = new LeafNode(40, new RowId("f2", 55, 2));

// 构建双向链表

connectNodes(node1, node2);

connectNodes(node2, node3);

connectNodes(node3, node4);

// 从头部遍历链表

System.out.println("从头部遍历叶子节点:");

LeafNode current = node1;

while (current != null) {

System.out.println(current);

current = current.getNext();

}

// 从尾部遍历链表

System.out.println("\n从尾部遍历叶子节点:");

current = node4;

while (current != null) {

System.out.println(current);

current = current.getPrev();

}

// 模拟范围查询 (key between 20 and 30)

System.out.println("\n范围查询结果 (key between 20 and 30):");

List<LeafNode> result = rangeQuery(node1, 20, 30);

for (LeafNode node : result) {

System.out.println(node);

}

}

// 连接两个叶子节点,建立双向关系

private static void connectNodes(LeafNode prevNode, LeafNode nextNode) {

prevNode.setNext(nextNode);

nextNode.setPrev(prevNode);

}

// 模拟范围查询,利用双向链表特性

private static List<LeafNode> rangeQuery(LeafNode head, int minKey, int maxKey) {

List<LeafNode> result = new ArrayList<>();

LeafNode current = head;

// 找到起始节点

while (current != null && current.getKey() < minKey) {

current = current.getNext();

}

// 收集范围内的节点

while (current != null && current.getKey() <= maxKey) {

result.add(current);

current = current.getNext();

}

return result;

}

}

相关推荐
百***49006 小时前
SQL Server查看数据库中每张表的数据量和总数据量
数据库·sql·oracle
代码or搬砖7 小时前
MyBatisPlus中的常用注解
数据库·oracle·mybatis
郑重其事,鹏程万里8 小时前
键值存储数据库(chronicle-map)
数据库·oracle
gkhost9 小时前
Linux基础——Oracle部署
oracle·表空间··instances
菜鸟小九11 小时前
mysql(锁)
数据库·mysql·oracle
云和数据.ChenGuang12 小时前
运维面试题之oracle和mysql单表最大容量
运维·mysql·oracle
aml258__13 小时前
MySQL 数据库管理入门:从创建到删除(T1)
数据库·mysql·oracle·初学数据库
悦悦欧呐呐呐呐13 小时前
数据库事务是什么,怎么用的
服务器·数据库·oracle
忘记92614 小时前
mybatis是什么
数据库·oracle·mybatis
q***925114 小时前
Springboot3 Mybatis-plus 3.5.9
数据库·oracle·mybatis