Ejemplo n.º 1
0
 /// <summary>
 /// 这是最简单的插入,没有节省空间,
 /// 如果一个节点的孩子数是2,除非关键字在这个节点的范围内,否则基本不会在往这里插入节点
 /// </summary>
 /// <param name="value"></param>
 /// <param name="tree"></param>
 /// <returns></returns>
 private IBNode Insert(int value, IBNode tree)
 {
     //说明空
     if (tree == null)
     {
         BLeaf leaf = new BLeaf(M);
         leaf.AddValue(value);
         tree = leaf;
     }
     else if (tree.IsLeaf())
     {
         tree = InsertLeaf(value, tree);
     }
     else//递归去找叶子节点
     {
         var  node   = (BNode)tree;
         bool finded = false;
         for (int i = 0; i < node.Keys.Count; i++)
         {
             if (value < node.Keys[i])
             {
                 tree = Insert(value, node.Children[i]);
             }
         }
         if (!finded)
         {
             tree = Insert(value, node.Children[node.Keys.Count]);
         }
     }
     return(tree);
 }
Ejemplo n.º 2
0
        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);
        }