c++ - 在 B 树中存储键值对

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

我在 B 树中工作,但我不明白它是如何工作的。

寻找一些示例,我找到了这个 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);

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

谢谢!

最佳答案

好的,有两件事。

首先,记得在引用字符串时使用双引号 "。单引号引用单个字符,您会出错。

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

一旦你理解了这一点,将类扩展为模板,或者简单地将键类型(当前为 int)声明为键值对。

关于c++ - 在 B 树中存储键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24725361/

相关文章:

algorithm - 为什么 B-Tree 用于文件系统?

c++ - 更改 Visual Studio 默认类设置

java - 检查输入到树中的有效路径(字符串)时遇到问题

c# - TreeNode 中的自动换行

algorithm - 二叉树什么时候比 B 树好?

algorithm - btree的插入复杂度是多少?

c++ - 如何将表示像素的字符数组读取为 unsigned int

c++ - getopt_long 将选项名称视为参数

c++ - 使用 BigInteger 数组时出现 Malloc 错误

java - JTree 如何响应已更改的 DefaultMutableTreeNode?