Search code examples
c++treenodeb-treeabstract-data-type

Storing a pair key-value in a B-Tree


I'm working in a B-Tree, but I can't undesrstand how it works.

Looking for some examples, I've found this page that explains how to write the code for this structure.

The problem is:

class BTreeNode {
    private
        int *keys;  // An array of keys
        int t;      // Minimum degree (defines the range for number of keys)
        BTreeNode **C; // An array of child pointers
        int n;     // Current number of keys
        bool leaf; // Is true when node is leaf. Otherwise false
    public:
        BTreeNode(int _t, bool _leaf);   // Constructor

    friend class BTree;
};

If it is the node of my tree, that stores all the keys in some range, in which part can I found my data? Suppose that I want to store a string, how can I get that string?

I want to get something like:

BTree.insert(1,'hello');
BTree.insert(2,' ');
BTree.insert(3,'world');
BTree.insert(4,'!');

Then, when I want to get some data by its associated ID...

BTree.getById(4);

But, how can I declare my node strcture for something like that?

Thanks!


Solution

  • Ok, two things.

    Firstly, remember to use double quotes " when referring to strings. Single quotes refer to a single character and you will get errors.

    Secondly, the code you have linked is made to store integers. Yes, you can expand it to store your own type but I recommend trying to understand what it is doing with the integers first.

    Once you understand that, expand the class to a template, or simply declare the keytype (currently int) to be a key-value pair instead.