Esempio 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);
 }
Esempio n. 2
0
        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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        private void MergeLeaf(BLeaf leftLeaf, BLeaf rightLeaf)
        {
            BLeaf newLeaf = new BLeaf(M);

            //位置调换一下
            if (leftLeaf.GetMinKey() > rightLeaf.GetMinKey())
            {
                var temp = leftLeaf;
                leftLeaf  = rightLeaf;
                rightLeaf = temp;
            }
            var list = leftLeaf.Values;

            list.AddRange(rightLeaf.Values);
            leftLeaf.Values   = list;
            leftLeaf.NextLeaf = rightLeaf.NextLeaf;
            var parent = leftLeaf.Parent;

            parent.RemoveChildren(rightLeaf);
        }
Esempio n. 5
0
        private void SplitLeaf(ref BLeaf oldLeaf, out BLeaf newLeaf, int value)
        {
            newLeaf = new BLeaf(M);
            var list = new List <int>(oldLeaf.Values);

            list.Add(value);
            list.Sort();
            int count = list.Count;
            var list1 = list.GetRange(0, count / 2);
            var list2 = list.GetRange(count / 2, count - count / 2);

            oldLeaf.Values = list1;
            newLeaf.Values = list2;

            //把叶子连起来
            newLeaf.NextLeaf = oldLeaf.NextLeaf;
            newLeaf.PrevLeaf = oldLeaf;
            oldLeaf.NextLeaf = newLeaf;
            // newLeaf.Parent = oldLeaf.Parent;

            // oldLeaf.Parent.AddKey(list2[0]);
        }