private bool InsertNonFull(BPlusNode <TItem> node, TItem newItem) { while (true) { var indexToInsert = BinarySearchFindLast(node.Items, newItem); if (indexToInsert < node.Items.Count && CompareItems(newItem, node.Items[indexToInsert]) == 0) { return(false); //throw new Exception("Item already exists"); } // leaf node if (node.IsLeaf) { node.InsertItem(indexToInsert, newItem); return(true); } // non-leaf var child = node.Nodes[indexToInsert]; if (child.HasReachedMaxSize) { SplitChild(node, indexToInsert, child); if (CompareItems(newItem, node.Items[indexToInsert]) > 0) { indexToInsert++; } } node = node.Nodes[indexToInsert]; } }
private void SplitChild(BPlusNode <TItem> parentNode, int nodeToBeSplitIndex, BPlusNode <TItem> nodeToBeSplit) { var newNode = new BPlusNode <TItem>(_degree); parentNode.InsertItem(nodeToBeSplitIndex, nodeToBeSplit.Items[_degree - 1]); parentNode.InsertNode(nodeToBeSplitIndex + 1, newNode); var keyRange = nodeToBeSplit.GetItems(_degree, _degree - 1); newNode.InsertItems(keyRange); nodeToBeSplit.RemoveItems(_degree - 1, _degree); if (!nodeToBeSplit.IsLeaf) { var nodesRange = nodeToBeSplit.GetNodes(_degree, _degree); newNode.InsertNodes(nodesRange); nodeToBeSplit.RemoveNodes(_degree, _degree); } }