java实现2-3树_2-3-4树的分裂核心代码【JAVA实现】 | 学步园

public void split(Node thisNode) // split the node

{

// assumes node is full

DataItem itemB, itemC; //数据项B,C

Node parent, child2, child3;//thisNode节点父节点,第3、4个子节点

int itemIndex;

itemC = thisNode.removeItem(); // 数据项C从thisNode节点移除

itemB = thisNode.removeItem(); // 数据项B从thisNode节点移除

child2 = thisNode.disconnectChild(2); // 节点的第3个子节点从节点中断开

child3 = thisNode.disconnectChild(3); //节点的第4个子节点从节点中断开

Node newRight = new Node(); // make new node

if(thisNode==root) // 如果是根节点,创建新的节点,作为根。它是要分裂节点的父节点。

{

root = new Node(); // make new root

parent = root; // root is our parent

root.connectChild(0, thisNode); // connect to parent

}

else // this node not the root

parent = thisNode.getParent(); // 得到节点的父节点

// deal with parent

itemIndex = parent.insertItem(itemB); // 将数据项B插入父节点,并返回插入的位置

int n = parent.getNumItems(); // 父节点的数据项总数

/*

*将父节点中子节点移动到合适的位置

*/

for(int j=n-1; j>itemIndex; j--) // move parent's

{ // connections

Node temp = parent.disconnectChild(j); // one child

parent.connectChild(j+1, temp); // to the right

}

// connect newRight to parent

parent.connectChild(itemIndex+1, newRight);//新节点与父节点相连

// deal with newRight

newRight.insertItem(itemC); // item C to newRight

newRight.connectChild(0, child2); // connect to 0 and 1

newRight.connectChild(1, child3); // on newRight

} // end split()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值