简体   繁体   中英

B+ Tree first insert

I am trying to implement B+ tree in java. I am confused if the first insert is saved only in the leaf or there are 2 entries ie one in root, pointing to the leaf and one in leaf(with data pointer).

If i try to enter it in both, i will have 2 leaf node that will be almost empty.

If i just enter it in leaf, my root will be of type leaf. I am not sure if root can be of type leaf.

public class BTree {
    private BTreeInnerNode root;
    private int fanout = 3;
    public BTreeInnerNode getRoot() {
        return root;
    }

    public BTree(int fanout) {
        this.fanout = fanout;
    }

    public BTree(){
        root = new BTreeInnerNode(fanout);
    }
}

Node

public class BTreeInnerNode extends BTreeNode {
    public BTreeNode[] children;
    public BTreeInnerNode(int fanout){
        super(fanout);
        nodeType = NodeType.Node;
        children = new BTreeInnerNode[2*fanout];
    }
}

Leaf

public class BTreeLeafNode extends BTreeNode {
    public int[] rid;
    public BTreeLeafNode(int fanout) {
        super(fanout);
        nodeType = NodeType.Leaf;
        rid = new int[(2*fanout)-1];
    }
}

Thanks for your help.

In B+ data entries are only stored in leafs and the inner(non-leaf) nodes only store keys. Whether you allow the root itself to be leaf is your call, but I think the implementation will be slightly easier if the root is never a leaf node and upon the first insert, you create an successor leaf node. It seems in your implementation the data and the key are the same thing and that is what is causing the confusion. Still I recommend you consider these two different things - the values in the inner nodes are "keys" and the values in the leafs are "data". This will make it easier to change your B+ tree to store different things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM