Esempio n. 1
0
        public void Insert(SimpleBTreeNode x, Record record)
        {
            int i = x.NumKeys;

            if (x.IsLeaf)
            {
                while (i >= 0)
                {
                    int cmp = record.GetKey().CompareTo(x.records[i].GetKey());
                    if (cmp > 0)
                    {
                        break;
                    }
                    x.records[i + 1] = x.records[i];
                    i--;
                }
                x.records[i + 1] = record;
                x.NumKeys++;
            }
            else
            {
                while (i >= 0)
                {
                    int cmp = record.GetKey().CompareTo(x.records[i].GetKey());
                    if (cmp > 0)
                    {
                        break;
                    }
                    i--;
                }
                //the loop would have i point to the first key in the node that is smaller than the new record's key
                //link to the right of this key would point to the node where the new record would have to be inserted
                //so we increment i to point to the appropriate child to traverse to.
                i = i + 1;
                SimpleBTreeNode child = x.child[i];
                if (child.IsFull())
                {
                    SplitChild(x, i, child);
                    //i will now have new key after split
                    int cmp = record.GetKey().CompareTo(x.records[i].GetKey());
                    //if the record's key is more than newly added key in current node,
                    //the new record will have to go to the child to the right of the new key
                    //so increment i by 1
                    if (cmp > 0)
                    {
                        i = i + 1;
                    }
                }
                Insert(x.child[i], record);
            }
        }
Esempio n. 2
0
 public void Insert(Record record)
 {
     if (root.IsFull())
     {
         SimpleBTreeNode newroot = new SimpleBTreeNode(MinDegree, false);
         newroot.child[0] = root;
         root             = newroot;
         SplitChild(root, 0, root.child[0]);
         Insert(root, record);
     }
     else
     {
         Insert(root, record);
     }
 }