Esempio n. 1
0
void BPlusTree::insert(DataType val) {
    if (!root) {
        root = new BTreeNode();
    }

    // searching a place for insert
    BTreeNode *tp = root;
    bool f = true;
    while (!tp->isLeaf) {
        f = false;
        for(ListNode* lp = tp->children.begin(); lp; lp = lp->next) {
            if (lp->index > val) {
                tp = lp->child;
                f = true;
                break;
            }
        }
        if (!f) {
            // right node (max)
            while (!tp->isLeaf) {
                tp = tp->children.end()->child;
            }
        }
    }
    
    if(!f) {
        //fixing delegates
        int tmp = tp->max();
        ListNode *d = tp->parent->children.find( tp->max() );
        tp->children.insert(val);
        while (d && d->child->parent && d->index == d->child->parent->max()) {
            d->index = val;
            d = d->child->parent->children.find( tmp );
        }
    } else {
        tp->children.insert(val);
    }
    
    
    // if D keys are in this Node then split Node
    if (tp->children.getLength() >= D) {
        split(tp);
    }
}
Esempio n. 2
0
void BPlusTree::split(BTreeNode* node) {
    DataType m = node->max();
    
    // new right Node
    BTreeNode *bro = new BTreeNode();
    bro->parent = node->parent;
    bro->lbro = node;
    bro->rbro = node->rbro;
    bro->isLeaf = node->isLeaf;
    node->rbro = bro;
    
    // splitting
    ListNode *lp = node->children.begin();
    int len1 = node->keysCount()/2;
    int len2 = node->keysCount() - len1;
    for(int i = 0; i < len1; i++) {
        lp = lp->next;
    }
    node->children.setLength( len1 );
    bro->children.setLength( len2 );
    bro->children.setBegin( lp );
    bro->children.setEnd( node->children.end() );
    node->children.setEnd( lp->prev );
    lp->prev->next = nullptr;
    lp->prev = nullptr;
    
    // fixing of delegates
    if (node == root) {
        BTreeNode *tp = new BTreeNode();
        tp->isLeaf = false;
        tp->children.insert( node->max(), node );
        tp->children.insert( bro->max(), bro );
        root = tp;
        node->parent = root;
        bro->parent = root;
    } else {
        ListNode *par = node->parent->children.find( m );
        par->child = bro;
        node->parent->children.insert( node->max(), node );
    }
}