我在 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/