在B树中存储一对键值

时间:2014-07-13 17:30:12

标签: c++ treenode b-tree abstract-data-type

我在B-Tree工作,但我无法理解其工作原理。

寻找一些示例,我发现这个page解释了如何编写此结构的代码。

问题是:

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;
};

如果它是我树的节点,它存储了某个范围内的所有键,我可以在哪个部分找到我的数据? 假设我想存储一个字符串,我该如何获得该字符串?

我希望得到类似的内容:

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

然后,当我想通过其相关ID获取一些数据时......

BTree.getById(4);

但是,我如何声明我的节点结构呢?

谢谢!

1 个答案:

答案 0 :(得分:2)

好的,有两件事。

首先,记得在引用字符串时使用双引号"。单引号引用单个字符,您将收到错误。

其次,您链接的代码用于存储整数。是的,您可以将其展开以存储您自己的类型,但我建议您首先尝试了解它对整数的作用。

一旦理解了这一点,就将类扩展为模板,或者只是将keytype(当前为int)声明为键值对。

相关问题