private IBNode DeleteLeaf(int value, IBNode tree) { BLeaf leaf = (BLeaf)tree; bool res = leaf.RemoveValue(value); if (res) { if (leaf.GetChildrenNum() < Math.Ceiling(M / 2m)) { BNode parent = leaf.Parent; if (parent == null) { } else { //先找到孩子节点 var siblings = parent.GetSiblings(leaf); if (siblings.Count == 0) {//非孩子节点 //do nothing } else { IBNode sibling = null; foreach (var node in siblings) { if (node.GetChildrenNum() > Math.Ceiling(M / 2m)) { sibling = node; break; } } if (sibling == null) {//兄弟节点都没有多的元素 MergeLeaf((BLeaf)siblings[0], leaf); //开始检索上级 parent = (BNode)DeleteNode(parent); } else { ShareLeafValue((BLeaf)sibling, leaf); } } } } else {//do nothing } } else {//节点不存在 } return(tree); }
private void ShareLeafValue(BLeaf leftLeaf, BLeaf rightLeaf) { int value = 0; if (leftLeaf.GetMinKey() < rightLeaf.GetMinKey()) { int i = leftLeaf.GetChildrenNum() - 1; value = leftLeaf.Values[i]; } else { value = leftLeaf.GetMinKey(); } rightLeaf.AddValue(value); leftLeaf.RemoveValue(value); }