Exemplo n.º 1
0
        protected void SplitLeaf(BPlusTreeNode <TKey, TValue> leafToSplit)
        {
            int pivotIndex = leafToSplit.Values.Count / 2;

            BPlusTreeNodeSortedValues <TKey, TValue> leftValues  = new BPlusTreeNodeSortedValues <TKey, TValue>();
            BPlusTreeNodeSortedValues <TKey, TValue> rightValues = new BPlusTreeNodeSortedValues <TKey, TValue>();

            leafToSplit.Values.CopyTo(leftValues, 0, pivotIndex + 1);
            leafToSplit.Values.CopyTo(rightValues, pivotIndex + 1, leafToSplit.Values.Count);

            BPlusTreeNode <TKey, TValue> leftNode = new BPlusTreeNode <TKey, TValue>(leftValues,
                                                                                     leafToSplit.ParentNode, null, null, leafToSplit.ParentTree, false);
            BPlusTreeNode <TKey, TValue> rightNode = new BPlusTreeNode <TKey, TValue>(rightValues,
                                                                                      leafToSplit.ParentNode, null, null, leafToSplit.ParentTree, false);

            leftNode.RightLeafNode = rightNode;
            leftNode.LeftLeafNode  = leafToSplit.LeftLeafNode;

            rightNode.LeftLeafNode  = leftNode;
            rightNode.RightLeafNode = leafToSplit.RightLeafNode;

            if (leafToSplit.LeftLeafNode != null)
            {
                leafToSplit.LeftLeafNode.RightLeafNode = leftNode;
            }
            if (leafToSplit.RightLeafNode != null)
            {
                leafToSplit.RightLeafNode.LeftLeafNode = rightNode;
            }



            if (!leafToSplit.IsRoot)
            {
                leafToSplit.ParentNode.Links.Remove(leafToSplit.Values.Keys.Max());

                leafToSplit.ParentNode.Links.Add(rightNode.Values.Keys.Last(), rightNode);
                leafToSplit.ParentNode.Links.Add(leftNode.Values.Keys.Last(), leftNode);
            }
            else
            {
                BPlusTreeNode <TKey, TValue> newRoot =
                    new BPlusTreeNode <TKey, TValue>(new BPlusTreeNodeSortedLinks <TKey, TValue>(), null, null, null,
                                                     leafToSplit.ParentTree, true);

                leftNode.ParentNode  = newRoot;
                rightNode.ParentNode = newRoot;

                newRoot.Links.Add(leftNode.Values.Keys.Last(), leftNode);
                newRoot.Links.Add(rightNode.Values.Keys.Last(), rightNode);

                Head = newRoot;

                NodesCount++;
            }

            if (leftNode.ParentNode.KeysCount > MaxDegree)
            {
                Split(leftNode.ParentNode);
            }
        }