I am currently in the process of coding my B+ Tree and this is the structure of my code for reference so that my proceeding question makes more sense:
Class BPlusTree{
// some functions, members.. etc
};
Class Node{
// some functions, members.. etc
}
Now, here is my question/dilemma, since my Node is a class of its own, if I want to insert a key/value pair into a node, I would have to call the BPlusTree insert function which in turn calls the Node's own insert function ( I cannot access Node's private members directly). This seems a bit weird to me.
However, if I were to simply do this in my Node Class:
friend class BPlusTree;
I would be able to access Node's private members and thus I do not need to make an insert function inside the Node class because now my BPlusTree insert function has access to the members.
Is this approach good or bad? I'm at the point where refactoring my code isn't going to be a pain so I thought I'd ask this now before it's too late.
Adding the insert()
in the Node class isn't bad at all. If it does look weird to you when BPlusTree::insert()
calls Node::insert()
please don't be there are quite some advantages to this style of code.
My answer could be opinion based.
With the Node::insert()
node is much more complete and is a full fledged class on its own. The Node
class can be used by many other classes that you might make in the future. So you won't have to write the insert()
function again and again.
Modularity is exactly what the above paragraph means. Your code becomes much more modular this way.
I'm not against friending the BPlusTree
class but the thing is that the day you make BPlusTreeImproved
or anything that uses Node
you'll have to friend that as well to use the Node
. Which means constant pain of changing and recompiling Node
.